This is Interesting: Free Magazines for Graphics designers and webmasters  


Home > Archive > Webmaster forum > March 2007 > mod rewrite and $_GET





You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

Author mod rewrite and $_GET
Richard Formby

2007-03-22, 7:23 pm

I'm pretty hazy with mod rewrite (and regular expressions in general) and
the obvious reference,
http://httpd.apache.org/docs/1.3/mod/mod_rewrite.html
is a little, er, hazy itself.

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

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

Works fine.

The non-existing http://example.com/1.html gets redirected to
http://example.com/index.html?page=1.html

I would like to include a query string as in
http://example.com/1.html?q=query gets redirected to
http://example.com/index.html?page=1.html&q=query 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
messy.

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?

--
Richard.









Kim André Akerĝ

2007-03-22, 7:23 pm

Richard Formby wrote:

> I'm pretty hazy with mod rewrite (and regular expressions in general)
> and the obvious reference,
> http://httpd.apache.org/docs/1.3/mod/mod_rewrite.html is a little,
> er, hazy itself.
>
> I'm redirecting non-existing pages to index.html:
>
> RewriteEngine On
> RewriteCond %{REQUEST_FILENAME} !-f
> RewriteCond %{REQUEST_FILENAME} .*.html
> RewriteRule ^(.*)$ index.html?page=$1
>
> Works fine.
>
> The non-existing http://example.com/1.html gets redirected to
> http://example.com/index.html?page=1.html
>
> I would like to include a query string as in
> http://example.com/1.html?q=query gets redirected to
> http://example.com/index.html?page=1.html&q=query 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 messy.
>
> 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?


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

becomes:

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

http://httpd.apache.org/docs/2.0/mo...tml#rewriterule

--
Kim André Akerĝ
- kimandre@NOSPAMbetadome.com
(remove NOSPAM to contact me directly)
Richard Formby

2007-03-22, 7:23 pm

Kim André Akerĝ wrote"
> Richard Formby wrote:
>
[color=darkred]
[color=darkred]
> 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
>
> becomes:
>
> RewriteRule ^(.*)$ index.html?page=$1 [QSA]
>
> http://httpd.apache.org/docs/2.0/mo...tml#rewriterule


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" :-(

--
Richard.


GreyWyvern

2007-03-22, 7:23 pm

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

> I'm pretty hazy with mod rewrite (and regular expressions in general) =

=

> and the obvious reference,
> http://httpd.apache.org/docs/1.3/mod/mod_rewrite.html
> is a little, er, hazy itself.
>
> I'm redirecting non-existing pages to index.html:
>
> RewriteEngine On
> RewriteCond %{REQUEST_FILENAME} !-f
> RewriteCond %{REQUEST_FILENAME} .*.html
> RewriteRule ^(.*)$ index.html?page=3D$1
>
> Works fine.
>
> The non-existing http://example.com/1.html gets redirected to
> http://example.com/index.html?page=3D1.html


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=
=

variables: REDIRECT_ERROR_NOTES, REDIRECT_REQUEST_METHOD, REDIRECT_STATU=
S, =

REDIRECT_UNIQUE_ID, REDIRECT_URL

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 http://example.com/1 instead. Contrary to popular belief, there=
=

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

opposed to not.

> I would like to include a query string as in
> http://example.com/1.html?q=3Dquery gets redirected to
> http://example.com/index.html?page=3D1.html&q=3Dquery or something sim=

ilar, =

> so both page and q end up in the (PHP) $_GET array.
>
> Think I can find it in google? Nope. Been searching on and off for day=

s.
> All sorts of real close examples that I have not been able to make wor=

k.

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

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

properly.

Grey

-- =

The technical axiom that nothing is impossible sinisterly implies the =

pitfall corollary that nothing is ridiculous.
- http://www.greywyvern.com/orca#search - Orca Search: Full-featured =

spider and site-search engine
Richard Formby

2007-03-22, 7:23 pm

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


[color=darkred]
>Are you describing an error recovery system, or a URI simplification
>system? If all /index.html?page=1.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.


No. It's a CMS. I'm avoiding the nasty things other CMS's use such as
http://example.com/generator.php?page=whatever 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:
http://hdta.org.au/index.cfm?s=2C1A...50D89838C42530F
In the one I wrote (archive copy) you would look here:
http://barefile.com.au/hdta/tnxdraw.html
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 http://example.com/tnxdraw.html gets redirected to
http://example.com/index.html?page=tnxdraw.html

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

>If, OTOH, this is a URI simplification system, there is no need to include
>the .html as a required element in your addresses. KISS and let users ink
>to http://example.com/1 instead. Contrary to popular belief, there s
>little to no search engine benefit in including file extensions as pposed
>to not.


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

The .html in http://example.com/index.html?page=tnxdrawt.html was just how
it turned out using the original rewrite rule I got from somewhere.

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

--
Richard.


Toby A Inkster

2007-03-22, 7:23 pm

Richard Formby wrote:

> RewriteCond %{REQUEST_FILENAME} .*.html


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:

*.html

In which case, the regular expression you need is:

.+\.html$

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

--
Toby A Inkster BSc (Hons) ARCS
Contact Me ~ http://tobyinkster.co.uk/contact
Geek of ~ HTML/SQL/Perl/PHP/Python*/Apache/Linux

* = I'm getting there!
Richard Formby

2007-03-22, 11:19 pm

Toby A Inkster wrote:
> Richard Formby wrote:
>
>
> 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:
>
> *.html
>
> In which case, the regular expression you need is:
>
> .+\.html$
>
> The expression you've used matches more widely, and will also match files
> called, for example, "not-and-html-file.pdf", "foo.shtml" or
> "foo.html.zip".


Thanks Toby. Correct.

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

--
Richard.


BBM

2007-03-25, 11:19 pm

On Mar 22, 9:09 am, "Richard Formby" <r...@invalid.com> wrote:
> GreyWyvern wrote
>
>
> No. It's a CMS. I'm avoiding the nasty things other CMS's use such ashttp://example.com/generator.php?page=whateveror, 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:http://hdta.org.au/index.cfm?s=2C1A...50D89838C42530F
> In the one I wrote (archive copy) you would look here:http://barefile.com.au/hdta/tnxdraw.html
> 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-existinghttp://example.com/tnxdraw.htmlgets redirected tohttp://example.com/index.html?page=tnxdraw.html
>
> And if said page doesn't exist in the database then yes, a 404 page is
> presented.
>
>
> Might the user be confused withhttp://example.com/tnxdraw? I perhaps would
> and would probably tack a .html on to it anyway, thinking it was a typo.
>
> The .html inhttp://example.com/index.html?page=tnxdrawt.htmlwas just how
> it turned out using the original rewrite rule I got from somewhere.
>
> Thanks and in hindsight [QSA] is obvious :-)


Perhaps code your CMS so that the .html is optional? So that
http://example.com/tnxdrawt.html is rewritten to
http://example.com/index.html?page=tnxdrawt.html, http://example.com/tnxdrawt
is rewritten to http://example.com/index.html?page=tnxdrawt, but both
display the tnxdrawt page in the database.

Sponsored Links


Copyright 2003 - 2008 forum4designers.com  Software forum  Computer Hardware reviews