Forum Moderators: phranque

Message Too Old, No Replies

Rewriting unsupported urls

Mod_rewrite, regex and redirection

         

SomeNameIJustMadeUp

3:59 pm on Jul 28, 2010 (gmt 0)

10+ Year Member



Hi there! This has been really annoying me for a few hours- I can't seem to crack it.

Assuming I'm using a url scheme like [whatever.com...] and I'm supporting en, de and fr language codes, and I come accross a url with it at the root of the url path, how can I redirect to a default, like en?

For example, If I visit www.whatever.com/es/ad/hoc/path, as es is not supported, I want to redirect to www.whatever.com/en/ad/hoc/path. The rule I've been using (and failing with) is this:

RewriteCond %{REQUEST_URI} !^/[en|de|fr](.*)$
RewriteRule ^$ /en/$1 [R,L]


What I think that means, is that if a url does not begin with en, de or fr, capture the remainder of the url (expressed by the wildcard before the dollar in the RewriteCond), then prepend it with en in the rewrite rule.

It (or variants of it) work.... if I want endless redirects. Can anyone shed any light on how to accomplish this?

Many thanks

jdMorgan

2:39 am on Jul 29, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



1) Square brackets define and contain alternate character groups. The pattern "[aeiou]" means "match any single character that is a vowel." This is equivalent to the pattern "a|e|i|o|u".
2) Back-references cannot be made to negative-match patterns (see the Apache mod_rewrite docs).
3) Back-references $1-$9 refer to the string matching the first through ninth parenthesized sub-patterns in the RewriteRule. Back-references %1-%9 refer to the string matching the first through ninth parenthesized sub-patterns in the last-matched RewriteCond.
4) The substitution for external redirects should specify a canonical URL to avoid various potential problems.

RewriteCond $1 !^(en|de|fr)$
RewriteRule ^([a-z]{2})/(.*)$ http://www.example.com/en/$2 [R=302,L]

Jim