Forum Moderators: phranque

Message Too Old, No Replies

need to be sure my 301 redirect is right

         

annej

7:35 am on Feb 8, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I've been trying to figure out how to redirect all the non www to www.

I've found these two possibilities

Options +FollowSymlinks
RewriteEngine on
rewritecond %{http_host} ^domain.com [ncrewriterule ^(.*)$ http://www.example.com/$1 [r=301,nc]

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

One has [r=301,nc] and the other has[L,R=301]

Do they do something differently or do both accomplish the same thing.

[edited by: engine at 10:28 am (utc) on Feb. 8, 2008]
[edit reason] examplified [/edit]

phranque

10:13 am on Feb 8, 2008 (gmt 0)

WebmasterWorld Administrator 10+ Year Member Top Contributors Of The Month



i think there is a typo in your first example.
NC means no-case which makes the pattern case insensitive.
should only be necessary if there are alphabetics in your pattern.
L means last which stops rewrite processing.
your second example is probably what you want to use.

the last post in this thread [webmasterworld.com] has two templates with explanatory comments for your requirements.

annej

4:38 pm on Feb 8, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Thanks so much! I am clueless on this stuff so am glad to know what I set up is correct.

I went with this one from the thread you sent me to.

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

[edited by: jdMorgan at 12:29 am (utc) on Feb. 9, 2008]
[edit reason] example.com [/edit]

g1smd

10:43 pm on Feb 8, 2008 (gmt 0)

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



You might also want to redirect index file URLs before doing the non-www and www stuff.

youfoundjake

12:14 am on Feb 9, 2008 (gmt 0)

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



annej
once your done putting the code into .htaccess, double check to make sure that the request does return an actual 301.
Try a google search [for http status code checker]

as far as the .htaccess file
here is mine

RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /([^/]+/)*index\.php\ HTTP/
RewriteRule ^(([^/]+/)*)index\.php$ http://www.example.com/$1 [R=301,L]
#
RewriteCond %{HTTP_HOST} ^example\.com [NC]
RewriteRule (.*) http://www.example.com/$1 [R=301,L]

The first rewrite takes index.php and redirects it to / and the second will of course redirect example.com to www.example.com
Have a good weekend everyone!
me

[edited by: jdMorgan at 12:33 am (utc) on Feb. 9, 2008]
[edit reason] No URLs please. [/edit]

g1smd

12:46 am on Feb 9, 2008 (gmt 0)

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



That code is very easy to edit if you use index.html or index.htm or something else.

annej

4:55 am on Feb 9, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



You might also want to redirect index file URLs before doing the
non-www and www stuff.

I wondered about that. How would it look if I combined it with

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

I've found I can edit it easily with HTML Notepad by downloading the old one
then opening it and making changes. Love those old tools.

I had already tested it simply by noticing that if I looked for the non www version it would flip over the the www. It's good to know there about status code checkers so I can try that.

jdMorgan

6:43 pm on Feb 9, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



> I wondered about that. How would it look if I combined it with ...

The result would be the code posted above by youfoundjake.

Jim

youfoundjake

8:02 pm on Feb 9, 2008 (gmt 0)

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




The result would be the code posted above by youfoundjake.

heeh,code courtesy of Jim from a thread a long long time ago in a unix enviroment far far away.
jdMorgan is all things Apache.
:)

annej

5:17 am on Feb 10, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member




RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /([^/]+/)*index\.php\ HTTP/
RewriteRule ^(([^/]+/)*)index\.php$ http://www.example.com/$1 [R=301,L]

What threw me on this one is that I don't use php. Also what does the [A-Z]{3,9}\ /([^/]+/ do? How could I write it just for index?

phranque

7:05 am on Feb 10, 2008 (gmt 0)

WebmasterWorld Administrator 10+ Year Member Top Contributors Of The Month



this should work:
Options +FollowSymLinks
RewriteEngine on
# if index.html is requested, redirect ...index.html to /
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /([^/]+/)*index\.html\ HTTP/
RewriteRule ^(([^/]+/)*)index\.html$ http://www.example.com/$1 [R=301,L]
# if subdomain is missing, rewrite to www. subdomain
RewriteCond %{HTTP_HOST} ^example\.com [NC]
RewriteRule (.*) http://www.example.com/$1 [R=301,L]

what does the [A-Z]{3,9}\ /([^/]+/) do?

if your index file is index.html, a typical HTTP request might look like:
GET /some/path/to/index.html HTTP/1.1

that regular expression is everything in the request up to but not including the "index.html HTTP/"

annej

3:12 pm on Feb 10, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



# if index.html is requested, redirect ...index.html to /
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /([^/]+/)*index\.html\ HTTP/

I have used htm on my sites instead of html so do I change the code to htm?

g1smd

5:43 pm on Feb 10, 2008 (gmt 0)

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



