简体   繁体   中英

Changing the root folder via .htaccess and ignore path

I want the public/ folder to be root but I want to ignore everything that comes in the url, but the files in the css, javascript, images folder are accessible.

Example:

http://localhost/ access to public/ folder

http://localhost/css/style.css or http://localhost/js/script.js or http://localhost/images/funny.gif is accessible

I want it when I type http://localhost/news/1?test=helloword

news/1?test=helloword need be ignored, but in PHP when using the parse_url() function I have to have access to all variables, like ['path'],['query;],...

Output:

['path'] => '/news/1'

['query'] => {
    ['test'] => 'helloword'
}

I'm using PHP to make the server php -S localhost:8000 -t public/ it works very well

but when i enter the apache2 server it doesn't work

I found this on the internet .htaccess

RewriteEngine On
RewriteBase /

RewriteCond %{THE_REQUEST} /public/([^\s?]*) [NC]
RewriteRule ^ %1 [L,NE,R=302]

RewriteRule ^((?!public/).*)$ public/$1 [L,NC]

just don't ignore the [path],[query],...

Hope I'm not being an idiot asking this:/

Can you try this out? I have given an example to redirect to index page so you may modify a bit for the index.php.

RewriteEngine On
RewriteCond %{REQUEST_URI} !^/index.php$
RewriteCond %{REQUEST_URI} !^/?$
RewriteCond %{REQUEST_URI} !^/public/
RewriteRule (.*) /? [R=301,L]

I'm using PHP to make the server php -S...

The webserver built into PHP is not Apache and so does not use .htaccess . You need to use Apache ( .htaccess ) in order to rewrite requests to the /public subdirectory.

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

These directives are on the right track, except they would also rewrite your CSS, JS and images. So you need some additional condition(s) to exclude these 3 subdirectories.

For example:

RewriteEngine On
RewriteBase /

RewriteCond %{THE_REQUEST} /public/([^\s?]*) [NC]
RewriteRule ^ %1 [L,NE,R=302]

RewriteRule !^(public|css|js|images)($|/) public/index.php [L,NC]

This assumes that the only static resources that need to be accessed outside of the public subdirectory are contained in the 3 subdirectories mentioned. This avoids the need for a filesystem check.

You then parse $_SERVER['REQUEST_URI'] in /public/index.php to extract the requested URL information.

UPDATE: I removed the RewriteCond directive before the last rule since the check can be performed in the RewriteRule directive itself.

NB: The redirect (first rule) is for SEO only and not integral to the process (as you should be linking to the URL without /public ). Ultimately, this redirect should be a 301 (permanent) redirect once you have confirmed it works as intended.

Thanks for the help, but I've found a solution. :)

Project folder:

config/
public/
    - css/
    - js/
    - index.php
src/

I used this in the project root folder:

RewriteEngine On
RewriteBase /

RewriteCond %{THE_REQUEST} /public/([^\s?]*) [NC]
RewriteRule ^ %1 [L,NE,R=302]

RewriteRule ^((?!public/).*)$ public/$1 [L,NC]

And in the public/ folder use this:

RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME}%  !-d
RewriteCond %{REQUEST_FILENAME}%  !-f

RewriteRule \.(js|css|svg|gif|jpg|jpeg|png)$ - [L]

RewriteRule ^(.*)$                index.php?router=$1 [QSA,L]

and it works very well, for those who have this problem, here's the solution

This my code parse url: https://ibb.co/r6HR1Qd

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