Forum Moderators: phranque

Message Too Old, No Replies

? symbol at the substitution parameter of a rewriterule

rewriterule

         

thosecars82

1:39 pm on Dec 31, 2009 (gmt 0)

10+ Year Member



Hello
What should be the different in behaviour between

RewriteCond %{REQUEST_URI} ^/$
RewriteCond %{QUERY_STRING} ^language=sp$
RewriteRule .* http://www.example.com/? [L,R=301]

and

RewriteCond %{REQUEST_URI} ^/$
RewriteCond %{QUERY_STRING} ^language=sp$
RewriteRule .* http://www.example.com/ [L,R=301]

?
The only difference is the ? symbol at the end of the rewriterule.
Thanks in advance

g1smd

8:11 pm on Dec 31, 2009 (gmt 0)

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



The second rule will re-append the original query string.

The first rule will suppress the query string.

jdMorgan

10:24 pm on Dec 31, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



And in both cases, the first RewriteCond is unnecessary and can be deleted if you simply change the RewriteRule pattern to

RewriteRule ^$

Jim

g1smd

10:29 pm on Dec 31, 2009 (gmt 0)

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



That too, and it'll be actioned a minute fraction of a second quicker too.

thosecars82

11:52 am on Jan 1, 2010 (gmt 0)

10+ Year Member



What is the ^$ pattern? is it like an empty pattern which for being so is satisfied by any string?

What should be the difference of behaviour among this
RewriteCond %{QUERY_STRING} ^language=sp$
RewriteRule .* http://www.example.com/? [L,R=301]
this

RewriteCond %{QUERY_STRING} ^language=sp$
RewriteRule ^$ http://www.example.com/? [L,R=301]

and this

RewriteCond %{REQUEST_URI} ^/$
RewriteCond %{QUERY_STRING} ^language=sp$
RewriteRule .* http://www.example.com/? [L,R=301]
?
Is it supposed that checking RewriteCond %{REQUEST_URI} ^/$
returns always true because all the URI's start with "/"?
Thanks

g1smd

1:33 pm on Jan 1, 2010 (gmt 0)

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



The first rule matches ALL URL requests (files, images, robots.txt, CSS files) and so a second comparision (the RewriteCond) has to be tested in order to see if the rule should run. That slows things down. It also has a flaw, such that if /robots.txt?language=sp were requested, the rule would run when you didn't want it to.

The second rule matches only a request for "/" and so anything not matching that will immediately abort this rule. That will make for quicker processing. The rule will then only run for the single '/?language=sp' request. This will be quicker to process than the first rule.

The third rule will does exactly the same job as the second rule, but will take longer to process because first it matches .* everything, and then has to test the first RewriteCond. Moving the pattern from the first RewriteCond to be the left side of the Rule speeds things up.

Patterns on the left side of a RewriteRule are localised to the current directory level, so ^$ matches a URL request for "/" and ^foo$ matches a URL request for "/foo".

thosecars82

2:04 pm on Jan 1, 2010 (gmt 0)

10+ Year Member



Thanks