Appending www to url

RewriteEngine on

RewriteCond %{HTTP_HOST} ^example.net$ [NC]

RewriteRule ^(.*)$ http://www.example.net/$1 [R=301,L]

the above code works for http://www.example.net but only http://example.net is giving "page not found"

I simply want www appended automatically. Please help.

7 Replies

Try this:

RewriteCond %{HTTP_HOST} !^www.domain.com$

RewriteRule ^(.*)$ http://www.domain.com/$1 [R=301]

It seems the problem still persists, my browser cache has been fooling me.

It turned out to be more of a redirection problem than rewrite rules so here is the solution.

In linode dns

example.com

In Virtual host

servername http://www.example.com

serveralias example.com

In the website directory .htaccess file is supposed to contain;

RewriteEngine on

RewriteCond %{HTTP_HOST} ^example.com$ [NC]

RewriteRule ^(.*)$ http://www.example.com/$1 [R=301,L]

Is the goal to make http://www.example.net/ == http://example.net/ ?

If so then you don't need a redirect rule just make sure your DNS is setup right.

The goal, most likely, is to have exactly one of 'www.example.com' or 'example.com' be the canonical hostname for the site by redirecting the "wrong" one to the "correct" one. This is generally considered a good thing to do, both for aesthetics and for making things easier on web crawlers. (It's real SEO, in other words.)

@jebblue,

yes the idea is to have a website which is accessible by http://www.example.net to be reached via example.net. @hoopycat is right. we want to achieve it for better seo.

http://www.example.net (currently the link is working fine)

&

example.net (currently the link gives page cannot be displayed)

to reach

http://www.example.net

Can't make it more clearer than this :(

Check if you can you ping both www and just the domain name and get back a response. Check if nslookup or dig see both names.

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