我有一个域名example.com
,它在example.com/en-US/
上托管一个英文版的网站,在example.com/fr-FR/
上托管一个法语版
的网站到目前为止,我们一直使用英文版本作为默认值,因此我们会将请求example.com
的每个人重定向到example.com/en-US/
.htaccess
文件中使用这些规则:
Redirect 301 /index.html http://examle.com/en/
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} !(.*)/$
RewriteRule ^(.*)$ http://example.com/$1/ [L,R=301]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} ^(.*)//(.*)$
RewriteRule . %1/%2 [R=301,L]
因此,当您在浏览器中请求 http://exmaple.com 时,服务器将请求索引.html这将触发 Rediret 到/en-US/
目录。
现在我们得到了法语example.fr
TLD - 指向相同的网络空间。我现在正在寻找一种方法来重新设置浏览器请求 http://example.fr 以 http://example.com/fr-FR/
我尝试添加以下条件,但它会导致某种循环将大量/en-US/
添加到 URL 中。
请帮助我,我需要一种方法来重定向
hxxp://example.com --> hxxp://example.com/en-US/
hxxp://example.fr --> hxxp://example.com/fr-FR/
以及所有明确要求 hxxp://example.com/index.html 的人 -> hxxp://example.com/en-US/
您可以根据基于HTTP_HOST
的条件使用此规则:
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} ^(?:www.)?example.com$ [NC]
RewriteRule ^(?:index.html)?$ /en-US/ [L,NC,R=301]
RewriteCond %{HTTP_HOST} ^(?:www.)?example.fr$ [NC]
RewriteRule ^(?:index.html)?$ /fr-FR/ [L,NC,R=301]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} !/$
RewriteRule ^ %{REQUEST_URI}/ [L,R=301,NE]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} ^(.*)//(.*)$
RewriteRule . %1/%2 [R=301,L,NE]