Rule to remove directory from URL

1

Good, I'm setting my .htaccess to put my web into production and the fact is that I do not finish it or I do not get along very well with this file and it gives me problems.

I have the following directory structure:

index.php
login.php
.htaccess
includes/
    └ panel.php
    └ etc..

The .htaccess I have it configured right now like this:

<IfModule mod_rewrite.c>
  Options +FollowSymLinks
  RewriteEngine On
  RewriteBase /
  RewriteCond %{REQUEST_FILENAME} !-f
  RewriteCond %{REQUEST_FILENAME} !-d
  RewriteRule ^(.*)$ $1.php [L]
</IfModule>

Both index.php and login.php work correctly, since I hide the extension .php as I want.

The problem comes when I login and send me panel.php , shows me the address like this: www.miweb.com/includes/panel , and I do not want to show me the directory > '/ includes /' .

How could it be the rule that I should configure or where can I have the problem in the current configuration of the file?

Thanks in advance.

    
asked by Cifu 29.05.2017 в 10:21
source

2 answers

0

What you need to do is divide the redirection rules in different steps:

<IfModule mod_rewrite.c>
  Options +FollowSymLinks
  RewriteEngine On
  RewriteBase /
  # Primer paso: ignoramos archivos y directorios existentes
  RewriteCond %{REQUEST_FILENAME} -f
  RewriteCond %{REQUEST_FILENAME} -d
  RewriteRule .* - [L]
  # Segundo paso: sustituimos coincidencias sin extensión .php en general
  RewriteCond %{DOCUMENT_ROOT}/$1.php -f [NC]
  RewriteRule ^(.*)$ $1.php [L]
  # Tercer paso: sustituimos coincidencias en el subdirectorio /includes
  RewriteCond %{DOCUMENT_ROOT}/includes/$1.php -f [NC]
  RewriteRule ^(.*)$ includes/$1.php [L]
</IfModule>

In each step, the existence of the substitution is checked before doing it, to make sure that the destination PHP script exists.

The first step checks if there is a file or directory with the requested name and in that case nothing is done ( - of RewriteRule ).

The second step (which will only be executed if the first one is not fulfilled) checks if the URL we are requesting exists by adding .php .

The third and final step (which will only be executed if the previous two are not met) checks the existence of the URL in the includes subdirectory by adding the .php extension.

You will have to repeat the last step as many times as subdirectories you want to hide, but keep in mind that only the first match will work.

That is to say: if you have a file hola.php and another includes/hola.php only the first one will be attended and the second one will be masked. The only way to access it will be through includes/hola (match in the second step) or includes/hola.php (match in the first step).

    
answered by 29.05.2017 / 11:29
source
1

You have not asked him to take you to the panel without displaying includes. It should be something like this:

RewriteRule ^panel$ /includes/panel.php [L]

This would cause "www.miweb.com/panel" to display "www.miweb.com/includes/panel.php".

    
answered by 29.05.2017 в 11:07