.htaccess语言的两个域忽略文件夹

qncylg1j  于 2022-11-16  发布在  其他
关注(0)|答案(1)|浏览(117)

给定此目录结构:

.
├── assets
│   └── img/
│       └── foo.png
├── en
│   └── index.php
└── fr
    └── index.php

有两个域:

english.com
  french.com

我的目标是

english.com => english.com/en
french.com => french.com/fr
english.com/fr => french.com/fr
french.com/en => english.com/en

没有重定向:

english.com/assets/img/foo.png
   french.com/assets/img/foo.png

当前的解决方案无法正确实现上述目标

<IfModule mod_rewrite.c>
    RewriteEngine On

    # no www, https only
    RewriteCond %{SERVER_PORT} !^443$ [OR]
    RewriteCond %{HTTP_HOST} ^www\.%{HTTP_HOST}$ 
    RewriteRule (.*) https://%{HTTP_HOST}/$1 [R=301,L]

    RewriteRule ^(en) - [L]
    RewriteRule ^(fr) - [L]
    RewriteRule ^(assets) - [L]

    redirectMatch 301 ^/en$ https://english.com/en
    redirectMatch 301 ^/fr$ https://french.com/fr

    RewriteCond %{HTTP_HOST}: englisch.com$ [NC]
    RewriteRule ^(.*)$ https://%{HTTP_HOST}/en/$1 [R=301,L]
  
    RewriteCond %{HTTP_HOST} french.com$ [NC]
    RewriteRule ^(.*)$ https://french.com/fr/$1 [R=301,L]
</IfModule>
dxxyhpgq

dxxyhpgq1#

有这样的话:

RewriteEngine On

# no www, https only
RewriteCond %{HTTP_HOST} ^www\. [NC,OR]
RewriteCond %{HTTPS} !on
RewriteCond %{HTTP_HOST} ^(?:www\.)?(.+)$ [NC]
RewriteRule ^ https://%1%{REQUEST_URI} [R=302,L,NE]

# ignore all rules for anything in assets/ directory
RewriteRule ^assets/ - [L,NC]

# for fr/ host name should be french.com
RewriteCond %{HTTP_HOST} english\.com$ [NC]
RewriteRule ^fr/ https://french.com%{REQUEST_URI} [R=302,L,NE]

# for en/ host name should be english.com
RewriteCond %{HTTP_HOST} french\.com$ [NC]
RewriteRule ^en/ https://eglish.com%{REQUEST_URI} [R=302,L,NE]

# for english.com URI must start with en/
RewriteCond %{HTTP_HOST} english\.com$ [NC]
RewriteRule !^en/ /en%{REQUEST_URI} [R=302,L,NE]

# for french.com URI must start with fr/
RewriteCond %{HTTP_HOST} french\.com$ [NC]
RewriteRule !^fr/ /fr%{REQUEST_URI} [R=302,L,NE]

验证它工作正常后,将R=302替换为R=301。测试mod_rewrite规则时,避免使用R=301(永久重定向)。

相关问题