Dheeraj
Dheeraj

Reputation: 323

Redirect non-www to www not working

I set up a virtual server using virtualmin, it didn't create the .htaccess file so I created one in the public_html folder and put the following code

RewriteEngine On
RewriteCond % ^megahotserved.com [NC]
RewriteRule ^(.*)$ http://www.megahotserved.com/$1 [L,R=301]

restarted apache and no effect and then tried

<VirtualHost *:80>
    ServerName megahotserved.com
    Redirect permanent / http://www.megahotserved.com/
</VirtualHost>

in the httpd.conf file, when I restarted apache firefox came up with an error

The page isn't redirecting properly.

Firefox has detected that the server is redirecting the request for this address in a way that will never complete.

What should I do ?

Upvotes: 1

Views: 5425

Answers (3)

whof
whof

Reputation: 11

your code is correct, you just need to follow the next paragraph which I quoted from http://httpd.apache.org/docs/current/mod/mod_rewrite.html

By default, mod_rewrite configuration settings from the main server context are not inherited by virtual hosts. To make the main server settings apply to virtual hosts, you must place the following directives in each section:

RewriteEngine On
RewriteOptions Inherit

Upvotes: 1

Shay Rojansky
Shay Rojansky

Reputation: 16742

Agree with the above, and a small addition: it is better to redirect non-www to www rather than rewrite, otherwise you have two complete views ("copies") of your entire website; each page has two URLs, instead of one canonical one. This can be bad for search engines and other things.

Upvotes: 0

hobbs
hobbs

Reputation: 240649

Seems like you don't have a VirtualHost that properly matches the www. address, so requests for http://www.megahotserved.com/ are hitting the very same vhost and getting into a circular redirect. So the redirect is working fine; you just have a different part of the server config to fix.

Upvotes: 0

Related Questions