Musterknabe
Musterknabe

Reputation: 6081

Redirect a domain to a path but exclude some urls

I have two domains.

Let's just call them

example.com and example.de.

Now, when someone types example.com in the address he comes to my site example.com. However, if the user types in example.de he will be redirected to example.com/de. The same goes for paths:

example.de/a => example.com/de/a

What I want to achieve now, is, that "specific" urls shouldn't be redirected.

So, while

example.de/a should still redirect to example.com/de/a/
example.de/b/ should stay example.de/b/

So, currently my .htaccess, part of it, looks like this

<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /

RewriteCond %{http_host} example\.de [NC]
RewriteRule ^(.*)$ http://www.example.com/de/$1 [L,R=301]

Now, I want to exclude specific URLs so what I did so far were the following things

RewriteCond %{http_host} example\.de [NC]
RewriteCond %{REQUEST_URI} !/b
RewriteRule ^(.*)$ http://www.example.com/de/$1 [L,R=301]

The problem is, that .de/b still redirects to .com/de/b

Another part, which might be interesting for you is that in the last line of the .htaccess I redirect anything that doesn't end with .com to .com.

### SUBDOMAIN REDIRECT *.DOMAIN.TLD -> www.DOMAIN.TLD
RewriteCond %{http_host} !^www\.example\.com [NC]
RewriteRule ^(.*)$ http://www.example.com/$1 [L,R=301]

So, anyone any idea, what could solve this problem?

To respond to anubhava answer

I've changed my .htaccess now to this

### SUBDOMAIN REDIRECT *.DOMAIN.TLD -> www.DOMAIN.TLD
RewriteCond %{http_host} example\.de [NC]
RewriteCond %{THE_REQUEST} !/b
RewriteRule ^(.*)$ http://www.example.com/de/$1 [L,R=301]

### SUBDOMAIN REDIRECT *.DOMAIN.TLD -> www.DOMAIN.TLD
RewriteCond %{http_host} !^www\.example\.com [NC]
RewriteCond %{THE_REQUEST} !/b
RewriteRule ^(.*)$ http://www.example.com/$1 [L,R=301]

But the problem is still there. :(

Upvotes: 1

Views: 1437

Answers (1)

anubhava
anubhava

Reputation: 785196

You better use THE_REQUEST variable in your condition:

RewriteCond %{http_host} example\.de [NC]
RewriteCond %{THE_REQUEST} !/b
RewriteRule ^(.*)$ http://www.example.com/de/$1 [L,R=301]

THE_REQUEST variable represents original request received by Apache from your browser and it doesn't get overwritten after execution of some rewrite rules. Example value of this variable is GET /index.php?id=123 HTTP/1.1.

Upvotes: 1

Related Questions