FREE THOUGHT · FREE SOFTWARE · FREE WORLD

Home  »  Htaccess  »  htaccess Tricks for Webmasters

by 12 comments

Apache For Webmasters is some of my favorite htaccess examples from some of my favorite .htaccess tutorials. These cut-and-paste ready htaccess code snippets are very useful for website and server administrators.



.htaccess Tutorial Index |


When site is "Under Construction"

This lets google crawl, lets me access (1.1.1.1) without a password, and lets access from anyone WITH a password. It also allows for XHTML and CSS validation through w3.org

Article: Instruct Search Engines with a 503 Service Unavailabe header and Retry-After header while site is offline

update combined allow code credit: megaspaz

AuthName "Under Development"
AuthUserFile /web/askapache.com/.htpasswd
AuthType basic
Require valid-user
Order Deny,Allow
Deny from all
Allow from 1.1.1.1 w3.org googlebot.com google.com google-analytics.com
Satisfy Any

Redirect everyone to different site except 1 IP

ErrorDocument 403 http://www.htaccesselite.com
Order deny,allow
Deny from all
Allow from 1.1.1.1

Redirect all but 1 IP to different site, using mod_rewrite

RewriteEngine On
RewriteBase /
RewriteCond %{REMOTE_HOST} !^1.1.1.1
RewriteRule .* http://www.htaccesselite.com [R=302,L]

Redirect Everyone but you to alternate page on your server.

RewriteEngine On
RewriteBase /
RewriteCond %{REMOTE_HOST} !^1.1.1.1
RewriteCond %{REQUEST_URI} !/temporary-offline.html$
RewriteRule .* /temporary-offline.html [R=302,L]

Set the Timezone of the server

SetEnv TZ America/Indianapolis

Set the Server Administrator Email

Include a link to your email address in Apache-Generated Error Documents. Shows up on default Apache error pages

ServerSignature EMail
SetEnv SERVER_ADMIN spamkill@spamkill.com

Turn off the ServerSignature

Turns off the address part of your Server Signature in Apache generated Error Documents.

ServerSignature Off

Force File download, do not display/open in browser

In your HTML directly link to the file.. Download Movie1. Now you will get a pop-up box asking whether you want to save the file or open it.

AddType application/octet-stream .avi
AddType application/octet-stream .mpg
AddType application/octet-stream .mov
AddType application/octet-stream .pdf

Process .gif files with a cgi script

When a user requests a .gif file (image/gif) the server instead of serving that .gif file it serves the results of /cgi-bin/filter.cgi

Action image/gif /cgi-bin/filter.cgi

Process Requests with certain Request Methods

Article: Request Methods

Script PUT /cgi-bin/upload.cgi
Script HEAD /cgi-bin/head-robot.cgi

Make any file be a certain filetype

Makes image.gif, blah.html, index.cgi all act as php

ForceType application/x-httpd-php

Use IfModule for robustness

Basically if the module is found it will perform the code inside the tag.


# any mod_rewrite directives here


# any Expires Directives go here


# any Header directives go here


htaccess Guide Sections

.htaccess Tutorial Index |


Tags

April 10th, 2007

Comments Welcome

  • mindrape

    Script HEAD directive doesn't seem to work in apache 2.2 :( any ideas?

  • AskApache

    @ mindrape

    Yeah I noticed that too.. Instead you can use mod_rewrite.

    RewriteCond %{REQUEST_METHOD} HEAD
    RewriteRule .* /cgi-bin/head-robot.cgi
  • Per Qvindesland

    Hello All,

    Appologies but I am having a slight problem, I am hosting mirrors for some open sourge mirrors and I am about to start for the Apache project but there is some requirements that apache has that I have never done before and I really would like some input, I have put my .htaccess file on the document root outside the apache directory and the other mirror directories, apache wants to have the following in

    IndexOptions FancyIndexing NameWidth=* FoldersFirst ScanHTMLTitles DescriptionWidth=*
    HeaderName HEADER.html
    ReadmeName README.html
    AllowOverride FileInfo Indexes
    Options Indexes SymLinksIfOwnerMatch

    but if i put this in and edit the path I get a 500 error, could you please tell me where I am going wrong?

    Regards
    Per Qvindesland

  • Love peace life

    Really one stop article for learning about .htacess.
    You should put complete .htaccess file content as you explain rather than part of it. This will help immensely to users like us.
    Thanks again.

  • driver66

    So, when are you going to publish this great info in a book for all us old school learners who can't seem to absorb information without a highlighter? ,

  • Bagesh Singh

    very nice code i also done these type of code but you did very well

    good keep it up:)

  • Ben

    Thanks for providing these! Some of the most powerful commands for webmasters ever. Great work.

  • John

    Hey this looks awesome. thanks for providing all the difference scenarios1 will try to install Redirect Everyone but you to alternate page on your server.. thanks!

  • Armando

    I'm using WP Super Cache plugin to improve loading speed of the web and works well. But shows wrong characters for accents after performing the compression of the cached pages. I think you should change the htaccess to correct it but I'm not sure how.

  • Dabbler

    I'm trying to do something that people will think is simple and unwise: a rewrite rule that tests if the The_Request equals HTTP_REFERER.

    But I think that these variables cannot be used as the Condition string.

    Is there another way to do it?

  • Fred

    Is there a way to invoke the htaccess u/p prompt within HTML? For example, instead of having a link to a password-protected page which, when clicked, pulls up the prompt for the username and password, is there a way to incorporate the prompt (the username and password fields) on the page where the link exists? This would allow a person to provide clues as to the username and password, and the visitor would not need to write them down; he could simply fill in the username and password fields WHILE reading the clues, which would allow for some measure of security from automated spam bots. In short, he would see the clues, fill in the fields and, let's say, click "Enter". The proper credentials would be immediately passed on, and the protected page (in the protected directory) would load without the additional step of a pop-up prompt. Please advise.

  • Fred

    Is there a way to invoke the htaccess u/p prompt within HTML? For example, instead of having a link to a password-protected page which, when clicked, pulls up the prompt for the username and password, is there a way to incorporate the prompt (the username and password fields) on the page where the link exists? This would allow a person to provide clues as to the username and password, and the visitor would not need to write them down; he could simply fill in the username and password fields WHILE reading the clues, which would allow for some measure of security from automated spam bots. In short, he would see the clues, fill in the fields and, let's say, click "Enter". The proper credentials would be immediately passed on, and the protected page (in the protected directory) would load without the additional step of a pop-up prompt. Please advise.


Related Articles


My Online Tools
Popular Articles


Hacking and Hackers

The use of "hacker" to mean "security breaker" is a confusion on the part of the mass media. We hackers refuse to recognize that meaning, and continue using the word to mean someone who loves to program, someone who enjoys playful cleverness, or the combination of the two. See my article, On Hacking.
-- Richard M. Stallman









[hide]

It's very simple - you read the protocol and write the code. -Bill Joy

Except where otherwise noted, content on this site is licensed under a Creative Commons Attribution 3.0 License, just credit with a link.
This site is not supported or endorsed by The Apache Software Foundation (ASF). All software and documentation produced by The ASF is licensed. "Apache" is a trademark of The ASF. NCSA HTTPd.
UNIX ® is a registered Trademark of The Open Group. POSIX ® is a registered Trademark of The IEEE.

+Askapache | askapache

Site Map | Contact Webmaster | License and Disclaimer | Terms of Service

↑ TOPMain