mod_rewrite具有相对路径

时间:2015-10-13 08:22:44

标签: regex apache .htaccess mod-rewrite

我的以下.htaccess在我的本地环境中正常运行。它位于http://myexample.com/subdirectory-1/subdirectory-2/。但是我希望它也可以在我的网络服务器上工作,显然它有一个不同的域。任何人都可以帮助调整以下代码以便在任何域上工作吗?

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]

1 个答案:

答案 0 :(得分:1)

您可以使用动态RewriteBase并在localhost和实时服务器上使用相同的规则集:

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]