apache2 - redirect www.domain.com to domain.com

I've been trying to redirect www.domain.com to domain.com for 4 hours and I'm getting no where. I have no idea how to do this and I've tried following tutorials but nothing is working. I sign into my site on domain.com, but when I go to www.domain.com I'm at my site but the session is different because I'm not logged in at www.domain.com, only at domain.com. How can I do the redirect?

12 Replies

I am not sure what you are trying to do. Do you have a cname in your nameserver for www.domain.com? If you do that apache should handle either request.

fb

mod_rewrite module needs to be enabled for this to work.

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> 

Sounds like you need to use Apache rewrite to rewrite requests for http://domain.com to http://www.domain.com

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… http://httpd.apache.org/docs/1.3/misc/rewriteguide.html (scroll down a little to "Canonical Hostnames").

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?

Just do this

 ServerName example.net
 ServerAlias www.example.net

@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 :D

@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.

@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 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 to www.domain.com and don't alias domain.com

Then only www.domain.com will work.

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 to www.domain.com and don't alias domain.com

Then 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 www.example.com. Making only one or the other work is just going to confuse and frustrate your visitors (or worse, make them think the site is down).

So, the only question is: do you serve the same content from both example.com and www.example.com or do you redirect one to the other?

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.

@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 www.example.com and then visit example.com where you aren't logged in. Which is the original issue….

Reply

Please enter an answer
Tips:

You can mention users to notify them: @username

You can use Markdown to format your question. For more examples see the Markdown Cheatsheet.

> I’m a blockquote.

I’m a blockquote.

[I'm a link] (https://www.google.com)

I'm a link

**I am bold** I am bold

*I am italicized* I am italicized

Community Code of Conduct