I have not having any luck getting my .htaccess with mod_rewrite working. Basically all I am trying to do is remove \'www\' from \"http://www.example.com\" and \"https://ww
RewriteEngine on
RewriteCond %{HTTPS} =on
RewriteCond %{HTTP_HOST} ^www\.(.*) [NC]
RewriteRule ^(.*)$ https://%1$1 [L,R=301]
RewriteCond %{HTTP_HOST} ^www\.(.*) [NC]
RewriteRule ^(.*)$ http://%1$1 [L,R=301]
Two slight changes from other answers:
The %1
backreference in the RewriteRule
takes from the last matched RewriteCond
, so the check for HTTPS must come before the check for www in the host name.
%1$1
doesn't need a slash in the middle, because you'll get that from the path match in the RewriteRule
.
One final piece of advice: since you have control over the VirtualHost
sections in the main Apache configuration, it would be faster to put these rules there. Additionally, you'd split them, putting the plain HTTP one in *:80 and HTTPS in *:443, meaning you can remove the RewriteCond %{HTTPS} =on
entirely, since it would only apply to requests intended for that virtual host.
You can easily test if your htaccess is being read or not:
Put garbage in it, like:
Options +FollowSymLinks
This is garbage
RewriteEngine on
RewriteCond %{HTTP_HOST} ^www\.(.*) [NC]
RewriteRule (.*) //%1/$1 [L,R=301]
If you get a 500 error (Internal Server Error) then it IS being read.
If it is, you should enable the RewriteLog in the main server configuration (not in .htaccess) like this:
RewriteLog "/tmp/rewrite.log"
RewriteLogLevel 9
And then check the file /tmp/rewrite.log to see what's happening. Report back with results.
(Stack Overflow isn't a good debugging medium, IRC is better for that, try #apache@irc.freenode.net)
Good luck.
First of all, check to make sure that mod_rewrite is actually loading. You can do that with apache2ctl:
[root@host ~]# apache2ctl -t -D DUMP_MODULES 2>&1 |grep rewrite
rewrite_module (shared)
If it is not, then you might have to run 'a2enmod rewrite'
Next, test to see if your .htaccess file is even being read. I typically do this by putting some garbage inside the .htaccess file, then loading a page in that directory in my browser and verifying that I get a 500 error
On a side note, as others have mentioned, if you have the ability to modify your Apache config directly, you should put the Rewrite Rules there instead of in the .htaccess file as it is less efficient. Apache has to first decide in which directory to look for the .htaccess file, then read it, then perform the rewrites. If the RewriteRules are specified inside your VirtualHost directives, then it can do them before finding the .htaccess file. Specifying them inside your VirtualHost also means that it doesn't matter if your .htaccess file is being read. It would look something like this:
<VirtualHost *:80>
.... existing config ....
RewriteEngine on
RewriteCond %{HTTP_HOST} ^www\.(.*) [NC]
RewriteRule (.*) http://%1/$1 [L,R=301]
</VirtualHost>
<VirtualHost *:443>
.... existing config ....
RewriteEngine on
RewriteCond %{HTTP_HOST} ^www\.(.*) [NC]
RewriteRule (.*) https://%1/$1 [L,R=301]
</VirtualHost>
Try this little trick:
RewriteEngine on
RewriteCond %{HTTPS}s/%{HTTP_HOST} ^(on(s)|[^/]+)/www\.(.+) [NC]
RewriteRule ^ http%2://%3%{REQUEST_URI} [L,R=301]
How about this rewrite rule?
RewriteEngine On
RewriteCond %{HTTP_HOST} ^www.example.com$ [NC]
RewriteRule ^(.*)$ http://example.com/$1 [R=301,L]
For me, a symlink was missing
ln -s /etc/apache2/mods-available/rewrite.load /etc/apache2/mods-enabled/rewrite.load