Forum Moderators: phranque

Message Too Old, No Replies

Mod rewrite - rewritten URL, now added extra parameters

         

kururunfa

5:12 pm on Feb 21, 2012 (gmt 0)

10+ Year Member



Hi everyone

I'm in a bit of a mess with mod rewrite, and would really appreciate some help.

I've been using a simple rule to convert news123.htm to point to news.php, as below:

RewriteRule news(.*)\.htm$ /news.php?newsid=$1

I've now added a third party gallery script to the site, but gallery pages don't work, and I suspect it's a mod rewrite issue. The gallery page uses parameters:

news123.htm?dir=&page=2

(Dir is blank currently, but I expect would contain text if I used folders, would contain the name of the folder).

I can't for the life of me figure out what the rewrite rule would be to stop mod rewrite just ditching everything after .htm

I thought something simple such as:

RewriteRule news(.*)\.htm(.*)\$ /news.php?newsid=$1&$2

Would work, but it doesn't seem to fire.

All I'm looking to do is have mod rewrite match the existing rule if there's no gallery stuff present, and if there is, just not strip it out.

I'd really appreciate some pointers.

Thanks.

g1smd

7:55 pm on Feb 21, 2012 (gmt 0)

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



RewriteRule cannot see parameters. It sees only the path part of the URL request and that is all your pattern can and should match.

You must add the
[L]
flag to every rule.

If you want to clear all parameters, add a trailing question mark to the rule target.
If you want to have new parameters on the rule target, list them as you have done in your original code.
If you want to re-append the original parameters on the end of some new parameters, use the QSA flag.

Don't use
(.*)
at the beginning or in the middle of a RegEx pattern. You might want to use
([^/.]+)\.
for root only or
(([^/]+/)*[^/.]+)\.
for root or folders.

kururunfa

9:10 pm on Feb 21, 2012 (gmt 0)

10+ Year Member



Thanks for such a clear and helpful response. If RewriteRule can't see parameters, no wonder nothing I was trying was working!

I've got rid of (*.) and used the QSA flag, it's working now. You've saved my sanity.

Thanks again.

g1smd

9:16 pm on Feb 21, 2012 (gmt 0)

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



If you explicitly want to test other things, use a
RewriteCond
(not needed in your case, unless you want to limit what additional parameters will actually have any effect).

A RewriteCond can test
HTTP_HOST
,
SERVER_PORT
,
QUERY_STRING
,
THE_REQUEST
and a range of other server variables.

However, let's say your new parameter is
?page=xx
and you have now re-appended any and all requested parameters to the rewrite target.

If I request
example.com/foobar?page=22
the numbered page will be served.

If I request
example.com/foobar?junk=rubbish
the main page will be served as Duplicate Content. This non-canonical page should either redirect to the canonical URL or should return the same content but with a
rel="canonical"
tag or return a 404 response.