簡體   English   中英

將所有請求重定向到公共文件夾中的 index.php 文件

[英]Redirect all request to index.php file in public folder

我在將所有請求重定向到位於公共文件夾中的 index.php 文件時遇到問題。

Name_of_app
|-app
|  |--controllers/
|-config/
|  |--db.config
|-public/
|  |--js/
|  |--css/
|  |--index.php
|  |--.htaccess
|-vendor/
|-.htaccess
|-composer.json

有兩個 .htaccess 文件,第一個是 Name_of_app/.htaccess 文件,這應該將所有流量重定向到 Name_of_app/public 文件夾

RewriteEngine On

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

第二個是 Name_of_app/public/.htaccess 文件,它將所有流量重定向到 /public/index.php 文件

RewriteEngine On

# Some hosts may require you to use the `RewriteBase` directive.
# If you need to use the `RewriteBase` directive, it should be the
# absolute physical path to the directory that contains this htaccess file.
#
# RewriteBase /

RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [QSA,L]

問題是:
1. 我無法從 url www.name_of_app.com/ 訪問 /public/index.php 文件。
2. www.name_of_app.com/index.php 顯示 /public/index.php page not found 錯誤

幫我解決這個問題。謝謝。

刪除/public/.htaccess /.htaccess

RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(?!public/(?:index\.php)?$) public/index.php [L]

然后,您的 PHP 應用程序應解析$_SERVER['REQUEST_URI']以查看發送的 URL。 使用其中的任何查詢覆蓋$_GET的內容,然后從$_GET然后$_POST重新生成$_REQUEST 一個簡單的方法是這樣的:

$url_path = explode($_SERVER['REQUEST_URI'], '?', 2);
if (isset($url_path[1])) {
    parse_str($url_path[1], $_GET);
}
else {
    $_GET = [];
}
$url_path = (string) substr($url_path[0], 1);// ignore leading '/'

您可能不需要RewriteBase

為 Name_of_app/public/.htaccess 嘗試以下代碼

DirectoryIndex index.php

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

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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM