Richard
Richard

Reputation: 1052

mod_rewrite affecting get variables

I have a PHP based blog on Apache with a simple mod_rewrite happening:

Options +FollowSymLinks
RewriteEngine On

RewriteRule ^(.*)$ index.php?story=$1 [L]

However, when I look at $_GET['story'] in the PHP script, it gives me "index.php" everytime.

Any ideas?

Upvotes: 0

Views: 64

Answers (1)

Peter Wooster
Peter Wooster

Reputation: 6089

Apache will run the rules again if there are any changes made during a pass that cause either an internal or external redirect. You should catch this with a rule for index.php

RewriteRule ^index\.php$ - [L]

that just uses that address as is. This is what WordPress does.

Depending on your version of apache you can set the rewrite log level to 8 either with RewriteLogLevel or LogLevel. On older versions this must be done in the httpd.conf, so you need a local test site.

Don't leave that logging on or your performance will suffer.

Edit:

Here's a quote from the docs on the rerun under the L flag:

If you are using RewriteRule in either .htaccess files or in sections, it is important to have some understanding of how the rules are processed. The simplified form of this is that once the rules have been processed, the rewritten request is handed back to the URL parsing engine to do what it may with it. It is possible that as the rewritten request is handled, the .htaccess file or section may be encountered again, and thus the ruleset may be run again from the start. Most commonly this will happen if one of the rules causes a redirect - either internal or external - causing the request process to start over.

Upvotes: 1

Related Questions