Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Westi on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

rewriting an url without www to a url with www

Status
Not open for further replies.

glimbeek

Programmer
Nov 30, 2009
83
NL
I want to rewrite to
I have several questions:

I tried the following already:

RewriteCond %{HTTP_HOST} ^domain.com [NC]
RewriteRule ^(.*)$ [L,R=301]

and

RewriteCond %{HTTP_HOST} !^ [NC]
RewriteCond %{HTTP_HOST} !^$
RewriteRule ^/?(.*) [L,R,NE]

What's the difference and is one "better" then the other?

Next thing I want to do is the following:

In a folder named /blog/ I installed Joomla! 1.5.15 so I have items>.
Joomla! uses its own .htaccess file so I will need to put a rewrite rule in that .htaccess as well to acomplish the same thing: rewrite none to URL's with www.

To accomplish this, I used the same rules as I gave above but that doesn't work.

This first rewriterule rewrites me to the and not to the article in the blog.

The second rule shows to be more promising but isn't fully what I want. The second rule rewrites the url to:
items>
and not:
items>
what it should be.

Now I can fix this by doing either the following 2 options:

RewriteCond %{HTTP_HOST} !^ [NC]
RewriteCond %{HTTP_HOST} !^$
RewriteRule ^/?(.*) [L,R,NE]

RewriteCond %{HTTP_HOST} !^ [NC]
RewriteCond %{HTTP_HOST} !^$
RewriteRule ^/?(.*) [L,R,NE]

Again the question, which one is better or am I approaching this completely the wrong way?

Thanks in advance for any help provided
 
Instead of using rewrite rules, wouldn't it be far easier to simply have the server A record as domain.com and have as a CNAME?

In other words, instead of having Apaache worry about converting domain.com to let this be handled by DNS where it is a simple alias entry.
 
That's sound actually like a good idea, but...
Does this work just as well? Is it full proof? Should I worry about anything special when I do this?
 
That is how I have mine setup.

A canonical name is just an alias name, in other words, an alternate name to refer to the same location. If you are running your own DNS this is pretty easy to setup. If you are using a dynamic re-direct service, such as dyndns, you will need to add the alias name there.
 
Checked it and I'm not a 100% it will work the way I want it to work. I'd rather use .htaccess.

So the questions remains,

why does:
RewriteCond %{HTTP_HOST} ^domain.com [NC]
RewriteRule ^(.*)$ [L,R=301]

or

RewriteCond %{HTTP_HOST} ^domain\.com$
RewriteRule ^(.*) [R=301]

not work in when I type: in my browser?

note: /blog/ is a folder on the server with it's own .htaccess file (It's a Joomla installation).
 
In addition to the above post:

Because the /blog/ folder has its own .htaccess file, I need to setup that file in such a way that it rewrites as well, right? Tell me if I'm doing/understanding this the wrong way.

To do this, I tried using the same code:

RewriteCond %{HTTP_HOST} ^mydomain\.com$ [NC]
RewriteRule ^(.*)$ [R=301]

Which results in the url being rewritten with without /blog/ in the url, so I end up with instead of
To fix this I'm using:
RewriteCond %{HTTP_HOST} ^casinogokkenn\.net$ [NC]
RewriteRule ^(.*)$ [R=301,L]

That works, but it confuses me. Why do I need to add /blog/ in the rewriterule line, shoulnd't it just pick up on it?
 
I am going to hazard a semi-educated guess that the problem has to do with the regex in your rewrite rule. The rule has the following ^(.*)$ and towards the end has a $1.

What this does is that it replaces the entire URL, including the /xxx portion with What I think you want to do is replace the text before the /. To do this you need to modify your regex. Try something like ^(.*)//?, which will match a string preceding a slash. Note that / is a special character and needs to be escaped, hence two of them.

Also note that in regex terms, the () isolates a portion of the string for future identification by variable. In this instance, it extracts the domain and makes it available by the $1 variable.

If the above doesn't work, you should be able to match a second string following the / and substitute it into your rewrite rule using $2. Just be sure to put () around it.

P.S. - Why do you think the DNS solution won't work?
One other option that I think Apache supports is to have be an alias name for domain.com.
 
You may be right, I am not sure. Lets think think things through, just to be thorough and whether the alias is a DNS or Apache alias. If it is an alias in the DNS (CNAME) a host lookup by name for either or domain will return the same IP, but a reverse lookup by IP will return the (one) name entry that is associated with the address. While some DNS servers like Bind will accept multiple names per IP, it is a mostly useless concept. Consequently, in either case, either domain name will resolve to your servers IP address.

At the Apache level, the server can have multiple virtual hosts and which host is requested is identified as part of protocol handshaking. In this case, when an alias is defined in addition to the server name, Apache will serve up that (one) page for a request to either.

So, unless you have different servers (IP address) or host files for a and domain, I don't see how they don't resolve to the same thing. Meaning that the same HTML is delivered for either request and a human or a bot gets the same information. Now, it is true that search engine bots don't see the page the same way a human does. They can read the 'invisible' and meta tags and they don't get the images. This means that it is necessary to write your pages to be bot friendly if so desired.

Given that you are expressing doubt and reservations, your best option would be to continue to read and research the subject and come to your own conclusion as to what you are comfortable with.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top