magener
magener

Reputation:

Replace ? in request with RewriteRule

site.com/link?p=2

give $_GET['p']==2 even though i've already made

site.com/link

rewrite to

site.com/index.php?page=link

So, i'm trying to replace site.com/link?p=2 with site.com/link&p=2

RewriteEngine on

RewriteRule (.*)\?(.*) $1\&$2

RewriteCond %{REQUEST_URI} !\....$
RewriteRule ^(.*)$ /index.php?p=$1

It doesn't work!

Upvotes: 0

Views: 170

Answers (2)

Denis P
Denis P

Reputation: 776

There are cases when you need both the URI and the query string within the same regex. I'm providing an answer for that case, as this request is redirected here as well by Google.

An example of such a case would be checking for presence of a URL part, or a parameter in a Regex assertion. Use %{THE_REQUEST} parameter for that cases, and NOT %{REQUEST_URI}. Here is how it looks in such an example (similar to what I had in the real life scenario):

RewriteCond %{THE_REQUEST} "/account(?!/password|/\?action=reset_pwd).*" [NC]
RewriteRule .? /profile [R,L]

Upvotes: 0

chaos
chaos

Reputation: 124257

RewriteRule cannot see query strings (the ? and anything after it) on the left-hand side; it matches only on the path part of the URL.

But the good news is, all you probably need to do is this:

RewriteEngine on

RewriteCond %{REQUEST_URI} !\....$
RewriteRule ^(.*)$ /index.php?p=$1 [QSA]

The QSA option, Query String Add, tells your RewriteRule to add to the query string instead of replacing it (the default behavior, which doubtless prompted the whole issue).

Upvotes: 3

Related Questions