Bryan
Bryan

Reputation: 69

mod_rewrite rule not sending $_GET PHP parameter

I've seen a couple other questions similar to this, but the answers are not working for me. Hopefully someone can shed some light.

I have a URL like http://website.com/show/?id=9999 and I use $_GET['id'] in a PHP script on the page. When I use a rewrite rule to convert the URL to http://website.com/show/9999/ then $_GET['id'] is no longer working.

Here's the rewrite rules I'm using:

    Options -MultiViews
    RewriteEngine On
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule ^show/(.*)$ /show/?id=$1 [NC,L,QSA]

Upvotes: 1

Views: 137

Answers (3)

Bryan
Bryan

Reputation: 69

Gah! Fixed it. I left out a key piece of info, which is that http://website.com/show/ is also rewritten through the WordPress permalink engine. The "L" flag in my rule was preventing the target from being picked up again by the WordPress rewrite rules.

Upvotes: 1

mopsyd
mopsyd

Reputation: 1922

The $_GET['id'] is no longer working, because the id parameter is no longer part of the get string. The apache rewrite happens at a lower layer of the web stack than php operates, which means that there is no longer a $_GET string at all by the time php takes over. You can test this with var_dump($_GET); and see for yourself.

That stated, it can be accessed via $_SERVER['REQUEST_URI'] by doing the following:

$params = explode('/', $_SERVER['REQUEST_URI']);
var_dump($params[0]); //This should print "show"
var_dump($params[1]); //This should print "9999"

You will need to be able to anticipate which segment of the URL corresponds to the value you require to do it this way. This is typically the job of a router.

Example: http://upshots.org/php/php-seriously-simple-router

Upvotes: 0

Adrianopolis
Adrianopolis

Reputation: 1292

You don't want those rewrite conditions. You just need the rule. This should work:

RewriteEngine On
RewriteRule ^show/([^/]*)$ /show/?id=$1 [L]

Upvotes: 0

Related Questions