Forum Moderators: phranque

Message Too Old, No Replies

mod rewrite, DirectoryIndex and optional param via QSA problem

         

isync

4:37 pm on Aug 15, 2007 (gmt 0)

10+ Year Member



I've got this .htaccess file (snippet):

Options -Indexes
RewriteEngine On
DirectoryIndex /cgi-bin/script?do=index [QSA]
RewriteRule ^some/? /cgi-bin/script?do=some [QSA]
RewriteRule ^index\.html /cgi-bin/script?do=index

The htaccess does properly rewrite any access to [foo.com...] to [foo.com...]

Now I'd like to have the ability to add an optional parameter to the passed vars. On the /some dir it works fine, doing things like:
[foo.com...]

which end up as:
[foo.com...]

BUT!
doing [foo.com...]

seems to overwrite the whole query_string! By writing DirectoryIndex /cgi-bin/script?do=index& [QSA] (added &) I was able to debug a bit. My query_string then becomes:

[foo.com...]

Where is the problem in my htaccess? How can I get the optional add=123 added as /cgi-bin/script?do=index&add=123 internally (which QSA should do..)?

jdMorgan

4:50 pm on Aug 15, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



First, the [QSA] flag is specific to mod_rewrite, and won't do anything on a DirectoryIndex (mod_dir) directive.

Second, DirectoryIndex is dumb as a rock, and is not intended to do fancy query-string-append manipulations.

I suspect the problem is simply that requesting "/" invokes DirectoryIndex, which is replacing both the URL-path and the query string.

If this were my problem, I would stop using DirectoryIndex, and instead, explicitly rewrite directory index path requests to the appropriate filepath, along with any fixed query parameters desired. Using [QSA] on that rule would then allow appending the fixed query data to any query string already present on the client-requested URL-path.

For example,


RewriteRule ^(index\.(html?¦php))?$ /cgi-bin/script?do=index [QSA,L]

Will rewrite requests for any of the following URL-paths to your script, retaining any attached query-strings:
example.com/
example.com/index.htm
example.com/index.html
example.com/index.php

(You will need to replace the broken pipe "¦" character with a solid pipe character before use; Posting on this forum modifies the pipe characters.

Jim

isync

5:26 pm on Aug 15, 2007 (gmt 0)

10+ Year Member



I've done as advised and.... it works like a charm!
Thanks so much for this hint!

(Actually I wasn't aware that DirectoryIndex is a mod_dir directive....)