Forum Moderators: phranque

Message Too Old, No Replies

redirect non-www to www with .htaccess

questions from a non-techie webmaster

         

fearlessrick

2:00 pm on May 25, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Some of us in the Google news forum have been bouncing this around and somebody suggested I ask here. I think I have the code right, but now my question is: what directory is my root. I know, I should know that, but read on.

I have the following structure in my ftp setup: / -- home -- mysitename -- www -- subdirectories.

OK, now I'm really confused. I am on a shared server. In directory / are all kinds of files I don't mess with like bash_history, bin, boot, etc. In directory "home" are everybody's sitenamed folders, including mine. In mysitename directory are more files I don't mess with, bash_history, .pinerc, .redirect (I have to look at that one), etc. plus my www folder. I now have .htaccess file in both the mysitename and www directories. Somebody please help me out here?

I am assuming that the www is what most of you are calling "root". Please correct me if I'm wrong.

Below is the code I'm using on a Unix server, which I believe is Apache.

-------------

Options +FollowSymLinks
AllowOverride FileInfo
RewriteEngine on
RewriteCond %{HTTP_HOST} ^example\.com
RewriteRule (.*) [example.com...] [R=301,L]

------------

fearlessrick

2:14 pm on May 25, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



I think I figured it out, since putting .htaccess in my www directory was causing "500 Internal Server Error" when I tried [mysite.com....]

Put .htaccess in "mysite" directory seems to do the trick. So now I know which directory is my root. I ffeel like a dunce.

Any help, thanks in advance.

jd01

6:18 pm on May 25, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Hi fearlessrick,

Your code looks good. The Options and AllowOverride are not always necessary, but if you are not crashing and the rewrites work you should be good to go.

I think you came from the G update thread... This is where I normally post, but haven't had too much time to spend here for the last week or two. (Sorry Jim).

The only change I would think about making is changing the condition to a negative, like this:

RewriteCond %{HTTP_HOST} !^www\.example\.com

There are a couple of reasons...

1. This helps break your site out of some types of frames (not all), because if the host making the request is not yours, the request for the page is re-written to your site.

2. The reason I really like it... If someone types in the wrong sub-domain, they will still end up at your site. EG wwww.yoursite.com (or even w4.yoursite.com) is a request for your domain with a wwww (or w4) sub-domain. When this request is processed if the requested sub-domain does not exist, the request is redirected to your site, and is then rewritten to the correct www version. (A request for an actual sub-domain will still be processed as normal, as long as it has a separate 'parent' or 'root' directory.)

Hope this helps.

Justin

jdMorgan

6:25 pm on May 25, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



If you do use a negative pattern, always test for a blank HTTP_HOST. Otherwise, true HTTP/1.0 clients will get an 'infinite' redirect loop:

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

Jim

Simon_Says

2:31 pm on May 29, 2005 (gmt 0)

10+ Year Member



Hi

I've managed to use

RewriteEngine on
RewriteCond %{HTTP_HOST} ^example\.com
RewriteRule (.*) http://www.example.com/$1 [R=301,L]

successfully to direct all http://example.com to http://www.example.com Headers check out as '301 moved permanently'.

I've put the code into htaccess in root. Other folders have their own htaccess files that parse static urls to various scripts / dynamic urls.

However, after setting up the 301 redirect, the header checker shows the permanent moved loction as the script and not the desired static url.

Is there a simple way that I can add something to htaccess in root to overcome the problem for all folders? Or is there something I need to do with each folder's htaccess file?

Thanks in advance!

Simon_Says

2:46 pm on May 29, 2005 (gmt 0)

10+ Year Member



Ok

Though I'd fixed it with 'Rewriteoptions inherit' in the folder htaccess but doesn't seem to work.

