Forum Moderators: phranque

Message Too Old, No Replies

Are these the right htaccess lines.

to 301 redirect to www..

         

wanderingmind

7:12 pm on Jul 17, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



RewriteCond %{HTTP_HOST}!^www\.
RewriteRule ^\/?(.*)$ [%{HTTP_HOST}...] [R=301,L]

I thought this was right, but doesn't seem to work. Site still working on non-www. Am I doing something wrong?

(This is used in a subdirectory)

youfoundjake

8:28 pm on Jul 17, 2007 (gmt 0)

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



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

^^^ redirects example.com to www.example.com

RewriteCond %{THE_REQUEST} ^.*\/index\.htm?
RewriteRule ^(.*)index\.html?$ http://www.example.com/$1 [R=301,L]

^^^^ redirects www.example.com/index.htm to www.example.com

g1smd

11:50 pm on Jul 17, 2007 (gmt 0)

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



Always do the index file redirects first.

This avoids a redirection chain.

youfoundjake

12:20 am on Jul 18, 2007 (gmt 0)

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



Good to know, always the insightful one your are g1smd.

jdMorgan

2:50 am on Jul 18, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



In the example posted above, the first rule has no [L] flag. Therefore, no 'chain' will occur.

If you reverse the order of the rules, both of them should have [L] flags on them.

Jim

youfoundjake

3:49 pm on Jul 18, 2007 (gmt 0)

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



Good to know, always the insightful one your are jdMorgan

g1smd

4:05 pm on Jul 18, 2007 (gmt 0)

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



So, you are saying that the very first example redirects from example.com/index.html to www.example.com/index.html to www.example.com/ in just one move?

jdMorgan

5:04 pm on Jul 18, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



No, the code from youfoundjake in the second post will, though... No [L] flag on the first rule. Therefore, the redirect is deferred until mod_rewrite processing is completed or an [L] flag is found.

I'd recommend reversing the rules (as you did) and using [L] on both rules, but in fact it *can* be done by omitting the [L] as shown. This approach might cause some efficiency-related problems if there were a lot of additional rules following the two shown, but if those two were the only rules, then it would work well.

mod_rewrite -- A million ways to do it, and some of them will work... Far fewer still are optimal. :)

Jim

g1smd

5:25 pm on Jul 18, 2007 (gmt 0)

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



Ooops. I did mean "second example".

I am going to have to go away and think about that some more.

g1smd

5:28 pm on Jul 18, 2007 (gmt 0)

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



OK, for the second example: What happens to the "value" that is "output" from the first rule?

Is it simply discarded, or is it used as the input value for the second rule?

I think I have missed something basic in the years I have been messing with this stuff.

jdMorgan

6:06 pm on Jul 18, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



> or is it used as the input value for the second rule?

It is stored in (overwrites) REQUEST_URI and the internal req_rec variable used by mod_rewrite.

Jim

sc112

9:31 pm on Jul 18, 2007 (gmt 0)

10+ Year Member



I'm confused by this line:

RewriteCond %{THE_REQUEST} ^.*\/index\.htm?

Should it be:

RewriteCond %{THE_REQUEST} ^.*\ /index\.html?

(space after back slash)

After the two rules were processed, if we were to test for REQUEST_URI, we'd have to use the new value, right?

youfoundjake

11:46 pm on Jul 18, 2007 (gmt 0)

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



No space between \/
My pages are .htm
So the rule I placed works for .htm, I believe you can substitute .html or .php. or .cfm or whatever format you use, and i imagine jim will correct me on this if needed, or confirm, either way, yeah, no space.

jdMorgan

11:52 pm on Jul 18, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



It will work either way, but the behaviour will be different: With a space, only requests for the root /index file will be rewritten. Without a space, any and all index requests will be rewritten to the root. In this latter case, the "\" is not needed, since "/" characters do not need to be escaped in mod_rewrite.

I prefer to code this function in a non-ambiguous manner using a negative-match pattern for speed:


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

[added] Note correction to inconsistent "index\.htm?" and "index\.html?" in original RewriteCond and RewriteRule. [/added]

Jim

[edited by: jdMorgan at 11:54 pm (utc) on July 18, 2007]