【问题标题】:How to rewrite requests to index.php to parent directory如何将 index.php 的请求重写到父目录
【发布时间】:2021-03-26 15:40:31
【问题描述】:

我有一个网站,其中包含两个 .htaccess 文件,一个在项目根目录中,一个在 /Project Root/Public/ 目录中,我想将直接请求重写为“http://localhost/Project Root/index.php " 到 "http://localhost/Project Root/" 我该怎么做呢?

我当前的 .htaccess(s):

项目根目录.htaccess:

RewriteEngine on
RewriteCond %{REQUEST_URI} !^public
RewriteRule ^(.*)$ public/$1 [L]

/项目根目录/Public/.htaccess

RewriteEngine On

RewriteCond %{THE_REQUEST} \s(?:/+(.+/))?public/ [NC]
RewriteRule ^ /%1 [L,R=301,NE]

DirectoryIndex index.php

<IfModule mod_negotiation.c>
    Options -MultiViews
</IfModule>

<IfModule mod_rewrite.c>
    RewriteEngine On

    RewriteCond %{REQUEST_URI}::$0 ^(/.+)/(.*)::\2$
    RewriteRule .* - [E=BASE:%1]

    RewriteCond %{HTTP:Authorization} .+
    RewriteRule ^ - [E=HTTP_AUTHORIZATION:%0]

    RewriteCond %{ENV:REDIRECT_STATUS} =""
    RewriteRule ^index\.php(?:/(.*)|$) %{ENV:BASE}/$1 [R=301,L]

    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteRule ^ %{ENV:BASE}/index.php [L]
</IfModule>

<IfModule !mod_rewrite.c>
    <IfModule mod_alias.c>
        RedirectMatch 307 ^/$ /index.php/
    </IfModule>
</IfModule>

【问题讨论】:

    标签: regex apache .htaccess symfony mod-rewrite


    【解决方案1】:

    让你的Project Root .htaccess 像这样:

    RewriteEngine On
    
    RewriteCond %{THE_REQUEST} \s/+([^/]+/)?index\.php[?\s] [NC]
    RewriteRule ^ /%1 [L,R=301,NC,NE]
    
    RewriteRule .* public/$0 [L]
    

    确保在测试前清除浏览器缓存。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-07-13
      • 1970-01-01
      • 1970-01-01
      • 2014-07-08
      • 2021-01-30
      • 1970-01-01
      相关资源
      最近更新 更多