【问题标题】:How to alter REQUEST_URI using .htaccess?如何使用 .htaccess 更改 REQUEST_URI?
【发布时间】:2012-01-19 08:18:12
【问题描述】:

我有这个文件夹结构:

/www/project/web/app.php

我可以通过/project/web/index.php 访问它。问题是我不想将web/ 作为 URL 的一部分。应该是/project/index.php

/www/project/ 文件夹中使用此 .htaccess:

重写引擎开启

重写规则 ^(.*)$ web/index.php [L]

当通过/project/foo/bar 访问时,它似乎重新路由到正确的文件,但是,REQUEST_URI 保持不变并且是/project/index.php/foo/bar,这会破坏很多东西。

如何将 REQUEST_URI 更改为不包含 project

简单地说:

/www/project/.htaccess

RewriteEngine On
RewriteRule ^(.*)$ web/index.php [L]

访问 URL /project/foo/bar 正确地重新路由到 index.php,但应用程序失败,因为 REQUEST_URI 包含 /project/foo/bar 而不是 /foo/barproject 是一个文件夹,不应成为请求的一部分。

【问题讨论】:

    标签: apache .htaccess


    【解决方案1】:

    尝试将以下内容添加到您的/www/project/.htaccess

    RewriteEngine on
    RewriteBase /project/
    
    #first, if project/web or web or just project is present, redirect and remove them
    RewriteRule ^(project/web/|web/|project/)(.*)$ $2 [L,R=301]
    
    
    #next, rewrite all requests to web/index.php
    RewriteRule ^(.*)$ web/index.php [L]
    

    【讨论】:

      【解决方案2】:

      要更改 URI,您必须使用 R 标志进行重定向,您当前的规则只会在内部重定向,因此保持相同的 URI。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2013-09-23
        • 1970-01-01
        • 1970-01-01
        • 2016-07-13
        • 1970-01-01
        • 1970-01-01
        • 2023-03-10
        • 2019-05-21
        相关资源
        最近更新 更多