You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@httpd.apache.org by Björn Heller <he...@elitehost.biz> on 2006/01/31 00:47:20 UTC

[users@httpd] RedirectMatch wrongly matching single chars of a string?

Hi List, 

I've got following problem: 

I want to redirect all requests like
site.com/something,
site.com/something/someotherthing,
site.com/something/xyz/someotherthing 

to site.com/something.html, no matter if or without trailing slash BUT NOT 
if the URL is a .gif, .jpg etc. 

So I've got the following RedirectMatch: 

RedirectMatch permanent ^/(.[^/(\.gif)(\.jpg)]*)/? 
http://www.site.com/$1.html 

It works fine for anything after site.com/ NOT containing g, i, f, j, p or . 

e.g.:
site.com/xyzxyz -> site.com/xyzxyz.html
site.com/xxxx/yxyx -> site.com/xxxx.html 

but: site.com/xxgxx -> site.com/xx.html although I bracketed \.gif etc. 
What's wrong? Can anyone make a suggestion? 

greets, hb.

---------------------------------------------------------------------
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] RedirectMatch wrongly matching single chars of a string?

Posted by Björn Heller <he...@elitehost.biz>.
Joshua Slive writes: 

> On 1/30/06, Björn Heller <he...@elitehost.biz> wrote:
>> Joshua Slive writes: 
>>
>> > On 1/30/06, Björn Heller <he...@elitehost.biz> wrote:
>> >> Joshua Slive writes:
>> >>
>> >> > On 1/30/06, Björn Heller <he...@elitehost.biz> wrote:
>> >> >
>> >> >> I want to redirect all requests like
>> >> >> site.com/something,
>> >> >> site.com/something/someotherthing,
>> >> >> site.com/something/xyz/someotherthing
>> >> >>
>> >> >> to site.com/something.html, no matter if or without trailing slash BUT NOT
>> >> >> if the URL is a .gif, .jpg etc.
>> >> >>
>> >> >> So I've got the following RedirectMatch:
>> >> >>
>> >> >> RedirectMatch permanent ^/(.[^/(\.gif)(\.jpg)]*)/?
>> >> >> http://www.site.com/$1.html
>> >> >
>> >> > You need to look again at a regex tutorial.  Stuff inside [] is a
>> >> > character class, not an arbitrary regex.  That means it will match any
>> >> > one of the set of characters included in the class.  You need
>> >> > something more like
>> >> > RedirectMatch permanent ^/(.*(?!\.(gif|jpg)))/?$ http://www.example.com/$1.html
>> >> > I haven't tested that, and the negative-lookahead assertion will
>> >> > certainly only work in httpd 2.x.
>> >> >
>> >> > Another way to do this that doesn't require as much regex magic is
>> >> > RewriteEngine On
>> >> > RewriteCond %{REQUEST_URI} !(gif|jpg)$
>> >> > RewriteRule ^/(.*)/?$ http://www.example.com/$1[R=permanent]
>> >> >
>> >> > Joshua.
>> >>
>> >> Thanks for the reply. Yes, I was wrong with thinking [^gif] would match only
>> >> the whole string. Got that in the meantime. I just tested your proposal but
>> >> it does not work =/ It redirects to /file.gif.html, /file.gif.html.html etc.
>> >> etc. in an infinite loop. ergo: it matches and redirects.
>> >
>> > You tried which proposal?  The RewriteRule one can be easily fixed by adding
>> > RewriteCond %{REQUEST_URI} !\.html$
>> >
>> > Joshua. 
>>
>> I tried your RewriteMatch proposal. It could work with a RewriteCond and a
>> RewriteRule but this wouldnt change the URL in the user's address bar and
>> that's what I want.
> 
> Yes it would.  Notice the [R=permanent] flag and look it up in the docs. 
> 
> The other big advantage to mod_rewrite is the RewriteLog, which lets
> you figure out exactly what is happening. 
> 
> Joshua.

Finally, I got it! Thanks Joshua, you brought me on the right way. Here it 
is:
RewriteRule ^/$ /start.html [R=permanent,L]
RewriteCond %{REQUEST_URI} !\.(gif|jpg|...etc...)$ [NC]
RewriteRule ^/(.[^/]*)/? /$1.html [R=permanent,L] 

Thanks, hb.

---------------------------------------------------------------------
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] RedirectMatch wrongly matching single chars of a string?

