【问题标题】:How to make a htacess RewriteRule with a url variable如何使用 url 变量创建 htaccess 重写规则
【发布时间】:2012-08-15 16:17:37
【问题描述】:

我想将网址http://www.mywebsite.com/tutos/tutos.php?q=my-tuto/tuto-1.html 重定向到 http://www.mywebsite.com/tutos/tutos/my-tuto/tuto-1.html,如何使用 .htaccess 来做到这一点?

我试过了,但它不起作用...:

RewriteRule ^http://www.mywebsite.com/tutos/([^-]*)$ http://www.mywebsite.com/tutos/tutos.php?q=$1 [L,QSA]

谢谢!

【问题讨论】:

    标签: .htaccess url redirect web rewrite


    【解决方案1】:

    您所拥有的规则似乎与您所说的相反。它将非查询字符串 URL 重定向到查询字符串 URL。但如果这是您想要的,您需要从正则表达式中删除主机和协议。只有 URI(无查询字符串)用于匹配 RewriteRule

    RewriteRule ^tutos/([^-]*)$ /tutos/tutos.php?q=$1 [L,QSA,R=301]
    

    但是,如果您希望像您所要求的那样反过来:

    RewriteCond %{QUERY_STRING} (.*)(^|&)q=([^&]+)(.*)
    RewriteRule ^tutos/tutos.php$ /tutos/%2?%1%3 [L,R=301]
    

    【讨论】:

    • 非常感谢您的帮助
    猜你喜欢
    • 2011-12-06
    • 2016-11-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-05-18
    相关资源
    最近更新 更多