How to rewrite directory to root directory

I want to change my url. Here I have a directory structure like this

htdocs/ example/ public/ login.php people/ people1.php people2.php animal/ animal1.php animal2.php 404.php assets/ css/ js/ 

then I want the url as shown below to match the existing directory in the root directory

 localhost/example/login localhost/example/people/people1 localhost/example/people/people2 localhost/example/animal/animal1 localhost/example/animal/animal2 

I tried to create a .htaccess file with the following contents

 Options +FollowSymLinks RewriteEngine On rewritecond %{REQUEST_URI} !^/public/(.*) rewritecond %{REQUEST_URI} !^/assets/(.*) RewriteRule .* index.php [L] 

and index.php

 $requested = empty($_SERVER['REQUEST_URI']) ? false : $_SERVER['REQUEST_URI']; switch ( $requested ) { case '/login': include 'public/login.php'; break; default: include 'public/404.php'; } 

when I headed localhost / example / login, but the destination is 404.php (ERROR).

Could you help me?

+6
source share
2 answers

The variable $_SERVER['REQUEST_URI'] is the entire URI. So, if they go to http://example.com/example/login , the variable $_SERVER['REQUEST_URI'] is equal to /example/login . Something you could try is to modify the htaccess file:

 Options +FollowSymLinks RewriteEngine On rewritecond %{REQUEST_URI} !/public/(.*) rewritecond %{REQUEST_URI} !/assets/(.*) RewriteRule ^(.*)$ index.php/$1 [L] 

(Note that ^/public/ will never match, because REQUEST_URI will be /example/public )

Then use $_SERVER['PATH_INFO'] in your code instead.

+1
source

It looks like you are trying to hide the PHP extension with the priority set:

  • public directory
  • in the other place

Put this code in your DOCUMENT_ROOT/.htaccess file:

 RewriteEngine On # skip for existing files/directories (/assets will be skipped here) RewriteCond %{REQUEST_FILENAME} -f [OR] RewriteCond %{REQUEST_FILENAME} -d RewriteRule ^ - [L] # try to load PHP file from public directory RewriteCond %{DOCUMENT_ROOT}/public/$1.php -f RewriteRule ^(.+?)/?$ /public/$1.php [L] # now try .php elsewhere RewriteCond %{DOCUMENT_ROOT}/$1.php -f RewriteRule ^(.+?)/?$ /$1.php [L] 
+1
source

All Articles