mod rewrite and $_GET

Do you have a question? Post it now! No Registration Necessary.  Now with pictures!

Threaded View

I'm pretty hazy with mod rewrite (and regular expressions in general)  and
the obvious reference,
is a little, er, hazy itself.

I'm redirecting non-existing pages to index.html:

RewriteEngine On
RewriteCond % !-f
RewriteCond % .*.html
RewriteRule ^(.*)$ index.html?page=$1

Works fine.

The non-existing gets redirected to

I would like to include a  query string as in gets redirected to or something similar, so
both page and q end up in the (PHP) $_GET array.

I know I gan get the required info out of ['REQUEST-URI] but that's a little

Think I can find it in google? Nope. Been searching on and off for days.
All sorts of real close examples that I have not been able to make work.

Has anybody done this?


Re: mod rewrite and $_GET

Richard Formby wrote:

Quoted text here. Click to load it

If you had read the manual for mod_rewrite's RewriteRule, you would've
seen the "qsappend|QSA" ("Query String Append") flag, ie:

    RewriteRule ^(.*)$ index.html?page=$1


    RewriteRule ^(.*)$ index.html?page=$1 [QSA]

Kim André Akerĝ
(remove NOSPAM to contact me directly)

Re: mod rewrite and $_GET

Kim André Akerĝ wrote"
Quoted text here. Click to load it

Thankyou. Obvious when I re-read that particular bit, and I read that
document several times. Guess I was looking for a $2 or something, not just
a "flag" :-(


Re: mod rewrite and $_GET

And lo, Richard Formby didst speak in alt.www.webmaster:

Quoted text here. Click to load it

Quoted text here. Click to load it

Are you describing an error recovery system, or a URI simplification  =

system?  If all /index.html?page=3D1.html says is that "'1.html' doesn't=

exist, please try again" then you are overcomplicating a very simple  =

problem, and actually making it worse.

You should be using the ErrorDocument directive for Not Found pages, and=

then you will find details of the requested page in the following server=

S,  =


In this way, the correct HTTP response code is sent, and there is no nee=
d  =

for any Rewrite rules at all.

If, OTOH, this is a URI simplification system, there is no need to inclu=
de  =

the .html as a required element in your addresses.  KISS and let users  =

link to instead.  Contrary to popular belief, there=

is little to no search engine benefit in including file extensions as  =

opposed to not.

Quoted text here. Click to load it

Quoted text here. Click to load it

Like Kim says, you need the QSA (Query String Append) flag.  It will  =

automatically merge sets of query strings and delimit them with ? and & =



-- =

The technical axiom that nothing is impossible sinisterly implies the  =

pitfall corollary that nothing is ridiculous.
- - Orca Search: Full-featured  =

spider and site-search engine

Re: mod rewrite and $_GET

GreyWyvern wrote
Quoted text here. Click to load it

No. It's a CMS. I'm avoiding the nasty things other CMS's use such as or, for an in the wild
example, the one somebody wrote to replace the nice Tennis site I produced:
look for me in the Tuesday Night draw here:
In the one I wrote (archive copy) you would look here:
Easier to remember and to tell somebody over the phone.

I've just never had to append a query string before.

Probably '1' in my example was not a good one. Perhaps:

The non-existing gets redirected to

And if said page doesn't exist in the database then yes, a 404 page is

Quoted text here. Click to load it

Might the user be confused with ? I perhaps would
and would probably tack a .html on to it anyway, thinking it was a typo.

The .html in was just how
it turned out using the original rewrite rule I got from somewhere.

Thanks and in hindsight [QSA] is obvious :-)


Re: mod rewrite and $_GET

Quoted text here. Click to load it

Perhaps code your CMS so that the .html is optional? So that is rewritten to ,
is rewritten to , but both
display the tnxdrawt page in the database.

Re: mod rewrite and $_GET

Richard Formby wrote:

Quoted text here. Click to load it

For what it's worth, this regular expression is more general than the
match that I assume you wanted. I assume you wanted a regular expression
equivalent to this DOS-style wildcard:


In which case, the regular expression you need is:


The expression you've used matches more widely, and will also match files
called, for example, "not-and-html-file.pdf", "foo.shtml" or "".

Toby A Inkster BSc (Hons) ARCS
Contact Me ~
Geek of ~ HTML/SQL/Perl/PHP/Python*/Apache/Linux

* = I'm getting there!

Re: mod rewrite and $_GET

Toby A Inkster wrote:
Quoted text here. Click to load it

Thanks Toby. Correct.

On thinking about it what I probably want is  .+\.html?$ - being an old DOS
programmer from way back :-)


Site Timeline