0

could someone check this regex I wrote, it does what I wanted to achieve, but I'm not sure if it's the correct way to do it and if it's not slowing everything up

That's what it should do:

IF the URL Path is longer than the domain only AND IF it doesn't contain the strings "/de" or "/en" at the beginning THEN 301 it to the domain only

That's what I wrote:

RewriteCond %{HTTP_HOST} !^(www\.)?example\.com$
RewriteCond %{HTTP_HOST} ^(?!.*(/de|/en))
RewriteRule .* http://example.com/ [L,R=301]

... is there a better way to achieve that?

Thanks!! Urs

Urs
  • 4,984
  • 7
  • 54
  • 116
  • `the URL Path is longer than the domain only`. What do you mean by that? Cause mod_rewrite can't check text-length. – Gerben Feb 12 '12 at 13:03

1 Answers1

2

I am assuming that you did a "mindfart" and the second cond should read %{REQUEST_URI} otherwise it makes no sense as HTTP_HOST will never include a /

In runtime it makes very little difference, but it is easier to understand if written as

RewriteCond %{REQUEST_URI} !(/de|/en)
TerryE
  • 10,724
  • 5
  • 26
  • 48
  • I've reflected again on what I want to do, it's even simpler than what I sketched above: If REQUEST_URI doesn't start with /de or /en, add /de in front of it. Can you help me do that? (and also maybe point me to a good mod_rewrite tutorial)? – Urs Feb 13 '12 at 21:27
  • Use what is called a negative lookahead assertion on the rule pattern, \(described [here](http://www.regular-expressions.info/lookaround.html)\) E.g. `RewriteRule ^(?!de/)(?!en/).* - [L]`. (no leading `/` on rule patterns in `.htaccess` files). Use [this script](http://stackoverflow.com/questions/9153262/tips-for-debugging-htaccess-rewrite-rules/9261963#9261963) to play with rule patterns. As to tutorials, there's this thing called Google. I learn't them before Google existed. LoL. Enjoy!! – TerryE Feb 13 '12 at 23:27