简体   繁体   English

将所有请求重定向到公共文件夹中的 index.php 文件

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

I am facing issue while redirecting all request to index.php file which is placed in public folder.我在将所有请求重定向到位于公共文件夹中的 index.php 文件时遇到问题。

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

There are two .htaccess files , First is Name_of_app/.htaccess file , this should redirect all traffic to Name_of_app/public folder有两个 .htaccess 文件,第一个是 Name_of_app/.htaccess 文件,这应该将所有流量重定向到 Name_of_app/public 文件夹

RewriteEngine On

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

Second is Name_of_app/public/.htaccess file that redirects all traffic to /public/index.php file第二个是 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]

Issues are:问题是:
1. I can't access /public/index.php file from url www.name_of_app.com/ . 1. 我无法从 url www.name_of_app.com/ 访问 /public/index.php 文件。
2. www.name_of_app.com/index.php shows /public/index.php page not found error 2. www.name_of_app.com/index.php 显示 /public/index.php page not found 错误

Help me in resolving this issue.Thanks .帮我解决这个问题。谢谢。

Delete /public/.htaccess .删除/public/.htaccess In /.htaccess put this:/.htaccess

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

Your PHP app should then parse $_SERVER['REQUEST_URI'] to see the URL as it was sent.然后,您的 PHP 应用程序应解析$_SERVER['REQUEST_URI']以查看发送的 URL。 Use any query within it to overwrite the contents of $_GET , then regenerate $_REQUEST from $_GET then $_POST .使用其中的任何查询覆盖$_GET的内容,然后从$_GET然后$_POST重新生成$_REQUEST An easy way is like so:一个简单的方法是这样的:

$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 '/'

You may not need RewriteBase .您可能不需要RewriteBase

Try the following code for Name_of_app/public/.htaccess为 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