Forum Moderators: phranque

Message Too Old, No Replies

Two basic rewrite rule questions to help me understand better

         

StaceyJ

5:11 pm on Dec 17, 2010 (gmt 0)

10+ Year Member



I can't find a definitive answer by searching here, and I hope it's not the 800th time it's been asked, but I'm trying to wrap my brain around all of this so I don't keep asking stupid questions. I also refer to this Webmaster World post [webmasterworld.com] for basic help.

I have the following in my htaccess to redirect IP address and non www to www.

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

1. Is it really necessary to use (www\.example\.com)? and wouldn't www\.example\.com suffice? I've seen it both ways but I'd like a pros answer. I understand the ()? means match 0 or 1 time, or at least I think I understand that's what that means.

2. Is it really necessary to use ^(.*)$ instead of just (.*) ? I've seen it both ways also, is one more explicit than the other? I just don't understand how the anchors would make it any more so.

I like to keep my code as short and efficient as possible, but I also don't want to leave out a few extra characters that might make a difference in efficiency or worse yet, functionality.

Thanks!

g1smd

8:52 pm on Dec 17, 2010 (gmt 0)

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



You need the ( )? so that blank HTTP/1.0 hostname requests do not trigger an infinite loop.

I always use (.*) but have never benchmarked if ^(.*)$ is faster or more efficient.

StaceyJ

9:53 pm on Dec 17, 2010 (gmt 0)

10+ Year Member



Thank you!