------------------------------------------------------------------------------- Rewrite rules are a pain. WARNING: "Options +FollowSymlinks" Must be enabled for mod_rewrite to work! Some servers may enable this at root level but disallow it in .htaccess Causing a 500 error. If that is the case just remove it and the rewrite rules will continue to work. When things are not working Add # # ONLY FOR TESTING REWRITE RULES!!!!! # RewriteLog "/tmp/rewrite.log" #RewriteLogLevel 9 RewriteLogLevel 5 Now load the page, immediatally hit 'STOP' on the browser and restart your apache within a couple of seconds. As setting of 1 has almost no information, 5 is useful, 2 will probably have enough info, 9 is gigabytes of information and seriously impact the server ------------------------------------------------------------------------------- # Make all accesses from a specific web server address # EG may any CNAME such as "example.com" to "www.example.com" # And for a site running on port 80 RewriteCond %{HTTP_HOST} !^www\.example\.com [NC] RewriteCond %{HTTP_HOST} !^$ RewriteRule ^/(.*) http://www.example.com/$1 [L,R] Redirect top level to a php script with path! RewriteEngine On RewriteRule ^/$ https://%{HTTP_HOST}/index.php/pneumonia [R] Note.... flags: C - chain to following rule/condition L - last no more rules R - redirect client ------------------------------------------------------------------------------- # Redirect whatever.htm requests to whatever.php # The NC makes it case insensitive Options +FollowSymlinks RewriteEngine on RewriteRule ^(.*)\.htm$ $1.php [NC] # However "whatever.htm" will remain in the users browser URL ------------------------------------------------------------------------------- # Redirect old to new page - regardless of directory/server location! # # Note the follow standard method for directory renames.. # # RewriteRule ^olddir(.*)$ newdir$1 [R] # # However in per-directory .htaccess files, in a users home directory, it # fails due to pre ~home expandsion. That is the prepended filepath is # incorrect for the redirection. Usally that is solved by using a # "RewriteBase", but that does not help if the directory is on multiple # machine and sub-directorys! # # As such the proper solution is to recover the originally requested URI # path first so that you can then modify the original request. # # The trailing ::: marker is needed to to a weird doubling # of the trailing path/file that happens on my apache server. # # RewriteRule ^olddir/.*$ %{REQUEST_URI}::: [C] # RewriteRule ^(.*)/olddir/(.*)::: $1/newdir/$2 [R,L] # Relative URI modification -- Independant of what server it is on RewriteRule ^mosaics/ %{REQUEST_URI}::: [C] RewriteRule ^(.*)/mosaics/(.*)::: $1/layers/$2 [R=301,L] RewriteRule ^manipulate/ %{REQUEST_URI}::: [C] RewriteRule ^(.*)/manipulate/(.*)::: $1/convolve/$2 [R=301,L] # The 'L' tells the server to stop processing rules for this request at that # point. # 301 - permanent redirection 302 - Temporary redirection # # WARNING about 301 # # Once the browser has been redirected permanently to the wrong address, if # you then go on to alter the wonky rule, your browser will still be # redirected to the old address (because it's a browser thing), and you may # even go on to fix, and then break the rule all over again without ever # knowing it. Changes to 301 redirects can take a long time to show up in your # browser. # # For testing use [R] instead of [R=301] until you have finished testing. ------------------------------------------------------------------------------- # Redirect any not accessing via a specific host to use # that specific host ALWAYS # # From Online .htaccess Editor # http://www.htaccesseditor.com/en.shtml#a_WWW # Options +FollowSymlinks RewriteEngine on RewriteCond %{HTTP_HOST} ^(my\.domain\.com/~anthony)(:80)? [NC] RewriteRule ^(.*) http://www.my.domain.com/~anthony/$1 [R=301,L] ------------------------------------------------------------------------------- # Redirect any access from www.cit.gu.edu.au for a sub-directory # to its new offical web site http://www.imagemagick.org/Usage/ # Any sub-path and query string is also added to the new URL. # R = Redirect to external, L = last rewrite, QSA = include query string RewriteEngine On # General redirect to the offical web site from old site RewriteCond %{SERVER_NAME} ^www\.cit\.gu\.edu\.au$ RewriteRule ^(.*) http://www.imagemagick.org/Usage/$1 [R,L,QSA] ------------------------------------------------------------------------------- For more cookbook examples see... http://corz.org/serv/tricks/htaccess2.php # 'flatting' script arguments. # That is making a link look like a path when path components are script # arguments RewriteRule ^blog/([0-9]+)-([a-z]+) http://corz.org/blog/index.php?archive=$1-$2 [NC] thus http://corz.org/blog/2003-nov internally means http://corz.org/blog/index.php?archive=2003-nov OR Shortening long URL's Remove the 'www' from the URL host Note the use of %1 and $1 Options +FollowSymlinks RewriteEngine on RewriteCond %{HTTP_HOST} ^www\.(.*) [NC] RewriteRule ^(.*)$ http://%1/$1 [R=301,NC,L] -------------------------------------------------------------------------------