apache2 - redirect www.domain.com to domain.com
12 Replies
fb
You can set up a rewrite in your virtual host configuration, or in a .htaccess file placed in your website root directory (make sure AllowOverride is set to All to use rewrite directives in .htaccess)
<ifmodule mod_rewrite.c="">RewriteEngine on
RewriteCond %{HTTP_HOST} ^www\.domain\.com$ [NC]
RewriteRule ^(.*)$ http://domain.com/$1 [L,R=301]</ifmodule>
There are various ways of formulating the rewrite rules. Try googling for "apache rewrite force www" or something like that that. You'll find lots of examples that vary slightly.
A basic example is on this Apache 1.3 docs page…
@sneaks:
Which would be better? A rewrite or a redirect? I got it working with a redirect finally, but it seems to me a redirect would be faster and take up less resources since Rewriting has parsing, right?
Ultimately, you want to do a 301 redirect.
To accomplish that, you can use apache's modrewrite (see melon's post) or you can use modalias and the redirect command. It's really up to you. A lot of people end up using modrewrite because they're using it for other things already and it's a bit more flexible than modalias.
@kangaby:
Just do this
ServerName example.net ServerAlias www.example.net
was just wondering why all were suggesting redirecting or rewriting instead, and about to post this before seeing your comment
@Vito Botta:
was just wondering why all were suggesting redirecting or rewriting instead, and about to post this before seeing your comment
Because the original poster specifically asked for how to do redirection.
He's already got this up and running, his problem was that he wanted people trying to go to "domain.com" to end up at "
@Xan:
@Vito Botta:was just wondering why all were suggesting redirecting or rewriting instead, and about to post this before seeing your comment
Because the original poster specifically asked for how to do redirection.
He's already got this up and running, his problem was that he wanted people trying to go to "domain.com" to end up at "
www.domain.com ", not just seeing the same stuff, but with that address in the URL bar.
Oops, you're right - he was indeed talking about redirection
@Xan:
he wanted people trying to go to "domain.com" to end up at "
www.domain.com ", not just seeing the same stuff, but with that address in the URL bar.
I guess I don't really understand the point. If
If you really want them to only use
Then only
Is there some special secret setup I'm missing here?
@kangaby:
@Xan:he wanted people trying to go to "domain.com" to end up at "
www.domain.com ", not just seeing the same stuff, but with that address in the URL bar.I guess I don't really understand the point. If
www.domain.com is the same content as domain.com, then why do a redirection at all.If you really want them to only use
www.domain.com then set the server name towww.domain.com and don't alias domain.comThen only
www.domain.com will work.Is there some special secret setup I'm missing here?
Users expect websites to be available at both example.com and
So, the only question is: do you serve the same content from both example.com and
Redirecting one to the other is usually the better solution. Users will always be presented with your "preferred" url. Also, if people link to your site using both example.com and
@btmorex:
Redirecting one to the other is usually the better solution. Users will always be presented with your "preferred" url. Also, if people link to your site using both example.com and
www.example.com , using a 301 redirect tells google that all that pagerank flow should be consolidated into one page. If you're serving content from both, then google is going to have two pages in its index and they're both probably not going to perform very well in search results because the links are split between them.
It also resolves confusion with cookies and sessions. You might be logged in to