【问题标题】:rewrite url in .htacess not working in php在.htaccess中重写url在php中不起作用
【发布时间】:2014-07-15 10:32:25
【问题描述】:

我正在使用 php 和 apache 服务器,我想将 sites/all/themes/chap2014/fpd/gift.php 重写为 fdp/giftsites/all/themes/chap2014/fpd/another.php fdp/another 正是我想要这样做

sites/all/themes/chap2014/fpd/*.php重写为fpd/*

启用重写模式,我在.htacess 文件中尝试以下代码。

RewriteEngine on
RewriteRule ^fpd/?$  sites/all/themes/chap2014/fpd/$1.php

但什么也没发生,实现这一目标的正确方法是什么?

感谢任何帮助,

【问题讨论】:

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


    【解决方案1】:

    您需要先在 URI 模式的 LHS 上对部分 URI 进行分组和捕获,然后才能在 RHS 上使用$1$2 等。

    RewriteEngine on
    
    RewriteRule ^fpd/([^/]+)/?$ /sites/all/themes/chap2014/fpd/$1.php [L,NC]
    

    PS:在您的问题中,您同时使用了fpdfdp。不知道哪个是对的。

    【讨论】:

    • 我尝试了这个但什么也没发生` RewriteRule 上的 RewriteEngine ^fpd/([^/]+)/?$ /sites/all/themes/chap2014/fpd/$1.php [L,NC] `
    • 我在martinmelin.se/rewrite-rule-tester 中使用此网址sites/all/themes/chap2014/fpd/tshirt.php 测试您的规则,但被告知不匹配
    • 1.切勿在该站点上进行测试,因为它不可靠。 2. 您需要输入您的 URI 为 http://example.com/fpd/tshirt.php,它将在内部转发到 http://example.com/sites/all/themes/chap2014/fpd/tshirt.php
    • 重写后有问题,请帮忙stackoverflow.com/questions/24763316/…
    【解决方案2】:

    试试这个:

    RewriteRule ^fpd/(.+)$ sites/all/themes/chap2014/fpd/$1.php

    您可能希望排除 / 符号,如果是,则将 .+ 替换为 [^/]+

    你必须用()将你想要放入变量的部分括起来,每个封闭的部分将被放入$1、$2、$3等等。

    另外,这是一个测试重写规则的好工具:

    http://martinmelin.se/rewrite-rule-tester/

    【讨论】:

    • 我在你建议的测试器中尝试,用 url sites/all/themes/chap2014/fpd/tshirt.php 返回不匹配,
    • @zhilevan 它不应该返回任何东西。在测试器中输入fpd/tshirt,你会看到它被重写为sites/all/themes/chap2014/fpd/tshirt.php。毕竟,这就是你的 URL 中域之后的内容
    猜你喜欢
    • 2014-06-06
    • 2015-09-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-04-04
    • 2014-12-26
    • 2015-04-15
    相关资源
    最近更新 更多