------------------------------------------------------------------------------- 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 ------------------------------------------------------------------------------- Map sub-directory from http to https =======8<-------- # This will enable the Rewrite capabilities RewriteEngine On # This checks to make sure the connection is not already HTTPS RewriteCond %{HTTPS} != on # This rule will redirect users from HTTP to HTTPS protocols RewriteRule . https://%{SERVER_NAME}%{REQUEST_URI} [R,L] =======8<-------- WARNING: does not work for redirections involving user homes and ".htaccess" files I do not know why as it does work for files in that directory, just not the "index.html" file, when referanced as "http://.../directory/" EG: http://localhost/~anthony/message.txt redirected to https://localhost/home/anthony/public_html/message.txt which will return "404 Not Found" --- Arrggghhh.... Alternative Example.... ... # Force use of HTTPS RewriteEngine On RewriteCond %{HTTPS} off RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} ------------------------------------------------------------------------------- # Make all accesses from a specific web server address # EG make 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 URL not accessing a site via a specific host to use # that specific host ALWAYS Options +FollowSymlinks RewriteEngine on RewriteCond %{HTTP_HOST} ^(my\.fhi\.com/)(:80)? [NC] RewriteRule ^(.*) http://fhi.net/$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] -------------------------------------------------------------------------------