.htaccess 使用htaccess和rewrite将目录添加到url

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

我有一个基于Modx CMS的网站,我需要创建一个301重定向的主文件夹和旧链接后,多语言功能已启用。
我需要实现的是默认回退到英语,例如,确保:
example.com/products example.com/en/productsexample.com/aboutexample.com/en/about〉
我还需要确保如果在url中已经有语言选择(例如de),我不会在url中添加en。(所以没有example.com/en/de/products)
我在将/en/添加到URL时遇到问题,并且URL上出现了infite /en/en/en循环
为了将/en/添加到URL,我尝试了以下方法。

RewriteCond %{REQUEST_URI} !^/de$
RewriteCond %{HTTP_HOST} .*example.com [NC]
RewriteRule ^https://example.com/en/%{REQUEST_URI} [L,R=301]

这会导致url中出现无休止的/en/en/en循环。
整个htaccess如下:

RewriteEngine On
RewriteCond %{HTTPS} !=on
RewriteRule ^.*$ https://%{SERVER_NAME}%{REQUEST_URI} [R,L]
RewriteBase /

RewriteCond %{REQUEST_URI} !^/de$
RewriteCond %{HTTP_HOST} .*example.com [NC]
RewriteRule ^https://example.com/en/%{REQUEST_URI} [L,R=301]

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?q=$1 [L,QSA]
sgtfey8w

sgtfey8w1#

尝试下面的代码。规则是链接的(默认为AND)。因此,如果你的URL不是以/en/de开头,它不是索引(/),也不是文件名或目录,请执行301重定向到英语版本:

RewriteEngine On

RewriteCond %{REQUEST_URI} !^/en
RewriteCond %{REQUEST_URI} !^/de
RewriteCond %{REQUEST_URI} !^/$
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^ https://%{HTTP_HOST}/en%{REQUEST_URI} [L,R=301]

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?q=$1 [L,QSA]

相关问题