Reputation: 451
I have 10 domains all pointing to a single directory. I need all of them to 301 redirect URLs without www. to having www. for example:
http://domain1.com/foo.php -301-> http://www.domain1.com/foo.php
I've achieved this fine with normal htaccess
RewriteCond %{HTTP_HOST} ^domain1.com$
RewriteRule ^(.*)$ "http\:\/\/www\.domain1\.com\/$1" [R=301,L]
Ideally the domain and TLD wouldn't be defined in the htaccess - I'm trying instead to do something more like
RewriteCond %{HTTP_HOST} ^[^^www.$](.*)$
RewriteRule ^(.*)$ "http\:\/\/www\.$1" [R=301,L]
ie, if HTTP_HOST
doesn't start with www. then redirect. I believe the part I'm doing wrong with [^^www.%]
. I thought [^ ]
meant 'not' and ^ %
contained a string, therefor [not[www.]]
I've read the apache mod rewrite documentation a lot and can't seem to figure it out
My full current htaccess:
RewriteEngine On
# Force www.
RewriteCond %{HTTP_HOST} ^domain1.com$
RewriteRule ^(.*)$ "http\:\/\/www\.domain1\.com\/$1" [R=301,L]
# Don't user-friendly remap resource files
RewriteCond %{REQUEST_FILENAME} ^.*^.ico$$ [OR]
RewriteCond %{REQUEST_FILENAME} ^.*^.css$$ [OR]
RewriteCond %{REQUEST_FILENAME} ^.*^.js$$ [OR]
RewriteCond %{REQUEST_FILENAME} ^.*^.gif$$ [OR]
RewriteCond %{REQUEST_FILENAME} ^.*^.png$$ [OR]
RewriteCond %{REQUEST_FILENAME} ^.*^.jpg$$ [OR]
RewriteCond %{REQUEST_FILENAME} ^.*^.jpeg$$ [OR]
RewriteCond %{REQUEST_FILENAME} ^.*^.xml$$ [OR]
RewriteCond %{REQUEST_FILENAME} ^.*^.txt$$
RewriteRule ^(.*)$ $1 [L]
# Remap /page/filter/?query to /index.php?page=page&filter=filter&query
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^/?([^/]+)/*([a-zA-Z0-9-]*)/?$ /index.php?page=$1&filter=$2 [QSA]
Many thanks
Upvotes: 1
Views: 101
Reputation: 786349
Change your force www
rule to this:
# Force www.
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule ^ http://www.%{HTTP_HOST}%{REQUEST_URI} [NE,R=301,L]
This will force www
to all of your domains. Make sure to test it after clearing your browser cache.
Upvotes: 1