【问题标题】:Htaccess rules get overridenHtaccess 规则被覆盖
【发布时间】:2018-11-18 11:44:41
【问题描述】:

我希望 Web 服务器根目录中的 .htaccess 将任何以 ^assets/(.*)$ 开头的请求重定向到文件夹 public/$1。所有其他请求都必须转到index.php?request=$1。在这种情况下,我还想保留获取参数(L 标志)。

RewriteEngine on

RewriteBase /

RewriteRule ^assets/(.*)$ public/$1 [QSA,L]

RewriteRule ^(?!assets)(.*)$ index.php?request=$1 [L,QSA]

唯一的问题是最后一行覆盖了前一行:/ 我还用(.*) 进行了测试,但没有成功。 感谢您的帮助。

【问题讨论】:

    标签: apache .htaccess mod-rewrite


    【解决方案1】:

    试试这个:

    RewriteEngine on
    
    RewriteRule "^/assets/(.*)$" "/public/$1" [L,QSA]
    
    RewriteCond %{REQUEST_URI} "!^/assets/"
    RewriteCond %{REQUEST_URI} "!^/index.php"
    RewriteCond %{REQUEST_URI} "!^/public"
    RewriteRule (.*) "/index.php?request=$1" [L,QSA]
    
    • QSA:将参数追加到重写部分
    • L:最后。这意味着如果匹配,则不处理任何其他重写。
    • 重写从/ 开始
    • 要对 RewriteRule 应用条件,请使用 RewriteCond

    参考资料:
    https://httpd.apache.org/docs/2.4/rewrite/flags.html
    https://httpd.apache.org/docs/current/mod/mod_rewrite.html

    第一条规则说:

    • 如果它以“/assets/”开头,则第二个/ 之后的任何内容都将被重写为“/public/$1”。

    RewriteCond 和 RewriteRule 说:

    • 如果它不是以“/assets/”开头,则将收到的任何内容添加到“/index.php?request=”的末尾。
      所以http://www.example.com/directory/somepage.html 将转到
      http://www.example.com/index.php?request=/directory/somepage.html
    • 如果请求中有参数,则将它们附加在末尾,如下所示: http://www.example.com/directory/somepage.html?a=1&b=4 将转到
      http://www.example.com/index.php?request=/directory/somepage.html?a=1&b=4

    编辑 1115 年 11 月 19 日

    添加了第二个 RewriteCond 以确保“/index.php”不会重写自身。第三个,如果您希望“/public”也不重定向到“/index.php”。

    【讨论】:

    • 不起作用,由于配置错误,我收到 500 错误。这与 php 无关,因为即使它不是我想要的,它也适用于我的版本。
    • 请提供更多细节。你试过什么网址?哪些不起作用?将print_r($GET); 放入您的 index.php 以查看您收到了什么,您在 Apache 中获得了哪些日志?我猜不出来。
    • 这是你的脚本在任何情况下给我的错误AH00124: Request exceeded the limit of 10 internal redirects due to probable configuration error. Use 'LimitInternalRecursion' to increase the limit if necessary. Use 'LogLevel debug' to get a backtrace. 但是,我的正确写入了 $_GET 数组,请求与这部分 url 匹配:any.com**any/any/any ?a=1&b=2** 和正确的其他获取参数为 ab。唯一的问题是,如果 url 以 assets 开头,它永远不会重定向,因为最后一行会覆盖前一行。
    • 好吧,我想通了,对不起。我添加了第二个 RewriteCond 以避免“/index.php”重写到自身。 “/公共”呢?是否应该转到“index.php”?如果您需要,我修改了 asnwer。
    • 我不关心 /public 最重要的是其余的工作。我认为在调用 public 时不重写会很好。
    猜你喜欢
    • 1970-01-01
    • 2017-11-19
    • 1970-01-01
    • 1970-01-01
    • 2012-01-16
    • 2013-09-17
    • 1970-01-01
    • 2023-03-25
    • 1970-01-01
    相关资源
    最近更新 更多