Forum Moderators: phranque

Message Too Old, No Replies

Apache rewrite AND querystring?

         

Xerath

2:56 pm on Aug 12, 2011 (gmt 0)

10+ Year Member



Hi Guys :)

Sorry if this is an obvious one... I'm trying to rewrite URLs AND include any querystrings on the end, which are currently being lost..

An example URL I want is;

mysite.com/category/3/fitness-equipment.html?page=7

So I'm currently using;

RewriteRule category/(.*)/(.*).html index.php?type=category&id=$1 [NC]

Which works fine! Except my page=7 is lost... So I tried;

RewriteRule category/(.*)/(.*).html$ index.php?type=category&id=$1$3 [NC]

But that doesn't seem to look like it should work, and doesn't! :(

Would appreciate any advice :) Thanks

wilderness

3:41 pm on Aug 12, 2011 (gmt 0)

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



do a site search (archives) via google for "QSA" or "?" or "string".

g1smd

5:55 pm on Aug 12, 2011 (gmt 0)

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



Query string is automatically re-appended unless you specify a replacement query string or clear the query string.

If you replace it, but need the original query string to also be appended then use the [QSA] flag.

Do read a selection of the previous threads as they contain a lot more useful and related information.

Xerath

6:48 pm on Aug 12, 2011 (gmt 0)

10+ Year Member



QSA worked perfectly! Thank you very much :)

g1smd

10:07 pm on Aug 12, 2011 (gmt 0)

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



The next problem to fix is to remove BOTH of the
(.*)
patterns, as they are very inefficient.

(.*)
says "match the entire requested URL". The double
(.*)
pattern further says that after you have put "everything" into the first one, put "everything" into the second one - which is impossible.

This coding error will cause the parser to make tens of thousands of trial match pattern matching attempts before it finds out what you actually meant. That will bog your server down on every single request.

The
([^/]+)
and
([^/.]+)
patterns may prove useful replacements.

When faced with a request for
id=thisstuff
how does the PHP script separate "this" and "stuff" to fetch the right content?


Be aware that you have several serious Duplicate Content problems lurking.

By using the [NC] flag, you allow mixed and random cased URLs to be rewritten and for all to serve content (unless your PHP script takes steps to send a 301 redirect to the correct URL when an incorrectly cased URL is requested).

Additionally, by rewriting to
id=$1$2
you allow another form of obscure Duplicate Content issue to occur. For the content stored at index.php?id=thisstuff all of these URL requests will serve the exact same content:
/thisstuf/f.html

/thisstu/ff.html

/thisst/uff.html

/thiss/tuff.html

/this/stuff.html

/thi/sstuff.html

/th/isstuff.html

/t/hisstuff.html