Forum Moderators: phranque

Message Too Old, No Replies

Using alphabet range in a redirect

         

JimmieT

12:23 pm on Dec 22, 2009 (gmt 0)

10+ Year Member



I've been looking at the various methods for Rewrite and Redirect, but just seem to be lacking in understanding.

I am using this redirect: redirect 301 /db/ipb.htm [mysite.com...]
It works just fine, but . . .
I'd like it to include the whole alphabet as such:
redirect 301 /db/ip(whole alphabet or alphabet range).htm [mysite.com...]

I have tried: /db/ip[a-z].htm and /db/ip[abcdefghijklmnopqrstuvwxyz].htm, neither of which work.

Also, the "db" could also be "DB"

Perhaps I am using the incorrect method and/or not getting the puncuation correct.

All help is appreciated.

g1smd

12:47 pm on Dec 22, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



If you use RewriteRule instead of Redirect you'll have a lot more power at your fingertips to do this very efficiently.

JimmieT

2:27 pm on Dec 22, 2009 (gmt 0)

10+ Year Member



Thank you for your suggestion, which works great. Only two lines to do exactly what I needed.

RewriteRule ^db/ip[a-z].htm$ [MySite.com...] [R,NC,L]
RewriteRule ^db/$ [MySite.com...] [R,NC,L]

g1smd

3:19 pm on Dec 22, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Beware that [R] gives a 302 redirect.

You might need R=301 instead.

JimmieT

9:05 pm on Dec 22, 2009 (gmt 0)

10+ Year Member



~g1smd

Yes that is what I need, R=301.

Thank you again.

jdMorgan

1:22 am on Dec 23, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Escape all literal periods in regular-expressions patterns.

RewriteRule ^db/ip[a-z[b]]\.h[/b]tm$ http://www.example.com/ [NC,R=301,L]
#
RewriteRule ^db/$ http://www.example.com/ [NC,R=301,L]

Your original approach would have worked with the RedirectMatch directive instead of the Redirect directive. The RedirectMatch directive uses regular expressions, as opposed to the prefix-matching used by Redirect.

However, a good bit of advice is that if you use mod_rewrite for any redirects and/or rewrites, then use it for all redirects and rewrites, because otherwise the execution order of the two kinds of directives is controlled by your server configuration, and not by the order that those directives appear in your code; Execution is 'per-module', and not line-by-line.

If you have not researched (and bookmarked) the Apache module documentation (mod_alias and mod_rewrite in this case), let me suggest that doing so will save you an awful lot of time and frustration...

Jim