.htaccess重写语言重定向,页面的两个版本,保留地址

时间:2012-08-16 13:29:15

标签: .htaccess mod-rewrite redirect

希望有人可以给我一个提示。

我的意思是重定向访问,具体取决于浏览器语言和输入的网址(是否包含/ v2)。我想将原始URL保留在地址栏中,所以我理解我只能使用内部apache重定向,而不是301.

URL            BROWSER LANGUAGE    INTERNAL REDIRECTION
domain.com           en                 en.html
domain.com/v2        en                 en_v2.html
domain.com           es                 es.html
domain.com/v2        es                 es_v2.html
domain.com           other              en.html
domain.com/v2        other              en_v2.html

所以这就是我在.htaccess中试过的:

#mod_rewrite on
RewriteEngine on

#Default language redirection
RewriteRule ^.*$ en.html
RewriteRule ^.*?v2$ en_v2.html

#Redirection by Language en
RewriteCond %{HTTP:Accept-Language} ^en [NC]
RewriteRule ^.*$ en.html
RewriteCond %{HTTP:Accept-Language} ^en [NC]
RewriteRule ^.*?v2$ en_v2.html

#Redirection by Language es
RewriteCond %{HTTP:Accept-Language} ^es [NC]
RewriteRule ^.*$ es.html
RewriteCond %{HTTP:Accept-Language} ^es [NC]
RewriteRule ^.*?v2$ es_v2.html [L]

所有html文件都在根文件夹中,与.htaccess文件相同。通过这种方式,我试图让一些重写“覆盖”以前的重写,例如

RewriteRule ^.*$ es.html
匹配所有内容的

将被

覆盖
RewriteRule ^.*?v2$ es_v2.html

以防/ v2在URL中。我不确定它的效果如何。

有人可以帮助我吗?

编辑:已解决,这是最终版本:

#mod_rewrite on
RewriteEngine on

#Allow direct access to html files
RewriteRule ^en(_v2)?\.html - [L]
RewriteRule ^es(_v2)?\.html - [L]

#Without this the css and other files are redirected and won't load properly
RewriteRule \.(gif|jpe?g|png|js|css|swf|php|ico|txt|pdf|xml|eot|svg|ttf|woff)$ - [L]

#Redirection by Language
RewriteCond %{HTTP:Accept-Language} (es) [NC]
RewriteRule ^v2$ es_v2.html [L]

#Redirection by Language
RewriteCond %{HTTP:Accept-Language} (es) [NC]
RewriteRule .* es.html [L]

#Defaults
RewriteRule ^v2$ en_v2.html [L]
RewriteRule .* en.html [L]

1 个答案:

答案 0 :(得分:1)

例如。

#mod_rewrite on
RewriteEngine on

RewriteRule ^en(_v2)?\.html - [L]

#Redirection by Language en
RewriteCond %{HTTP:Accept-Language} ^en [NC]
RewriteRule ^.*?v2$ en_v2.html [L]
RewriteCond %{HTTP:Accept-Language} ^en [NC]
RewriteRule ^.*$ en.html [L]

#Redirection by Language es
RewriteCond %{HTTP:Accept-Language} ^es [NC]
RewriteRule ^.*?v2$ es_v2.html [L]
RewriteCond %{HTTP:Accept-Language} ^es [NC]
RewriteRule ^.*$ es.html [L]

#Default language redirection
RewriteRule ^.*?v2$ en_v2.html [L]
RewriteRule ^.*$ en.html [L]