views:

404

answers:

2

I'm trying to get all HTML and PHP files on my site to redirect through the index.php, so that they can have common frames applied to them, a templating solution I coded that I found to be quite elegant. Anywho, I'm having issues with my PHP files, specifically those that have arguments after them.

My regular rule for PHP files is the following:

RewriteCond %{REQUEST_URI} !index.php$
RewriteRule ^(.+).php$ index.php?page=$1&type=1 [NC,L]

This works fine for any pages that have no arguments, but you can see that any PHP documents that have

?argument=something

end up as:

index.php?page=path/to/page?argument=something&type=1

which is not a working solution at all. Now, what's bothering me here is the $ at the end of the rule, shouldn't that cause it to fail if there is anything after the .php?

Anywho, I tried rewriting the rule as:

RewriteRule ^(.+).php\?(.+)$ index.php?page=$1&type=1&$2 [NC,L]

but that simply doesn't trigger at all. It seems that the regex flavor used in mod_rewrite is far different than I'm used to working with, so I'm sure these are simple mistakes I've made, but I can't seem to find decent documentation for this flavor of regex other than the most basic of examples.

Can anyone show me what I'm doing wrong? Thanks.

+3  A: 

Try qsa in your rule, which stands for "query string append" - mod_rewrite will then append any query string from the original URL to the rewritten URL

RewriteCond %{REQUEST_URI} !index.php$
RewriteRule ^(.+).php$ index.php?page=$1&type=1 [NC,L,qsa]

RewriteRule doesn't match against the query string, which is why your second attempt did not work. Here's the relevant note from the manual

The Pattern will not be matched against the query string. Instead, you must use a RewriteCond with the %{QUERY_STRING} variable. You can, however, create URLs in the substitution string, containing a query string part. Simply use a question mark inside the substitution string, to indicate that the following text should be re-injected into the query string. When you want to erase an existing query string, end the substitution string with just a question mark. To combine a new query string with an old one, use the [QSA] flag.

Paul Dixon
Yay! That worked like a charm. I didn't know it wasn't testing the whole string. ^_^
Nicholas Flynt
A: 

Common question this. Duplicate of Apache mod rewrites giving 404 error and how do websites do this index.php?something=somepage.

cletus