.htaccess用于Codeigniter的URI语言代码重写

时间:2012-12-03 23:58:20

标签: php codeigniter mod-rewrite

我的网站使用“语言”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

为实现这一目标,正确的重写规则集是什么?它真的可能吗?

2 个答案:

答案 0 :(得分:3)

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

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

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]

答案 1 :(得分:0)

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

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

至于第二条规则(对于没有语言前缀的网址),你可以这样做。正如乔恩所说,你无法反向引用负面匹配。

RewiteCond $1 !^index\.php|robots\.txt|sitemap\.xml|robots\.txt
RewriteRule (.*) /index.php?/$1