:(

jdMorgan

3:24 pm on May 29, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



> the header checker shows the permanent moved loction as the script and not the desired static url.

What script? How was the static URL mapped to this script before you installed the domain redirect?

I suspect you may have a 301 redirect from the static URL to the 'script' -- when an internal rewrite should have been used. Please post more detail.

Jim

Simon_Says

8:30 am on May 31, 2005 (gmt 0)

10+ Year Member



Thanks for getting back jd

The folders are using

RewriteEngine on
Options +FollowSymLinks
RewriteCond %{REQUEST_FILENAME}!-f
RewriteRule ^([^/]+)$ $1/
RewriteCond %{REQUEST_FILENAME}!-d
RewriteRule ^([^/]+)/$ ../scriptfolder/scriptname.php?qry=$1 [L]

User goes to [domain.com...] and gets shown the content of scriptname.php with the filename posted as a variable qry= The .html gets stripped off in the php file.

[domain.com...] remains in the address bar. Header check shows 200 OK.

Previously [domain.com...] showed exactly the same (making me worry about duplicate content issues - mostly discussed in Google News Forum).

Have changed the htaccess in folder1 to

RewriteEngine on
Options +FollowSymLinks
RewriteCond %{HTTP_HOST} ^domain\.co\.uk
RewriteRule (.*) [domain.co.uk...] [R=301,L]
RewriteCond %{REQUEST_FILENAME}!-f
RewriteRule ^([^/]+)$ $1/
RewriteCond %{REQUEST_FILENAME}!-d
RewriteRule ^([^/]+)/$ ../scriptfolder/scriptname.php?qry=$1 [L]

This now includes a 301 redirect for [domain.co.uk...] to [domain.co.uk...] Headers show 301 Moved Permanently.

However, I had to add

RewriteCond %{HTTP_HOST} ^domain\.co\.uk
RewriteRule (.*) [domain.co.uk...] [R=301,L]

to a lot of folder htaccess files as a work around. I originally added it to root htaccess. I wondered if there was a directive to force the root htaccess 301 redirect on all folders (I assumed that this would be done automatically).

Although I've worked around it (successfully I think), it would be good to know for the future.

Hope this makes sense!. I'm a bit of a newbie to htaccess and mod rewrite. My files seem to work and show the correct headers but may not be ideal. Please feel free to put me right! What do you think?

Thanks again!

jdMorgan

3:52 pm on May 31, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Adding the RewriteOptions Inherit directive should have worked "automatically".

Here's another possibility: [webmasterworld.com...] msg#2.

It's also possible that some other server config problem exists that is interfering with your rules.

And be sure to flush your browser cache before testing *any* change to your code.

A comment:


RewriteCond %{REQUEST_FILENAME}!-f
RewriteRule ^([^/]+)$ $1/
RewriteCond %{REQUEST_FILENAME}!-d
RewriteRule ^([^/]+)/$ ../scriptfolder/scriptname.php?qry=$1 [L]

This method can be very inefficient. The reason is that checking -f (file exists) and -d (directory exists) involves a request to your server's filesystem to search for the filename for every request that matches the pattern in the rule.

Your patterns are moderately exclusive, but if there is any way to make the rule patterns more specific, or to add additional exclusions by adding more RewriteConds above the -f/-d checks, then do so. A good way to bring a server down is to use ".*" as the pattern for -d/-f checks, forcing a check on *every* request. If you can make the rule pattern any more exclusive, then do so.

For example, you may wish to exclude image files, included JavaScript, and CSS files from being checked by adding

RewriteCond %{REQUEST_URI} !\.(jpe?g¦gif¦png¦js¦css)$ [NC]

above the file/directory exists checks.

I should note that a similar problem exists with doing %{REMOTE_HOST} checks; These force the server to issue a reverse-DNS lookup request, with the client's request hanging until the reverse-DNS response is received. When using either file/directory exists checks or REMOTE_HOST lookups, it's important to limit them to cases where you know they are needed.

Jim

brokenbricks

3:57 pm on Jun 2, 2005 (gmt 0)

10+ Year Member



What if you have:

www.site.com/directory/

www.site.com has nothing on it, only the /directory

but you want

site.com/directory/ to go to www.site.com/directory/

brokenbricks

1:39 pm on Jun 13, 2005 (gmt 0)

10+ Year Member



bump

jdMorgan

3:23 pm on Jun 13, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Hey, maybe this is it:

You stated above that you added:


RewriteCond %{HTTP_HOST} ^domain\.co\.uk
RewriteRule (.*) http://www.domain.co.uk[b]/folder1[/b]/$1 [R=301,L]

thereby mixing a domain redirect with a folder rewrite.

What happens if you handle them separately (which I'd recommend) and just fix the domain and let your other code add the subfolder after the redirect is performed (as it always has)?


RewriteCond %{HTTP_HOST} ^domain\.co\.uk
RewriteRule (.*) http://www.domain.co.uk[b]/$1[/b] [R=301,L]

Jim

stu2

8:22 pm on Jun 13, 2005 (gmt 0)

10+ Year Member



I have a few questions about this important feature...

1) How do you know if it's working? Is it that when you enter example.com into your browser it resolves as http://www.example.com?

2) If I put the line "Rewrite Engine on" in my .htaccess file, I get an internal server error. Why?

3) When I tried to discuss this with my webhost, what they did was place an index.html (I use php) file in my root directory which did an meta http-equiv="REFRESH" to my http://www.example.com/index.php homepage. Is this the same thing as a 301 redirect in .htaccess?