Posted by Joshua Slive <jo...@slive.ca>.
On 1/30/06, Björn Heller <he...@elitehost.biz> wrote:
> Joshua Slive writes:
>
> > On 1/30/06, Björn Heller <he...@elitehost.biz> wrote:
> >> Joshua Slive writes:
> >>
> >> > On 1/30/06, Björn Heller <he...@elitehost.biz> wrote:
> >> >
> >> >> I want to redirect all requests like
> >> >> site.com/something,
> >> >> site.com/something/someotherthing,
> >> >> site.com/something/xyz/someotherthing
> >> >>
> >> >> to site.com/something.html, no matter if or without trailing slash BUT NOT
> >> >> if the URL is a .gif, .jpg etc.
> >> >>
> >> >> So I've got the following RedirectMatch:
> >> >>
> >> >> RedirectMatch permanent ^/(.[^/(\.gif)(\.jpg)]*)/?
> >> >> http://www.site.com/$1.html
> >> >
> >> > You need to look again at a regex tutorial.  Stuff inside [] is a
> >> > character class, not an arbitrary regex.  That means it will match any
> >> > one of the set of characters included in the class.  You need
> >> > something more like
> >> > RedirectMatch permanent ^/(.*(?!\.(gif|jpg)))/?$ http://www.example.com/$1.html
> >> > I haven't tested that, and the negative-lookahead assertion will
> >> > certainly only work in httpd 2.x.
> >> >
> >> > Another way to do this that doesn't require as much regex magic is
> >> > RewriteEngine On
> >> > RewriteCond %{REQUEST_URI} !(gif|jpg)$
> >> > RewriteRule ^/(.*)/?$ http://www.example.com/$1[R=permanent]
> >> >
> >> > Joshua.
> >>
> >> Thanks for the reply. Yes, I was wrong with thinking [^gif] would match only
> >> the whole string. Got that in the meantime. I just tested your proposal but
> >> it does not work =/ It redirects to /file.gif.html, /file.gif.html.html etc.
> >> etc. in an infinite loop. ergo: it matches and redirects.
> >
> > You tried which proposal?  The RewriteRule one can be easily fixed by adding
> > RewriteCond %{REQUEST_URI} !\.html$
> >
> > Joshua.
>
> I tried your RewriteMatch proposal. It could work with a RewriteCond and a
> RewriteRule but this wouldnt change the URL in the user's address bar and
> that's what I want.

Yes it would.  Notice the [R=permanent] flag and look it up in the docs.

The other big advantage to mod_rewrite is the RewriteLog, which lets
you figure out exactly what is happening.

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] RedirectMatch wrongly matching single chars of a string?

Posted by Björn Heller <he...@elitehost.biz>.
Joshua Slive writes: 

> On 1/30/06, Björn Heller <he...@elitehost.biz> wrote:
>> Joshua Slive writes: 
>>
>> > On 1/30/06, Björn Heller <he...@elitehost.biz> wrote:
>> >
>> >> I want to redirect all requests like
>> >> site.com/something,
>> >> site.com/something/someotherthing,
>> >> site.com/something/xyz/someotherthing
>> >>
>> >> to site.com/something.html, no matter if or without trailing slash BUT NOT
>> >> if the URL is a .gif, .jpg etc.
>> >>
>> >> So I've got the following RedirectMatch:
>> >>
>> >> RedirectMatch permanent ^/(.[^/(\.gif)(\.jpg)]*)/?
>> >> http://www.site.com/$1.html
>> >
>> > You need to look again at a regex tutorial.  Stuff inside [] is a
>> > character class, not an arbitrary regex.  That means it will match any
>> > one of the set of characters included in the class.  You need
>> > something more like
>> > RedirectMatch permanent ^/(.*(?!\.(gif|jpg)))/?$ http://www.example.com/$1.html
>> > I haven't tested that, and the negative-lookahead assertion will
>> > certainly only work in httpd 2.x.
>> >
>> > Another way to do this that doesn't require as much regex magic is
>> > RewriteEngine On
>> > RewriteCond %{REQUEST_URI} !(gif|jpg)$
>> > RewriteRule ^/(.*)/?$ http://www.example.com/$1[R=permanent]
>> >
>> > Joshua. 
>>
>> Thanks for the reply. Yes, I was wrong with thinking [^gif] would match only
>> the whole string. Got that in the meantime. I just tested your proposal but
>> it does not work =/ It redirects to /file.gif.html, /file.gif.html.html etc.
>> etc. in an infinite loop. ergo: it matches and redirects.
> 
> You tried which proposal?  The RewriteRule one can be easily fixed by adding
> RewriteCond %{REQUEST_URI} !\.html$ 
> 
> Joshua.

I tried your RewriteMatch proposal. It could work with a RewriteCond and a 
RewriteRule but this wouldnt change the URL in the user's address bar and 
that's what I want.

---------------------------------------------------------------------
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] RedirectMatch wrongly matching single chars of a string?

