views:

31

answers:

2

I am trying to find the correct mod_rewrite code for the following case:

domain.de => www.domain.com/de/
domain.de/... => www.domain.com/...
www.domain.de => www.domain.com/de/
www.domain.de/... => www.domain.com/...

domain.com => www.domain.com/en/
domain.com/... => www.domain.com/...
www.domain.com => www.domain.com/en/
www.domain.com/... => www.domain.com/...

so basically

  1. all non-www domains should be redirect to www.
  2. all uri's ending on .de or .de/ should be redirected to www.domain.com/de/
  3. but if somebody enters something after the .de/ that should be simply appended to www.domain.com/...

Can anyone think of a clever solution for this? I have been struggling with {REQUEST_URI} and {REQUEST_FILENAME} trying to figure out if the requested URI ends on .de or .de/ but couldn't find a working solution...

A: 

Check some examples here: http://www.thesitewizard.com/apache/redirect-domain-www-subdomain.shtml

and here: http://howto.kryl.info/mod_rewrite/

to start:

RewriteCond %{HTTP_HOST} ^domain.de$ [NC]

RewriteRule ^(.*)$ http://www.domain.com/de/$1 [R=301,L]

etc

DmitryK
A: 

Try this:

RewriteCond %{HTTP_HOST} ^(www\.)?example\.de$
RewriteRule ^$ http://www.example.com/de/ [L,R=301]
RewriteCond %{HTTP_HOST} ^(www\.)?example\.de$
RewriteRule . http://www.example.com%{REQUEST_URI} [L,R=301]
RewriteCond %{HTTP_HOST} ^(www\.)?example\.com$
RewriteRule ^$ http://www.example.com/en/ [L,R=301]
RewriteCond %{HTTP_HOST} ^example\.com$
RewriteRule . http://www.example.com%{REQUEST_URI} [L,R=301]
Gumbo
hi gumbo, thanks! that is exactly what i was looking for. can you tell me what the difference between ^$ and . in the rewriterule is?
Frank
`^$` matches just the URL path `/` while `.` matches anything else (`/` plus more). This is because when mod_rewrite is used in a .htaccess file, the per-directory path prefix is removed before testing and appended after applying a rule. And the path prefix of the root directory is `/`.
Gumbo