我用多种语言建立了一个网站,直到现在我对内部页面进行了简单的重写。
RewriteRule ^([^/]+)$ /page.php?id=$1 [L]
所以我所有的页面都是这样的:
website.com/about
但现在我添加了另一个规则,用于多种语言
RewriteRule ^/?(en|es|ar|fr|de|ru|pt-br)/(.+?)/?$ /$2&lang=$1 [L]
问题是,我有更多的页面,像索引页或个人资料页,像这样:
website.com/en/
website.com/en/profile.php
它们都不再工作了,因为它们将我重定向到不同的页面。像
page.php?id=en
page.php?id=profile.php&lang=en
我很困惑这个问题的解决方案是什么。
注意,我不希望我的主语言有一个前缀/he/
这是整个htaccess
RewriteEngine On
ErrorDocument 404 /error
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteCond %{SCRIPT_FILENAME} !-f
RewriteRule ^([^/]+)$ /page.php?id=$1 [L]
RewriteRule ^/?(en|es|ar|fr|de|ru|pt-br)/(.+?)/?$ /$2&lang=$1 [L]
我想要这些
website.com/
website.com/en/
website.com/en/profile.php
website.com/about
website.com/en/about
来匹配这些
website.com/
website.com/?lang=en
website.com/profile.php?lang=en
website.com/page.php?id=about
website.com/page.php?id=about&lang
不确定这是否是最好的方法,但它是有效的:
RewriteEngine On
ErrorDocument 404 /error
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteCond %{SCRIPT_FILENAME} !-f
RewriteRule ^(en|es|ar|fr|de|ru|pt-br)/?$ ?lang=$1 [L]
RewriteRule ^(en|es|ar|fr|de|ru|pt-br)/(.*).php /$2.php?lang=$1 [L]
RewriteRule ^(en|es|ar|fr|de|ru|pt-br)/?(.*) /page.php?id=$2&lang=$1 [L]
RewriteRule ^([^/]+)$ /page.php?id=$1 [L]
我用这个网站进行了测试