apache - .htaccess RewriteRule 国家/地区代码和 url

标签 apache .htaccess url mod-rewrite url-rewriting

我正在使用以下 .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

但由于服务器错误而失败。关于如何修复它有什么想法吗?

谢谢

最佳答案

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

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]

关于apache - .htaccess RewriteRule 国家/地区代码和 url,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12171282/

相关文章:

java - 包含 Apache POI 时的 Android 重复类错误

apache - 图像和 CSS 的 URL 重写

javascript - 定义仅在运行 javascript 函数后附加的 URL 的最佳实践

java - 在 JAX-RS 服务中获取 XML

apache - fsimage hdfs 的内容

java - Maven 和 Java_Home

php - 如何使用纯 HTTP 身份验证和 PHP 在 Apache 下获取经过身份验证的用户名?

php - htaccess 为带或不带尾部斜杠的 url 添加 .html 扩展名

php - htaccess 文件中使用 cdn 重定向 url

java - 忽略过期的 ssl 证书