【问题标题】:htaccess url rewrite with multiple variables in urlhtaccess url用url中的多个变量重写
【发布时间】:2011-06-23 06:08:56
【问题描述】:

我想在我的 .htaccess 文件上制定一些 url 重写规则,以便此链接:http://myseite.com/index.php?var1=value1&var2=value2 将变为:http://myseite.com/var1/value2.html

到目前为止,我已经成功解决了这个问题,但只是针对一个变量。

我也试过这个代码:

RewriteRule ^([^/]*)/([^/]*)\.html$ /index.php?var1=$1&var2=$2 [L]

但它不起作用..

感谢您的帮助!

【问题讨论】:

    标签: html .htaccess url-rewriting


    【解决方案1】:

    请求 http://mysite.com/var1/value2.html 被重写为 http://mysite.com/index.php?var1=var1&var2=var2

    index.php 和 .htaccess 文件位于文档根目录

    .htaccess 文件只有在 httpd.conf(或某些 apache conf 文件)为您正在工作的 DocumentRoot 设置了“AllowOverride All”时才有效。首先检查一下。

    接下来,确保您在 Apache conf 文件中启用了 mod_rewrite(更改 conf 文件后重新启动网络服务器),然后在您的 .htaccess 文件中启用它

    .htaccess 的内容:

    <IfModule mod_rewrite.c>
    RewriteEngine On
    # /var1/value2.html to /index.php?var1=value1&var2=value2
    RewriteRule ^([^/]+)/([^\.]+)\.html$ /index.php?var1=$1&var2=$2
    </IfModule>
    

    把它放在 /index.php 中看看它是否工作:

    <? print_r($_GET); ?>
    

    【讨论】:

    • 它不起作用:(,我尝试了丑陋的链接,只有那些有效..如果我尝试 value1/value2.html 它不起作用..它说找不到页面跨度>
    • 我在发布之前确实在真实服务器上运行过它,所以它对我有用。
    猜你喜欢
    • 1970-01-01
    • 2017-10-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-06-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多