2012-08-16 72 views
0

希望有人可以給我這個提示。的.htaccess重寫語言重定向頁面的兩個版本,保持地址

我的意思是根據瀏覽器語言和輸入的URL(包含其中或不包含/ 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 。但我不確定它是如何工作的。

有人可以幫助我嗎?

EDIT:解決,這是最後的版本:

#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] 
+0

由於'en'與'other'相同,似乎最好將它視爲'es'和「世界其他地方」。處理'es',如果不是這樣,那麼處理其他所有事情。 – 2012-08-16 13:36:47

+0

當然!之後我添加了世界其餘的部分,但沒有再給它一個想法,你是對的。 – jrg 2012-08-17 14:50:57

回答

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] 
+0

謝謝!這幫助了很多,我編輯了最後的工作版本的原始帖子。 – jrg 2012-08-17 14:51:31