user1706680
user1706680

Reputation: 1111

mod_rewrite with relative paths

My following .htaccess works fine in my local environment. It is located in http://myexample.com/subdirectory-1/subdirectory-2/. However I want it to also work on my webserver which obviously has a different domain. Can anybody help to adjust the below code to make in work on any domain?

Options +FollowSymlinks
RewriteEngine On

# If cookie 'de'
RewriteCond %{HTTP_COOKIE} de [NC]
RewriteRule ^$ http://myexample.com/subdirectory-1/subdirectory-2/de [R=301]

# If cookie 'en'
RewriteCond %{HTTP_COOKIE} en [NC]
RewriteRule ^$ http://myexample.com/subdirectory-1/subdirectory-2/en [R=301]

# If browser language 'de'
RewriteCond %{HTTP:Accept-Language} ^de [NC]
RewriteRule ^$ http://myexample.com/subdirectory-1/subdirectory-2/de [R=301]

# Else
RewriteRule ^$ http://myexample.com/subdirectory-1/subdirectory-2/en [R=301]

Upvotes: 2

Views: 33

Answers (1)

anubhava
anubhava

Reputation: 785376

You can use a dynamic RewriteBase and use same set of rules both in localhost and on live server:

RewriteEngine On

# generate rewritebase and store in BASE env variable
RewriteCond $0#%{REQUEST_URI} ^([^#]*)#(.*)\1$
RewriteRule ^.*$ - [E=BASE:%2]

# If cookie 'en' or 'de'
RewriteCond %{HTTP_COOKIE} (en|de) [NC]
RewriteRule ^$ %{ENV:BASE}%1 [L,R=301]

# If browser language 'de'
RewriteCond %{HTTP:Accept-Language} ^de [NC]
RewriteRule ^$ %{ENV:BASE}de [R=301]

# Else
RewriteRule ^$ %{ENV:BASE}en [L,R=301]

Upvotes: 1

Related Questions