【问题标题】:htaccess multiple parameters rewrite rulehtaccess 多参数重写规则
【发布时间】:2013-12-26 10:25:54
【问题描述】:

我知道这个问题被问多了,但找不到任何适合我的问题。

我目前正在创建一个网站,我的网址是这样的:

www.foo.com/ 或 www.foo.com/index.php。 它们可以采用 1、2 或三个不同的参数,例如

www.foo.com/index.php?page=Home&lang=en&article=1

我想要的是一个像

这样的网址
   www.foo.com/Home/

or www.foo.com/en/Home

or www.foo.com/Article/1

or www.foo.com/en/Article/1

page参数是必须的,其他两个不是.. 我无法为我工作...任何帮助将不胜感激

非常感谢!

【问题讨论】:

    标签: php regex apache .htaccess mod-rewrite


    【解决方案1】:

    最好有单独的干净规则。将此代码放入您的 DOCUMENT_ROOT/.htaccess 文件中:

    RewriteEngine On
    
    RewriteRule ^index\.php$ - [L]
    
    RewriteRule ^([^/]+)/?$ /index.php?page=$1 [L,QSA]
    
    RewriteRule ^([a-z]{2})/([^/]+)/?$ /index.php?page=$2&lang=$1 [L,QSA]
    
    RewriteRule ^([a-z]{2})/([^/]+)/([0-9]+)/?$ /index.php?page=$2&lang=$1&article=$3 [L,QSA]
    
    RewriteRule ^([^/]+)/([0-9]+)/?$ /index.php?page=$1&article=$2 [L,QSA]
    

    【讨论】:

    • 在哪里可以了解有关这些 RewriteRules 的更多信息?我搜索了,但我没有找到很多资源。
    • @AvinashPatil: askapache.com/htaccess 有很好的教程。
    【解决方案2】:

    试试这样的

    RewriteRule ^([a-z0-9_-]+)/([a-z0-9_-]+)/([a-z0-9_-]+)\.html$           index.php?param1=$1&param2=$2&param3=$3
    

    【讨论】:

    • 我收到一个 500 内部错误,对不起,我对 htaccess 文件非常不满意
    • htaccess 文件中可能有错误。请粘贴您的 htaccess 文件以便我查看
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2018-02-03
    • 2023-04-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-02-15
    相关资源
    最近更新 更多