Posted by Joshua Slive <jo...@slive.ca>.
On 1/30/06, Björn Heller <he...@elitehost.biz> wrote:
> Joshua Slive writes:
>
> > On 1/30/06, Björn Heller <he...@elitehost.biz> wrote:
> >
> >> I want to redirect all requests like
> >> site.com/something,
> >> site.com/something/someotherthing,
> >> site.com/something/xyz/someotherthing
> >>
> >> to site.com/something.html, no matter if or without trailing slash BUT NOT
> >> if the URL is a .gif, .jpg etc.
> >>
> >> So I've got the following RedirectMatch:
> >>
> >> RedirectMatch permanent ^/(.[^/(\.gif)(\.jpg)]*)/?
> >> http://www.site.com/$1.html
> >
> > You need to look again at a regex tutorial.  Stuff inside [] is a
> > character class, not an arbitrary regex.  That means it will match any
> > one of the set of characters included in the class.  You need
> > something more like
> > RedirectMatch permanent ^/(.*(?!\.(gif|jpg)))/?$ http://www.example.com/$1.html
> > I haven't tested that, and the negative-lookahead assertion will
> > certainly only work in httpd 2.x.
> >
> > Another way to do this that doesn't require as much regex magic is
> > RewriteEngine On
> > RewriteCond %{REQUEST_URI} !(gif|jpg)$
> > RewriteRule ^/(.*)/?$ http://www.example.com/$1[R=permanent]
> >
> > Joshua.
>
> Thanks for the reply. Yes, I was wrong with thinking [^gif] would match only
> the whole string. Got that in the meantime. I just tested your proposal but
> it does not work =/ It redirects to /file.gif.html, /file.gif.html.html etc.
> etc. in an infinite loop. ergo: it matches and redirects.

You tried which proposal?  The RewriteRule one can be easily fixed by adding
RewriteCond %{REQUEST_URI} !\.html$

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] RedirectMatch wrongly matching single chars of a string?

Posted by Björn Heller <he...@elitehost.biz>.
Joshua Slive writes: 

> On 1/30/06, Björn Heller <he...@elitehost.biz> wrote: 
> 
>> I want to redirect all requests like
>> site.com/something,
>> site.com/something/someotherthing,
>> site.com/something/xyz/someotherthing 
>>
>> to site.com/something.html, no matter if or without trailing slash BUT NOT
>> if the URL is a .gif, .jpg etc. 
>>
>> So I've got the following RedirectMatch: 
>>
>> RedirectMatch permanent ^/(.[^/(\.gif)(\.jpg)]*)/?
>> http://www.site.com/$1.html
> 
> You need to look again at a regex tutorial.  Stuff inside [] is a
> character class, not an arbitrary regex.  That means it will match any
> one of the set of characters included in the class.  You need
> something more like
> RedirectMatch permanent ^/(.*(?!\.(gif|jpg)))/?$ http://www.example.com/$1.html
> I haven't tested that, and the negative-lookahead assertion will
> certainly only work in httpd 2.x. 
> 
> Another way to do this that doesn't require as much regex magic is
> RewriteEngine On
> RewriteCond %{REQUEST_URI} !(gif|jpg)$
> RewriteRule ^/(.*)/?$ http://www.example.com/$1[R=permanent] 
> 
> Joshua.

Thanks for the reply. Yes, I was wrong with thinking [^gif] would match only 
the whole string. Got that in the meantime. I just tested your proposal but 
it does not work =/ It redirects to /file.gif.html, /file.gif.html.html etc. 
etc. in an infinite loop. ergo: it matches and redirects. 

hb

---------------------------------------------------------------------
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] RedirectMatch wrongly matching single chars of a string?

Posted by Joshua Slive <jo...@slive.ca>.
On 1/30/06, Björn Heller <he...@elitehost.biz> wrote:

> I want to redirect all requests like
> site.com/something,
> site.com/something/someotherthing,
> site.com/something/xyz/someotherthing
>
> to site.com/something.html, no matter if or without trailing slash BUT NOT
> if the URL is a .gif, .jpg etc.
>
> So I've got the following RedirectMatch:
>
> RedirectMatch permanent ^/(.[^/(\.gif)(\.jpg)]*)/?
> http://www.site.com/$1.html

You need to look again at a regex tutorial.  Stuff inside [] is a
character class, not an arbitrary regex.  That means it will match any
one of the set of characters included in the class.  You need
something more like
RedirectMatch permanent ^/(.*(?!\.(gif|jpg)))/?$ http://www.example.com/$1.html
I haven't tested that, and the negative-lookahead assertion will
certainly only work in httpd 2.x.

Another way to do this that doesn't require as much regex magic is
RewriteEngine On
RewriteCond %{REQUEST_URI} !(gif|jpg)$
RewriteRule ^/(.*)/?$ http://www.example.com/$1[R=permanent]

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