You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@httpd.apache.org by Erik Funkenbusch <er...@gmail.com> on 2006/09/09 00:50:48 UTC
[users@httpd] Mod_rewrite and default document
I've been banging my head on this for a while and can't seem to come
up with a solution that works. Unfortunately, due to pre-exsting
url's that I can't break, I need to support the old format, which uses
this kind of URL:
http://domain/?id=123
This uses the default document passthrough, which is
http://domain/index.php?id=123
I need to have this be in the form
http://domain/123 which will rewrite the url to the passthrough URL above.
This pattern works fine to redirect the /123 to /index.php?id=123 and
also works with the long form http://domain/id=123
RewriteRule !^index\.php.* - [C]
RewriteRule ^(.*) index.php?id=$1 [L]
However, this doesn't work with the original form:
http://domain/?id=123
Can anyone offer any suggestions on making this work with the implied
default document and a query string? Thanks.
---------------------------------------------------------------------
The official User-To-User support forum of the Apache HTTP Server Project.
See <URL:http://httpd.apache.org/userslist.html> for more info.
To unsubscribe, e-mail: users-unsubscribe@httpd.apache.org
" from the digest: users-digest-unsubscribe@httpd.apache.org
For additional commands, e-mail: users-help@httpd.apache.org
Re: [users@httpd] Mod_rewrite and default document
Posted by Ace Suares <li...@suares.an>.
>
> *sigh*. Do we really need the condescending tone? Especially since
> your example doesn't work either Mr. Expert.
I am sorry if I have offended you.
I stand corrected.
Apparently, the query string is a special case in mod_rewrite.
Cheers,
ace
--
maven n : someone who is dazzlingly skilled in any field [syn: ace,
adept, champion, sensation, mavin, virtuoso, genius,
hotshot, star, superstar, whiz, whizz, wizard,
wiz]
- WordNet (r) 2.0
Suares & Co, Open Source Solutions
mail: Gravenstraat #4, Willemstad, Curacao (NA)
phone: +599 786 23 73
fax: +31 848 707 705
web: http://www.suares.an email: ace@suares.an
---------------------------------------------------------------------
The official User-To-User support forum of the Apache HTTP Server Project.
See <URL:http://httpd.apache.org/userslist.html> for more info.
To unsubscribe, e-mail: users-unsubscribe@httpd.apache.org
" from the digest: users-digest-unsubscribe@httpd.apache.org
For additional commands, e-mail: users-help@httpd.apache.org
Re: [users@httpd] Mod_rewrite and default document
Posted by Joshua Slive <jo...@slive.ca>.
On 9/13/06, Erik Funkenbusch <er...@gmail.com> wrote:
> > > See my problem? I have to match a root page request with a query
> > > string of ?id=[anything]
> >
> > There's an excellent book about regular expressions from O'Reilly. Spend a
> > weekend with it, really.
>
> *sigh*. Do we really need the condescending tone? Especially since
> your example doesn't work either Mr. Expert.
>
> This is not a simple problem. I've tried hundreds of different
> patterns, read dozens of different web pages, and yes, I've read
> Mastering Regular Expressions.
>
> > RewriteEngine on
> > # this rule will match all url's starting with index.php,
> > # including /index.php?foo=bar but also /index.phpxxx
> > RewriteRule /index.php(.*)$ /index.php$1 [L]
> > # if that rule isn't matched, the follwing rule will ut anything as id=
> > RewriteRule ^(.*)$ /index.php?id=$1 [L]
> >
> > Would that help !?
>
> No, it doesn't, because it doesn't match the url:
>
> http://mydomain/?id=abc123
>
> As far as I can tell, mod_rewrite doesn't work the way you think it
> does. The default document gets interpreted AFTER mod_rewrite
> processes the URL. I could be wrong about that, but that's what it
> seems to be doing.
>
> There seems to be some cryptic interaction between mod_rewrite and
> apache in terms of what order it processes things.
What is missing is the special handling of the query string described
in the box "Note: Query String" under here:
http://httpd.apache.org/docs/2.2/mod/mod_rewrite.html#rewriterule
You could probably use something like
RewriteCond %{QUERY_STRING} ^id=.*$
RewriteRule !/index\.php /index.php [QSA,L]
RewriteCond %{REQUEST_URI} !^/index\.php
RewriteRule (.*) /index.php?id=$1 [L]
And given the first set of rules duplicates what mod_dir will already
be doing, you might even be able to get away with just
RewriteCond %{QUERY_STRING} !^id=
RewriteRule (.*) /index.php?id=$1 [L]
Use the RewriteLog for further debugging.
Joshua.
---------------------------------------------------------------------
The official User-To-User support forum of the Apache HTTP Server Project.
See <URL:http://httpd.apache.org/userslist.html> for more info.
To unsubscribe, e-mail: users-unsubscribe@httpd.apache.org
" from the digest: users-digest-unsubscribe@httpd.apache.org
For additional commands, e-mail: users-help@httpd.apache.org
Re: [users@httpd] Mod_rewrite and default document
Posted by Erik Funkenbusch <er...@gmail.com>.
> > See my problem? I have to match a root page request with a query
> > string of ?id=[anything]
>
> There's an excellent book about regular expressions from O'Reilly. Spend a
> weekend with it, really.
*sigh*. Do we really need the condescending tone? Especially since
your example doesn't work either Mr. Expert.
This is not a simple problem. I've tried hundreds of different
patterns, read dozens of different web pages, and yes, I've read
Mastering Regular Expressions.
> RewriteEngine on
> # this rule will match all url's starting with index.php,
> # including /index.php?foo=bar but also /index.phpxxx
> RewriteRule /index.php(.*)$ /index.php$1 [L]
> # if that rule isn't matched, the follwing rule will ut anything as id=
> RewriteRule ^(.*)$ /index.php?id=$1 [L]
>
> Would that help !?
No, it doesn't, because it doesn't match the url:
http://mydomain/?id=abc123
As far as I can tell, mod_rewrite doesn't work the way you think it
does. The default document gets interpreted AFTER mod_rewrite
processes the URL. I could be wrong about that, but that's what it
seems to be doing.
There seems to be some cryptic interaction between mod_rewrite and
apache in terms of what order it processes things.
---------------------------------------------------------------------
The official User-To-User support forum of the Apache HTTP Server Project.
See <URL:http://httpd.apache.org/userslist.html> for more info.
To unsubscribe, e-mail: users-unsubscribe@httpd.apache.org
" from the digest: users-digest-unsubscribe@httpd.apache.org
For additional commands, e-mail: users-help@httpd.apache.org
Re: [users@httpd] Mod_rewrite and default document
Posted by Ace Suares <li...@suares.an>.
>
> http://mydomain/?id=abc123
> http://mydomain?id=abc123
>
> get automatically mapped to
>
> http://mydomain/index.php?id=abc123
>
> I can't change that those URL's exist already, so I have to be able to
> handle them when they come in. But I want to also handle:
>
> http://mydomain/abc123 as http://mydomain/index.php?id=abc123
>
> See my problem? I have to match a root page request with a query
> string of ?id=[anything]
There's an excellent book about regular expressions from O'Reilly. Spend a
weekend with it, really.
RewriteEngine on
# this rule will match all url's starting with index.php,
# including /index.php?foo=bar but also /index.phpxxx
RewriteRule /index.php(.*)$ /index.php$1 [L]
# if that rule isn't matched, the follwing rule will ut anything as id=
RewriteRule ^(.*)$ /index.php?id=$1 [L]
Would that help !?
Ace
>
> ---------------------------------------------------------------------
> The official User-To-User support forum of the Apache HTTP Server
> Project. See <URL:http://httpd.apache.org/userslist.html> for more
> info. To unsubscribe, e-mail: users-unsubscribe@httpd.apache.org
> " from the digest: users-digest-unsubscribe@httpd.apache.org
> For additional commands, e-mail: users-help@httpd.apache.org
--
maven n : someone who is dazzlingly skilled in any field [syn: ace,
adept, champion, sensation, mavin, virtuoso, genius,
hotshot, star, superstar, whiz, whizz, wizard,
wiz]
- WordNet (r) 2.0
Suares & Co, Open Source Solutions
mail: Gravenstraat #4, Willemstad, Curacao (NA)
phone: +599 786 23 73
fax: +31 848 707 705
web: http://www.suares.an email: ace@suares.an
---------------------------------------------------------------------
The official User-To-User support forum of the Apache HTTP Server Project.
See <URL:http://httpd.apache.org/userslist.html> for more info.
To unsubscribe, e-mail: users-unsubscribe@httpd.apache.org
" from the digest: users-digest-unsubscribe@httpd.apache.org
For additional commands, e-mail: users-help@httpd.apache.org
Re: [users@httpd] Mod_rewrite and default document
Posted by Erik Funkenbusch <er...@gmail.com>.
On 9/11/06, Bob Ionescu <bo...@googlemail.com> wrote:
> Erik Funkenbusch wrote:
> >> # 1 rewrite /?id=123 as index.php?id=123 - Does not work
>
> It is intended *not* to match in this case - and if you are using
> numerical values, why are you using the regEx .* which matches too much,
> your second rule will never work, because the first one will match once
> the filepath contains 1 or n chars.
Actually, I just used numbers as an example. I'm sorry. The value
can be anything alphanumeric.
> RewriteEngine on
> RewriteRule ^([0-9]+)$ /index.php?id=$1 [L]
I'm not sure how that would match the url /?id=123 either, even if it
were only numbers. I've tried, and the above rule doesn't even match
only with numbers. Remember, i/m not trying to match "/123" i'm
trying to match /?id=123 (or rather anything).
The problem here is that before I started using URL rewriting, then
Apache would automatically assume default.php for the default document
when using only the querystring. The following url's:
http://mydomain/?id=abc123
http://mydomain?id=abc123
get automatically mapped to
http://mydomain/index.php?id=abc123
I can't change that those URL's exist already, so I have to be able to
handle them when they come in. But I want to also handle:
http://mydomain/abc123 as http://mydomain/index.php?id=abc123
See my problem? I have to match a root page request with a query
string of ?id=[anything]
---------------------------------------------------------------------
The official User-To-User support forum of the Apache HTTP Server Project.
See <URL:http://httpd.apache.org/userslist.html> for more info.
To unsubscribe, e-mail: users-unsubscribe@httpd.apache.org
" from the digest: users-digest-unsubscribe@httpd.apache.org
For additional commands, e-mail: users-help@httpd.apache.org
Re: [users@httpd] Mod_rewrite and default document
Posted by Bob Ionescu <bo...@googlemail.com>.
Erik Funkenbusch wrote:
>> # 1 rewrite /?id=123 as index.php?id=123 - Does not work
It is intended *not* to match in this case - and if you are using
numerical values, why are you using the regEx .* which matches too much,
your second rule will never work, because the first one will match once
the filepath contains 1 or n chars.
RewriteEngine on
RewriteRule ^([0-9]+)$ /index.php?id=$1 [L]
# 2 rewrite /123/blah as index.php?id=123&blah
RewriteRule ^([0-9]+)/(.+)$ /index.php?id=$1&$2 [L]
---------------------------------------------------------------------
The official User-To-User support forum of the Apache HTTP Server Project.
See <URL:http://httpd.apache.org/userslist.html> for more info.
To unsubscribe, e-mail: users-unsubscribe@httpd.apache.org
" from the digest: users-digest-unsubscribe@httpd.apache.org
For additional commands, e-mail: users-help@httpd.apache.org
Re: [users@httpd] Mod_rewrite and default document
Posted by Erik Funkenbusch <er...@gmail.com>.
On 9/9/06, Erik Funkenbusch <er...@gmail.com> wrote:
> On 9/8/06, Bob Ionescu <bo...@googlemail.com> wrote:
> > Erik Funkenbusch wrote:
> > > RewriteRule !^index\.php.* - [C]
> > > RewriteRule ^(.*) index.php?id=$1 [L]
> > >
> > > However, this doesn't work with the original form:
> > >
> > > http://domain/?id=123
> > >
> > > Can anyone offer any suggestions on making this work with the implied
> > > default document and a query string? Thanks.
> >
> > Use
> >
> > RewriteRule !^index\.php$ - [C]
> > RewriteRule ^(.+) index.php?id=$1 [L]
> >
> > instead, which will not match an empty local filename in directory context.
>
> Thanks for the help. Unfortunately, that doesn't seem to do work.
> I'm using Apache 2.0.54 in case it matters.
>
> Here's my current .htaccess
>
> # 1 rewrite /?id=123 as index.php?id=123 - Does not work
> RewriteEngine on
> RewriteRule !^index\.php.* - [C]
> RewriteRule ^(.+) index.php?id=$1 [L]
>
> # 2 rewrite /123/blah as index.php?id=123&blah
> RewriteRule !^index\.php.* - [C]
> RewriteRule ^(.*)/(.*)$ index.php?id=$1&$2 [L]
>
> # 3 rewrite /123 as /index.php?id=123
> RewriteRule !^index\.php.* - [C]
> RewriteRule ^(.*) index.php?id=$1 [L]
>
> The second rule set of rules is used to add an additional / keyword.
> Rulesets 2 and 3 work fine, but ruleset 1 does not.
Can anyone help here? Rule #1 doesn't work on the default document
with a query string. These are the only rules in place.
---------------------------------------------------------------------
The official User-To-User support forum of the Apache HTTP Server Project.
See <URL:http://httpd.apache.org/userslist.html> for more info.
To unsubscribe, e-mail: users-unsubscribe@httpd.apache.org
" from the digest: users-digest-unsubscribe@httpd.apache.org
For additional commands, e-mail: users-help@httpd.apache.org
Re: [users@httpd] Mod_rewrite and default document
Posted by Erik Funkenbusch <er...@gmail.com>.
On 9/8/06, Bob Ionescu <bo...@googlemail.com> wrote:
> Erik Funkenbusch wrote:
> > RewriteRule !^index\.php.* - [C]
> > RewriteRule ^(.*) index.php?id=$1 [L]
> >
> > However, this doesn't work with the original form:
> >
> > http://domain/?id=123
> >
> > Can anyone offer any suggestions on making this work with the implied
> > default document and a query string? Thanks.
>
> Use
>
> RewriteRule !^index\.php$ - [C]
> RewriteRule ^(.+) index.php?id=$1 [L]
>
> instead, which will not match an empty local filename in directory context.
Thanks for the help. Unfortunately, that doesn't seem to do work.
I'm using Apache 2.0.54 in case it matters.
Here's my current .htaccess
# 1
RewriteEngine on
RewriteRule !^index\.php.* - [C]
RewriteRule ^(.+) index.php?id=$1 [L]
#2
RewriteRule !^index\.php.* - [C]
RewriteRule ^(.*)/(.*)$ index.php?id=$1&$2 [L]
#3
RewriteRule !^index\.php.* - [C]
RewriteRule ^(.*) index.php?id=$1 [L]
The second rule set of rules is used to add an additional / keyword.
Rulesets 2 and 3 work fine, but ruleset 1 does not.
---------------------------------------------------------------------
The official User-To-User support forum of the Apache HTTP Server Project.
See <URL:http://httpd.apache.org/userslist.html> for more info.
To unsubscribe, e-mail: users-unsubscribe@httpd.apache.org
" from the digest: users-digest-unsubscribe@httpd.apache.org
For additional commands, e-mail: users-help@httpd.apache.org
Re: [users@httpd] Mod_rewrite and default document
Posted by Bob Ionescu <bo...@googlemail.com>.
Erik Funkenbusch wrote:
> RewriteRule !^index\.php.* - [C]
> RewriteRule ^(.*) index.php?id=$1 [L]
>
> However, this doesn't work with the original form:
>
> http://domain/?id=123
>
> Can anyone offer any suggestions on making this work with the implied
> default document and a query string? Thanks.
Use
RewriteRule !^index\.php$ - [C]
RewriteRule ^(.+) index.php?id=$1 [L]
instead, which will not match an empty local filename in directory context.
---------------------------------------------------------------------
The official User-To-User support forum of the Apache HTTP Server Project.
See <URL:http://httpd.apache.org/userslist.html> for more info.
To unsubscribe, e-mail: users-unsubscribe@httpd.apache.org
" from the digest: users-digest-unsubscribe@httpd.apache.org
For additional commands, e-mail: users-help@httpd.apache.org