PDA

View Full Version : mod_rewrite troubles


_Dan
01-27-2007, 07:23 PM
Hi

This gives an "internal server error":

RewriteRule ^([A-Za-z0-9-]+)/(.+)$ /mydir/$2&FAKE_PHP_SELF=$1

Why? (it works without the &FAKE_PHP_SELF=$1)

I'm trying to change any random dir to "mydir" keeping the trailing GET variable string intact, also appending another get variable, FAKE_PHP_SELF which equals the original random dir.

E.g. rewriting /anything/index.php?stuff as /mydir/index.php?stuff&FAKE_PHP_SELF=anything


Also, another question...

RewriteRule ^([A-Za-z0-9-]+)/(.+)$ /mydir/$2

Works, redirecting /anything/index.php?stuff, however it fails to redirect /anything/?stuff, instead giving 404. Why?

Thanks.

schleppel
01-28-2007, 05:36 PM
RewriteRules in .htaccess files match the bit after the current dircetory (in your case /) and before the ? (if it's there at all).

This gives an "internal server error":
The server probably doesn't unserstand the request to /anything/index.php&FAKE_PHP_SELF=anything.

Works, redirecting /anything/index.php?stuff, however it fails to redirect /anything/?stuff, instead giving 404. Why?
You have .+ (one ot more of anything), so it doesn't match nothing.

Try
Options +FollowSymLinks

RewriteEngine On

RewriteRule ^([-a-z0-9]+)/(.*)$ /mydir/$2?FAKE_PHP_SELF=$1 [NC,QSA,L]

_Dan
01-29-2007, 02:31 AM
So the QSA flag makes it rewrite the whole lot past the ? symbol? That's good.

The rule you gave me doesn't give the internal error, however I need to append the FAKE_PHP_SELF with a & as there is already some get vars.

And it doesn't seem to work with a &

RewriteRule ^([-a-z0-9]+)/(.+)$ /mydir/$2&FAKE_PHP_SELF=$1 [NC,QSA]

(gives the internal server error again)

schleppel
01-29-2007, 07:27 PM
Don't change it just copy what i gave you. The QSA (Query String Append) flag will add the query string on to the result URL before it's rewritten. You don't want .+.

_Dan
01-29-2007, 08:58 PM
Ok thanks schleppel :)