user1376752
user1376752

Reputation:

mod_rewrite with GET parameters

I'm trying to make a RewriteRule with get parameters. It's a simple example but still don't work.

RewriteRule ^/?localhost/([0-9]+) test.php?id=$1 [QSA]

So if I go to localhost/123 i want the 123 as $_GET in the test.php script.

Any idea why this fails? All other 'normal' RewriteRules work perfectly.

Upvotes: 0

Views: 191

Answers (2)

F. Müller
F. Müller

Reputation: 4062

Try this:

RewriteBase /localhost
RewriteRule ^(\d+)$ test.php?id=$1 [QSA]

\d stands for [0-9]

Hope this helps.

Upvotes: 0

Michael Berkowski
Michael Berkowski

Reputation: 270607

The localhost is the domain, and doesn't belong in the RewriteRule. And I have changed your [QSA] to [L,QSA] so no further rules are executed.

RewriteRule ^([0-9]+) test.php?id=$1 [L,QSA]

If this needs to be restricted so it only rewrites if the domain is localhost, then you would use a RewriteCond like:

RewriteCond %{HTTP_HOST} ^localhost$
RewriteRule ^([0-9]+) test.php?id=$1 [L,QSA]

That would be useful if you had a rewrite to perform under development only, but not when deployed to a production server.

Upvotes: 2

Related Questions