Can someone please provide me a little assistance? I’m trying to do a 301 redirect so that when a user enters http://test.com they will be redirected to http://www.test.com . Below are my current httpd configurations. I’m also running two websites on one IP. I’ve tried a lot of things and would love to avoid having to make modifications to the .htaccess file. Current Configuration NameVirtualHost *:80 <VirtualHost *:80> ServerName www.test.com ServerAlias www.test.com test.com DocumentRoot /srv/www/htdocs/test CustomLog /var/log/apache2/test/access_log combined ErrorLog /var/log/apache2/test/error_log </VirtualHost> <VirtualHost *:80> ServerName www.test2.com ServerAlias www.test2.com test2.com DocumentRoot /srv/www/htdocs CustomLog /var/log/apache2/test2/access_log combined ErrorLog /var/log/apache2/test2/error_log </VirtualHost> I’ve also tried the following which didn’t seem to work. NameVirtualHost *:80 <VirtualHost xx.xx.xx.xx> ServerName test.com RewriteEngine on RewriteRule ^/(.*) http://www.test.com/$1 [L,R=301] DocumentRoot /srv/www/htdocs/test CustomLog /var/log/apache2/test/access_log combined ErrorLog /var/log/apache2/test/error_log </VirtualHost> <VirtualHost *:80> ServerName test2.com RewriteEngine on RewriteRule ^/(.*) http://www.test2.com/$1 [L,R=301] DocumentRoot /srv/www/htdocs CustomLog /var/log/apache2/test2/access_log combined ErrorLog /var/log/apache2/test2/error_log </VirtualHost>
i would suggest both with a / and without / (and also putting the $ on the end) RewriteRule ^/(.*)$ http://www.test.com/$1 [L,R=301] RewriteRule ^(.*)$ http://www.test.com/$1 [L,R=301] Code (markup):
The main trick here is to use a RewriteCond just before the RewriteRule like so: RewriteCond %{HTTP_HOST} !^www.test.com$ RewriteRule ^/(.*) http://www.test.com/$1 [R=301,NC,L] Code (markup): The critical part is the exclamation mark. It means "NOT"... so anything that is not www.test.com matches the RewriteCond and will invoke the RewriteRule on the next line. Anything that is exactly www.test.com doesn't match the RwriteCond and will skip the following rewrite rule. [R=301] obviously produces a 301 redirect rather than a 302 which is the default for [R] [NC] matches the regex with NoCase. i.e. Case insensitive matching. [L] means that this is the last rule and no more will even be attempted for this request.