Forum Moderators: phranque

Message Too Old, No Replies

Redirecting or rewriting from www.domain.com to domain.com

Do I need to redirect or rewrite

         

rbacal

8:21 pm on Aug 19, 2005 (gmt 0)



I've seen htaccess code to redirect/rewrite from domain.com to www.domain.com but I want to do it the other direction, and I'm syntax impaired. Anyone have the code hand?

Also is mod-rewrite required to do this or is it possible to simply do a permanent one line redirect instead?

Thanks.

jd01

6:19 am on Aug 20, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I believe this can be done with either, although every time I comment on a Redirect I get myself in trouble, because it has not been worth my time to learn...

The mod_rewrite version is possible two ways:

# Necessary to both sets below
RewriteEngine ON

# Option 1 - Positive Check:
RewriteCond %{HTTP_HOST} ^www\.yoursite\.com [NC]
RewriteRule (.*) http://yoursite.com/$1 [R=301,L]

The above ruleset checks to see if the host is the www. version of your domain, and if so, rewrites that to the non-www. version.

# Option 2 - Negative Check: (My Preference)
RewriteCond %{HTTP_HOST} .
RewriteCond %{HTTP_HOST} !^yoursite\.com [NC]
RewriteRule (.*) http://yoursite.com/$1 [R=301,L]

The above ruleset checks to see if the HTTP_HOST is set (HTTP 1.0 clients do not send a HOST header, so they would end in a loop if there was no check). Then if there is a HOST, the second condition checks to see if the HOST is *not* yoursite.com - if it is not, the rule is applied and the URL is rewritten.

I like the negative check for a couple of reasons:
1. If someone mistypes a subdomain EG wwww.yoursite.com they will be correctly redirected

2. In some cases you will break framing scripts from placing your site in a frame set.

Hope this helps.

Justin

bigearz

11:41 pm on Aug 21, 2005 (gmt 0)

10+ Year Member




At present, if people don't type in the www. part of my site's web address, they aren't given the site but instead diverted to Google, which suggests they go to my site.

I'm hoping there's something I can write into my .htaccess file to let people go straight to the site, as they seem to be able to do on just about every other site.

As you can likely tell, I'm not a programmer! Any help appreciated.

jd01

3:52 am on Aug 22, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Either of the above will do this. You may need to precede the RewriteEngine line with one or both of the following:

AllowOverride FileInfo
Options +FollowSymLinks

Again, I like the negative ruleset better.

Have you tried either?

If not, make sure you create the file (or edit if it exists) in a *plain* text editor, and you upload in ASCII mode.

Justin

bigearz

11:05 pm on Aug 26, 2005 (gmt 0)

10+ Year Member



I haven't tried either and confess I'm struggling with my limited understanding of these things to see which line I should put into my .htaccess file.

We're on a shared server so I don't have access to any other top level server administration files beyond the .htaccess - does this matter?

I've got redirects in my .htaccess for error 404 pages and the like, so that much I have worked out - advice beyond that would be very welcome.

jd01

12:26 am on Aug 27, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Try This:

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

If that does not work, please let us know exactly what happens (or does not happen) and any error message you receive.

Justin

bigearz

6:35 pm on Aug 29, 2005 (gmt 0)

10+ Year Member



Well, the site can only be seen using:

www.domain.com

It cannot be seen using:

domain.com

And it doesn't make any difference whichever version of these mod_rewrite commands I use.

Was thinking it was a DNS issue that could be solved with CNAME, but I haven't the slightest idea how to sort that.

Basically, I want people trying to access the site without using the www. in front of the domain name to be able to get to it, which presently they can't.

The host is Active24 here in the UK.

jdMorgan

9:09 pm on Aug 29, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



You don't have to use a CNAME, just modify your zone file so that the A Record domain name starts with "*." instead of "www." Or, you could duplicate the existing A record, but omit the leading "www."

If it still doesn't work after doing that, ask your host to enable wildcard subdomains on your acount.

Jim

bigearz

9:50 am on Aug 30, 2005 (gmt 0)

10+ Year Member



What's a zone file, what's an A file and how do I access them - and then what do I do with them?

We have a shared server, so I can only change things like the .htaccess file - I don't have administrative access to the server set-up.

It's really maddening - just about every site seems to be able to show itself with or without a www. except ours!

jdMorgan

1:28 pm on Aug 30, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



You should contact your host about changing your zone file and the server configuration so that your site resolves under both the domain name and the www subdomain of that domain name.

For basic questions like "What's a zone file," I'd also suggest trying a few searches.

Jim