【问题标题】:.htaccess RewriteRule country codes and urls.htaccess RewriteRule 国家代码和网址
【发布时间】:2012-08-23 16:05:46
【问题描述】:

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

但它因服务器错误而失败。关于如何解决它的任何想法?

谢谢

【问题讨论】:

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


    【解决方案1】:

    RewriteCond 条件仅适用于紧跟条件的RewriteRule。您的最后两条规则没有任何条件,并且规则正在循环。只需在最后 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]
    

    【讨论】:

    • 解决了!也就是说,xyz.com/ca 在这种情况下转换为xyz.com/index.php?p=ca&c=。我该如何处理这个案子?
    • 是否可以重写,以便用于任何国家/地区代码。我不想为每个国家/地区代码创建一个巨大的 htaccess 文件。
    猜你喜欢
    • 2016-07-20
    • 1970-01-01
    • 1970-01-01
    • 2014-10-21
    • 2012-01-10
    • 2010-11-16
    • 1970-01-01
    • 2018-10-16
    • 1970-01-01
    相关资源
    最近更新 更多