简体   繁体   中英

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.

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

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

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/ .
2. www.name_of_app.com/index.php shows /public/index.php page not found error

Help me in resolving this issue.Thanks .

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

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. Use any query within it to overwrite the contents of $_GET , then regenerate $_REQUEST from $_GET then $_POST . 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 .

Try the following code for 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>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM