[英]How-to RewriteRule?
我具有以下站点结构:
site.com
\about\
\contact\
\mail\
\ru\
\uk\
\lt\
\ru\o-stranice\
\ru\kontakty\
\ru\pochta\
\uk\storinka\
\uk\zvorotnij-zvyazok\
\uk\skrynka\
\lt\apie\
\lt\kontaktai\
\lt\pastas\
和.htaccess:
RewriteEngine On
# Force English
# for http://www.website.com/
RewriteRule ^([^/]*)/$ index.php?lang=en&article_id=$1
# Other Languages
# for http://www.website.com/ru/
RewriteRule ^(ru|uk|lt)/$ index.php?lang=$1&article_id=index
# Other Languages
# for http://www.website.com/ru/contact/ etc
RewriteRule ^(ru|uk|lt)/([^/]*)/$ /index.php?lang=$1&article_id=$2
变量$article_id
是唯一的。
http://site.com
http://site.com/about/
http://site.net/ru/o-stranice/
工作正常,但是
http://site.com/ru/
http://site.com/uk
http://site.com/lt
被解释为文件夹名称,因此
echo $_GET ['lang']; //gives en
echo $_GET ['article_id']; //gives "ru".
我应该如何纠正重写规则以显示ru
页面和index
值? 我希望网站结构对您来说更容易。
试试这些:
# http://site.com
# note: that this rule changes http://site.com to http://site.com/index.php?...
# which matches the next rule, the [L] flag should prevent that
RewriteRule ^$ index.php?lang=en&article_id=index [L]
# http://site.com/anything except ru,uk,lt
# http://site.com/anything except ru,uk,lt/
RewriteRule ^(?!ru|uk|lt)([^/]+)/?$ index.php?lang=en&article_id=$1
# http://site.com/ru,uk,lt
# http://site.com/ru,uk,lt/
RewriteRule ^(ru|uk|lt)/?$ index.php?lang=$1&article_id=index
# http://site.com/ru,uk,lt/anything
# http://site.com/ru,uk,lt/anything/
RewriteRule ^(ru|uk|lt)/([^/]+)/?$ index.php?lang=$1&article_id=$2
理想情况下,您应该决定是否允许以/
结尾的URL。 然后,说你选择有一个尾随/
,301重定向完全没有斜杠的请求都有-词shash网址。 我希望你明白我的意思。
您需要重新组织规则。 当您输入http://site.com/ru/
,Apache会将路径视为ru/
, 它与您的第一个规则(零个或多个不是正斜杠的字符,然后是正斜杠; 请检查 )相匹配。
组织重写规则时,通常的经验法则是首先列出最深/最详细的级别 。 由于它们是最详细的,因此您将遇到的问题更少。 在更详细的规则之前拥有较少的详细规则会增加在实际要触发的规则之前触发规则链的风险。
尝试简单地将它们全部替换为.htaccess文件:
RewriteEngine On
# Other Languages
# for http://www.website.com/ru/contact/ etc
RewriteRule ^(ru|uk|lt)/([^/]*)/?$ /index.php?lang=$1&article_id=$2
# Other Languages
# for http://www.website.com/ru/
RewriteRule ^(ru|uk|lt)/?$ index.php?lang=$1&article_id=index
# Force English
# for http://www.website.com/
RewriteRule ^([^/]*)/?$ index.php?lang=en&article_id=$1
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.