jd01

8:53 pm on Jun 13, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



1) How do you know if it's working? Is it that when you enter example.com into your browser it resolves as http://www.example.com?

YES

2) If I put the line "Rewrite Engine on" in my .htaccess file, I get an internal server error. Why?

Your host probably does not have the module loaded or enabled.

3) When I tried to discuss this with my webhost, what they did was place an index.html (I use php) file in my root directory which did an meta http-equiv="REFRESH" to my http://www.example.com/index.php homepage. Is this the same thing as a 301 redirect in .htaccess?

Unfortunately, NO. This is a different type of redirect, which is browser side, and does not send the correct headers (from what you described) for a proper redirect to take place. A meta refresh is actually one of the techniques used to 'hijack' content.

A true redirect will send a header code of 302 (temporary move), or 301 (permanent move) which tells clients (browsers, and SE spiders) 'the page is not here any more, restart the request over here...'

My guess is you are on a 'free' hosting plan and do not have authorization to use mod_rewrite. Though the solution your host provided will work, it is *far* from ideal, and in some circumstances can be Search Engine death. I, personally, would be very careful about using this type of redirect, and would actually change hosts before I allowed this on my site.

With the current situation involving redirects and refreshes, I am extremely sceptical about using anything that does not fully inform a client (browser or SE) through the use of a proper HTTP header, of exactly what is happening and why.

I am sure Jim or someone else can elaborate on the differences between the two better than I can, so you might wait for a more thorough reply before making any final decision.

Hope this helps.

Justin

stu2

6:01 am on Jun 14, 2005 (gmt 0)

10+ Year Member



OK, I finally got my 301 redirect in .htaccess working. The internal server error was due to a syntatical error :( No need to hijack my own pages with a REFRESH :) It isn't a free host but you know how hard it can be to get anything sensible out of support. Makes me very happy I found this forums just a couple of days ago.

g1smd

2:21 pm on Jun 15, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month




Can we make this thread, or one that covers this subect in equal depth, a sticky thread in this forum?

Qur1uS

11:46 pm on Jun 15, 2005 (gmt 0)

10+ Year Member



Need some help friends...I read the above responses and I am really confused, so I am hoping that someone can simply give me the answer to this problem that I have...

I have several links on search engines and such pointing to www.somesite.co.uk/~directory/alltypesofpages.htm

I own www.somesite.co.uk/ and have it parked on my

www.somesite.com/ webserver....with the content that those searh engines are looking for but they are

www.somesite.com/alltypesofpages.htm in that location.... so in the root of different domain name.

Is it possible to redirect the visitors to where the content is now using the methods described in this thread? If so could someone please make a suggestion...

www.somesite.co.uk/~directory/alltypesofpages.htm to
www.somesite.com/alltypesofpages.htm

so in effect....

any link that starts with
www.somesite.co.uk/~directory/

should point to
www.somesite.com/

Cheers...

Qur1uS

4:09 pm on Jun 16, 2005 (gmt 0)

10+ Year Member



Having been reading the Apache Rewriting Guide and other suggested materials...I'm still lost but think that my question can be simplified...any help would be appreciated.

Incoming Links to point from

www.example.com/~example/directory/file.html

to

www.example.com/directory/file.html [perm redirect]

Thanks.

jdMorgan

5:45 pm on Jun 16, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Qur1uS,

We will be happy to help you write your own code, but we can't write code on demand here -- Doing so results in far too many requests for the few contributors to handle. Our forum charter [webmasterworld.com] explains this, and contains links to basic documentation for mod_rewrite and regular expressions to get you started.

Also, a search of the WebmasterWorld archives for existing redirection threads [google.com] may be helpful.

Note that you will need to place the code in either the www.example.com/ or www.example.com/~example/ directory in order for it to work.

Jim

Qur1uS

6:53 pm on Jun 16, 2005 (gmt 0)

10+ Year Member



noted...
thanks

Qur1uS

7:22 pm on Jun 17, 2005 (gmt 0)

10+ Year Member



OK...I think I have a part of it...

RewriteEngine on
RewriteCond %{HTTP_HOST} ^(www\.)?example\.co\.uk
RewriteRule (.*) http://www.example.com/$1 [R=301,L]

If someone visits
www.example.co.uk/anydirectory/anyfile.html

they will get redirected to
www.example.com/anydirectory/anyfile.html

with a 301 Perm Redirect In the header Information.

Now...that's great

Is this how I would also fix my other problem?

