1

I'm trying to sort out a redirect for my site from www to the "naked" domain.

My redirect currently does this:

www.example.com -> example.com
www.example.com/test.html -> example.com

Is this the correct behaviour? My only concern is, is it correct for it to use a 301 redirect to bring it back to the front-page no matter where it has come from?

I would think this would be the expected behaviour would be this instead:

www.example.com -> example.com
www.example.com/test.html -> example.com/test.html

I have implemented this redirect on the nginx web server.

  • 4
    Your second example. – closetnoc May 10 '15 at 18:24
3

The HTTP status code 301 is named "Moved Permanently":

The requested resource has been assigned a new permanent URI and any future references to this resource SHOULD use one of the returned URIs.

So the resource (i.e., your document) would stay the same, it just gets a new URI.

As your front page http://example.com/ is (usually) not the same resource as a page like http://www.example.com/test.html, it would not be correct to use the status code 301.

Or in other words: Yes, include the path.

| improve this answer | |
2

I would think this would be the expected behaviour would be this instead: www.example.com -> example.com www.example.com/test.html -> example.com/test.html

That's a good idea. Just map the last parts of the URL (particularly folder and file) from the old domain to the new domain. You can easily use mod-rewrite if you have apache. Just make an .htaccess file in the folder where the www.example.com document root is and add the following:

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

That way, everything will be nicely redirected over from the www version to the non-www version.

My only concern is, is it correct for it to use a 301 redirect to bring it back to the front-page no matter where it has come from?

The only time you should redirect a URL to the homepage is if that URL was the homepage in the past. For example, if your previous homepage was at the url http://www.example.com/homepage.html and the new homepage is just example.com, then you can use a 301 return code on http://www.example.com/homepage.html and redirect it to example.com.

Make sure you test everything as you go because you don't want to end up with too many redirects or you'll see a message in your browser something similar to "the document has moved here" and when you click on "here", the same message reappears.

| improve this answer | |
  • Note that code you provided is for Apache .htaccess but that the question was asked using the nginx tag. Max Lazar's answer has appropiate code for nginx – Stephen Ostermiller May 12 '15 at 20:44
1

I'm always setup 301 redirect from www.example.com domain to example.com. This code helps you:

server
{
    server_name www.example.com;
    return      301 $scheme://example.com$request_uri;
}
| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.