.htaccess RewriteRule 国家代码和URLs

5
我正在使用以下的 .htaccess 代码:
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^ca/(.+)$ /index.php?p=$1&c=ca [L,QSA]
RewriteRule ^fr/(.+)$ /index.php?p=$1&c=fr [L,QSA]
RewriteRule ^(.+)$ /index.php?p=$1 [L,QSA]

为了实现以下效果:
http://xyz.com/ca/test -> http://xyz.com/index.php?p=test&c=ca
http://xyz.com/fr/test -> http://xyz.com/index.php?p=test&c=fr
http://xyz.com/test    -> http://xyz.com/index.php?p=test

但是它遇到了服务器错误。有什么解决办法吗?
谢谢
1个回答

3

RewriteCond 条件仅适用于紧随其后的 RewriteRule。您的最后两条规则没有任何条件,并且规则正在循环。只需在最后两条规则前添加这两个条件:

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^ca/(.+)$ /index.php?p=$1&c=ca [L,QSA]

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^fr/(.+)$ /index.php?p=$1&c=fr [L,QSA]

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

搞定了!话说,在这种情况下,http://xyz.com/ca 会转换为 http://xyz.com/index.php?p=ca&c=。我该如何处理这种情况? - Edward
它能被重写以便用于任何国家代码吗?我不想为每个国家代码创建一个庞大的htaccess文件。 - andrebruton

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接