RewriteEngine on
RewriteCond %{HTTP_HOST} ^(www\.)?example\.co\.uk
RewriteCond %{HTTP_HOST} ^(www\.)?example\.co\.uk/~something
RewriteRule (.*) http://www.example.com/$1 [R=301,L]

jdMorgan

12:09 am on Jun 18, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



The HTTP_HOST variable will contain only the hostname (domain name) and possibly a port number, but not anything else following that. So you can't include /~something in the pattern to match against it. That is part of the requested URL-path, and not part of the domain.

I'm not sure how your server(s) is/are set up, but just correcting what you have above would yield:


RewriteEngine on
RewriteCond %{HTTP_HOST} ^(www\.)?example\.co\.uk
RewriteRule ^~something/(.*)$ http://www.example.com/$1 [R=301,L]

Jim

Qur1uS

10:57 pm on Jun 20, 2005 (gmt 0)

10+ Year Member



Thanks for the above suggestion...it gave me a Server error 500.

I am getting alot of 404s on my site because of old links to my other URLs.

I have 2 directories that used to be on my old site...and all the old links are pointing to them :(

I'm going to see if my tech support can help me out with this problem....

Thank You for all your help....

I may be back, you never know what tech support can do.

Q

Qur1uS

11:44 pm on Jun 20, 2005 (gmt 0)

10+ Year Member



lol

looks like i gotta learn this myself :)
they had no clue.

Chico_Loco

12:00 am on Jun 21, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



If it gave you a 500 error, then take a look at your error_log. I can't remember if mod_rewrite writes to that file, but it can't hurt to look.

1. You don't need to specify "RewriteEngine on" twice. Some module turn off when posed with a double statement, not sure about mod_rewrite.

2. An absolute must to read is: [httpd.apache.org...]

According to that document, the following should correct issues with a tilde in the request_uri:

RewriteRule ^/~something/(.+) [newserver...] [R,L]

I'm not great with mod_rewrite, and some of the advice above is extremely good, and probably more accurate, but this is something else to try.

Qur1uS

1:43 am on Jun 21, 2005 (gmt 0)

10+ Year Member



Hi Chico_Loco,

That looked great. I gave it a try and nothing happened, but I think it's probaby of something else....:)

Here is what I have in my .htaccess file, including your suggested addition...i've added comments after each line to explain what I think each line does.

XBitHack on /* this is for my adsense include */
RewriteEngine on /* this turns on the RewriteEngine :) */
RewriteCond %{HTTP_HOST} ^(www\.)?domain\.co\.uk /* if the URL being searched for matches www.domain.co.uk or domain.co.uk */
RewriteRule (.*) [domain.com...] [R=301,L] /* rewrite that URL to www.domain.com/whatever */
RewriteRule ^/~something/(.+) [domain.com...] [R=301,L] /* people visiting from .co.uk/~something/ or .com/~something/ will be redirected to www.domain.com/whatever */
RewriteRule ^/~somethingelse/(.+) [domain.com...] [R=301,L] /* people visiting from .co.uk/~somethingelse/ or .com/~somethingelse/ will be redirected to www.domain.com/whatever */

So I have 2 domains on 1 server.
The parked domain is the .co.uk
And I have links pointing to the .co.uk

The above code only redirects .co.uk links to .com

Should I be adding another RewriteCond?

jd01

2:16 am on Jun 21, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



In the .htaccess file Apache strips the preceding /, so the examples as posted will not qualify for a match.

IWO the correction is:
RewriteRule ^~something/(.+) http://www.domain.com/$1 [R=301,L]

Justin

Qur1uS

2:32 am on Jun 21, 2005 (gmt 0)

10+ Year Member



AHAHAHAHAHAH..YES...Thanks so much

It worked :)

Q

stu2

6:14 am on Jun 24, 2005 (gmt 0)

10+ Year Member



How to redirect non-www to www for internal pages?

I'm using the following to redirect my home page:-

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

Should I modify the above somehow or include more RewriteCond/RewriteRules for the internal pages? What should that cond/rule look like? Should I just add "\path\" to the "\.com" in RewriteCond and "/path/" before the "$1" in the RerwriteRule?

[edited by: jdMorgan at 7:58 pm (utc) on July 11, 2005]
[edit reason] Unlinked [/edit]

stu2

10:49 am on Jun 25, 2005 (gmt 0)

10+ Year Member



"Should I just add "\path\" to the "\.com" in RewriteCond and "/path/" before the "$1" in the RerwriteRule?"

OK. I tried this and added about 7 folders. I got an error from the server... can't remember the exact wording but it wouldn't let me access my home page because the were too many rules (or something).

This 36 message thread spans 2 pages: 36