Domanda

I have the following rewrite rules:

RewriteRule ^/events$ /pagebase.php?pbid=3627 [QSA,L,I]
RewriteRule ^/events-list$ /pagebase.php?pbid=3663 [QSA,L,I]
RewriteRule ^/bob2013$ /pagebase.php?pbid=3688 [QSA,L,I]
RewriteRule ^/contact-us$ /pagebase.php?pbid=3634 [QSA,L,I]
RewriteRule ^/detail$ /pagebase.php?pbid=3890 [QSA,L,I]

Which work great until I need to pass in query string.

So if I type

http://www.domain.com/events It works perfectly.

But if I type in

http://www.domain.com/events?type=1

Then the rewrite fails because it does not match "events" anymore.

However if I remove the dollar sign then it works fine. The query string type is passed through correctly.

like so.

RewriteRule ^/events$ /pagebase.php?pbid=3627 [QSA,L,I]

However the issue with that is, if someone types the url for events list.

http://www.domain.com/events-list

The rewrite matches the "events" page displays that page.

Basically, I need to know how to get around this issue.

È stato utile?

Soluzione

The easiest solution to this is to order the rules accordingly: specific rules before general rules.

Since the first rule (without the $ at the end) includes the second rule, put the second rule before the first one:

RewriteRule ^/events-list /pagebase.php?pbid=3663 [QSA,L,I]
RewriteRule ^/events      /pagebase.php?pbid=3627 [QSA,L,I]

Now any url starting with /events-list will be rewritten to pbid=3663.

However, in order to prevent urls like /events-anything from matching, it's better to include the matching of the optional query string in the rule:

RewriteRule ^/events-list(\?.*)?$ /pagebase.php?pbid=3663 [QSA,L,I]
RewriteRule ^/events(\?.*)?$      /pagebase.php?pbid=3627 [QSA,L,I]
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top