Yes, change index.html in the code to be whatever the filename you use actually is. There are TWO places in the code that need to be amended in this way.

The code has that extra stuff on it so that it can work both for index files in the root of the site and for index files in folders. The folder-path is preserved in that redirect. That's a Good Thing.

annej

6:11 pm on Feb 10, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Good to have it work for folders too. I've never used subdomains but most of my sites have folders.

Do I have the winner in the code below? I don't need to leave in the comments do I?

Options +FollowSymLinks
RewriteEngine on

RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /([^/]+/)*index\.htm\ HTTP/
RewriteRule ^(([^/]+/)*)index\.htm$ http://www.example.com/$1 [R=301,L]

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

youfoundjake

8:17 pm on Feb 10, 2008 (gmt 0)

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



Looks good to me annej, once you place it in your .htaccess file, don't forget to check your header status with the link I sent you.

probably the best way to see the results is by typing in
http://example.com/index.htm

and you should see


#1 Server Response: http://example.com/index.htm
HTTP Status Code: HTTP/1.1 301 Moved Permanently
Date: Sun, 10 Feb 2008 20:13:53 GMT
Location: http://www.example.com/
Connection: close
Redirect Target: http://www.example.com/

#2 Server Response: http://www.example.com/
HTTP Status Code: HTTP/1.1 200 OK
Date: Sun, 10 Feb 2008 20:13:53 GMT
Connection: close

That will show you that first the index.htm is redirected to / and then the non-www is redirected to www.
so it looks like this:
example.com/index.htm -> example.com/ -> www.example.com/

jdMorgan

8:27 pm on Feb 10, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Actually, that's not what it will show... Thankfully.

It will show that the first rule redirects index.htm to www.example.com/ all in one step, no matter whether it is requested as example.com/index.htm or www.example.com/index.htm

The second rule is not invoked in this case. To test the scoend rule, request example.com/ and you should see that get redirected to www.example.com by virtue of the second rule. Or you can test the second rule by requesting any URL-path except the /index.htm path that invokes the first rule.

Jim

[edited by: jdMorgan at 8:28 pm (utc) on Feb. 10, 2008]

g1smd

8:45 pm on Feb 10, 2008 (gmt 0)

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



JD beat me to it.

The index file redirect also forces www at the same time so that a redirection chain is avoided.

The second redirect forces www for everything else.

youfoundjake

9:00 pm on Feb 10, 2008 (gmt 0)

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



Right right,
I should have reiterated that. I just took it from a non-logical way of how the redirects look graphically, given the arguement.
Of course the first request for index.htm will redirect to the fully qualified domain name.

jdMorgan

9:09 pm on Feb 10, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



It's annej's thread, and the posts above are addressed to annej. We must correct factual errors, or misunderstandings will only spread more.

Jim

youfoundjake

1:33 am on Feb 11, 2008 (gmt 0)

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



Actually, that's not what it will show... Thankfully.

Um. ok, a little confused here...

when the request is for index.htm, or index.php, the server is showing a 301 redirect to www.example.com/

then it queries www.example.com/ which returns a status 200 OK message.

i had mentioned using example.com/index.htm as the URL to make sure that it redirected to www.example.com/ domain without the index.htm at the end of the file path.

If that's not what the header check is supposed to show, what is it supposed to show?

annej

2:57 am on Feb 11, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



non-logical way of how the redirects look graphically

Jake knows enough about my background to know the "non-logical" way would make it clear to me. ;) But the more complete info will be good for people reading this thread who have a clue.

My own highly technical way to check it was to put in
http://example.com/index.htm
and
http://www.example.com/index.htm
then watch to see if the right URL pops up at the top of my screen.

It all works great now. I just want to thank everyone. I have enough understanding of the whole thing that I won't mess it up again like I did to the one my server people put up a couple of years ago. Yet I didn't have to spend days trying to figure it out on my own and still wonder if I had messed things up again.

Thanks again

Anne

[edited by: tedster at 4:42 am (utc) on Feb. 11, 2008]
[edit reason] switch to example.com [/edit]

jdMorgan

3:27 am on Feb 11, 2008 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



This is what was incorrect:
That will show you that first the index.htm is redirected to / and then the non-www is redirected to www.
so it looks like this:
example.com/index.htm -> example.com/ -> www.example.com/

All that shows (and should show) in the headers display above is:

example.com/index.htm --301 redirect--> www.example.com/ (200-OK)

And this is the correct and desired result, canonicalization of both the index page URL-path and the domain with a single redirect.

Jim

phranque

8:36 am on Feb 11, 2008 (gmt 0)

WebmasterWorld Administrator 10+ Year Member Top Contributors Of The Month



annej:

i would suggest the Live HTTP Headers 0.13.1 firefox add-on [addons.mozilla.org] for checking these types of header/redirect issues.