0

我有一个使用“语言”URI 属性来设置语言的网站

oursite.com/home?language=en

我们还需要能够使用对 SEO 友好的 2 字符 URI 前置程序

oursite.com/en/home

我目前正在重定向到 .htaccess 中的 index.php 文件,但有一些例外,所以我的 .htaccess 有这两行用于重写

RewriteRule ^([a-z]{2}|[a-z]{2}-[A-Z]{2})/(.*)? ?$2/language=$1
RewriteRule !^(index\.php|robots\.txt|sitemap\.xml|robots\.txt) /index.php?/$1  

我需要进行逻辑重写以结束

/index.php?/home?language=en

什么是正确的重写规则集来实现这一点?真的有可能吗?

4

2 回答 2

3

RewriteRule !^(index\.php|robots\.txt|sitemap\.xml|robots\.txt) /index.php?/$1行不起作用,因为您无法创建对否定匹配的反向引用。

此外,像: 这样的请求/index.php?/home?language=en有点模棱两可,?是保留的,需要在查询字符串中编码,否则,可以附加它(这样?就变成了 a &)。尝试类似:

RewriteRule ^([a-z]{2}|[a-z]{2}-[A-Z]{2})/(.*)? /$2?language=$1 [L]
RewriteRule !^(index\.php|robots\.txt|sitemap\.xml|robots\.txt) /index.php?%{REQUEST_URI} [L,QSA]

这需要:http://oursite.com/en/home并在内部将其重写为 URI /index.php?/home&language=en。但是,如果您确实想要?在查询字符串中进行编码,则将第二条规则更改为:

RewriteRule !^(index\.php|robots\.txt|sitemap\.xml|robots\.txt) /index.php?%{REQUEST_URI}\%3F%{QUERY_STRING} [L,NE]
于 2012-12-04T08:00:54.080 回答
0

您可以在第一条规则中执行此操作。

RewriteRule ^([a-z]{2}|[a-z]{2}-[A-Z]{2})/(.*)? /index.php?/$2?language=$1

至于第二条规则(对于没有语言前缀的 url),你可以这样做。正如乔恩所说,您不能反向引用否定匹配。

RewiteCond $1 !^index\.php|robots\.txt|sitemap\.xml|robots\.txt
RewriteRule (.*) /index.php?/$1
于 2012-12-09T20:32:01.390 回答