【问题标题】:htaccess redirect specific TLD to subfolder while preserving existing root-to-subfolder redirecthtaccess 将特定 TLD 重定向到子文件夹,同时保留现有的根到子文件夹重定向
【发布时间】:2018-05-28 11:02:23
【问题描述】:

我有一个域名example.com,它在example.com/en-US/ 上托管一个英文版网站,在example.com/fr-FR/ 上托管一个法文版

到目前为止,我们默认使用英文版本,因此我们会使用 .htaccess 文件中的这些规则将所有请求 example.com 的人重定向到 example.com/en-US/

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 时,服务器将请求 index.html,这将触发重定向到 /en-US/ 目录。

现在我们得到了法语example.fr TLD - 指向同一个网络空间。我现在正在寻找一种将浏览器请求 http://example.fr 重定向到 http://example.com/fr-FR/ 的方法

我已尝试添加以下条件,但它会导致某种循环在 URL 中添加大量 /en-US/s。

请帮帮我,我需要重定向的方法

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/ 的人

【问题讨论】:

    标签: regex apache .htaccess redirect url-rewriting


    【解决方案1】:

    您可以根据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]
    

    【讨论】:

      猜你喜欢
      • 2015-10-06
      • 1970-01-01
      • 2016-06-27
      • 2017-12-05
      • 2017-09-08
      • 2014-06-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多