Forum Moderators: phranque
RewriteCond %{HTTP_HOST} ^example\.com
RewriteRule (.*) http://www.example.com [R=301,L]
Permanently redirect www.example.com to example.com:
RewriteCond %{HTTP_HOST} ^www\.example\.com
RewriteRule (.*) http://example.com [R=301,L]
Permanently redirect anything except www.example.com to www.example.com:
RewriteCond %{HTTP_HOST} .
RewriteCond %{HTTP_HOST} !^www\.example\.com
RewriteRule (.*) http://www.example.com [R=301,L]
Permanently redirect anything except example.com to example.com:
RewriteCond %{HTTP_HOST} .
RewriteCond %{HTTP_HOST} !^example\.com
RewriteRule (.*) http://example.com [R=301,L]
If you use any of the samples above and are redirected to Google, it means you have a browser hijacker on your computer, or that someone has modified the "hosts" file on that computer. On Win XP, the hosts file is in C:\WINDOWS\SYSTEM32\DRIVERS\etc. It is a plain-text file and can be edited with NotePad. If this is a personal machine, then it is almost always safe to delete the hosts file (or just rename it to hosts.old).
Jim
www.example.com/adir/afile.html
should redirect to:
example.com/adir/afile.html
etc.
Note the caret in the last line of the rewrite code below: ^(.*) Should that be there, or should I remove it?
I also have a few other .htaccess files that have things like this in them:
RedirectPermanent /adir/index.html [a_different_domain.com...]
Will the "L" in the last line of the rewrite code have an effect on these other .htaccess files?
Should I consolidate all of my .htaccess files into one .htaccess file in the root, placing the RedirectPermanent's before the rewrite code? Or, does it not matter, and the "L" only applies to that one .htaccess file?
I tried the rewrite code out briefly and it seemed to work and the other redirects worked too, but I want to be sure before I leave it there for any length of time.
- BEGIN REWRITE CODE -
RewriteEngine on
Options +FollowSymlinks
RewriteBase /
RewriteCond %{HTTP_HOST} www.example.com
RewriteRule ^(.*)$ [example.com...] [R=301,L]
- END REWRITE CODE -
If you have privileges to put the code in httpd.conf, it's much more efficient there. This is because code in httpd.conf is compiled at server restart, whereas code in .htaccess is interpreted for each HTTP request. But you do have to restart your server before any httpd.conf changes take effect.
If you want the rules to apply to only one virtual host, then put them in that VirtualHost container.
Note the subtle differences between URL patterns in .htaccess and httpd.conf; the URL is localized to the current directory in .htaccess, but not in httpd.conf. As a simple example, to detect a request for index.html in .htaccess, you'd use a RewriteRule pattern of "^index\.html$" whereas in httpd.conf, it would be "^/index\.html$".
Don't confuse mod_rewrite directives with mod_alias directives. mod_rewrite flags like [L] won't work for mod_alias.
The order in which you place directives for different modules makes no difference in Apache; Each module takes its turn scanning your files for directives it understands and executes them. This order is determined by the LoadModule list order (reversed) in Apache 1.x, and by pre-defined module "priority" in Apache 2.x.
Jim
Jim