.htaccess automatically change .php and .html extensions?

0

I'm using my htaccess like this

# Hide .html or .php extension
## External Redirect 
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s([^.]+)\.php [NC]
RewriteRule ^ %1 [R,NC]

## ## Internal Redirect
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^ %{REQUEST_URI}.php
  

When accessing myite.com / folder / site / test.php OK it mounts my url so ...............   (myite.com / folder / site / test) and it does not have the extension, but if I access it like this ...

     

mysite.com/folder/site/users.php === Gives 404 error

     

mysite.co.uk/folder/site/index.php === Gives 404 error

     

because it mounts those 2 urls there above so ...

     

mysite.com/index/

     

mysite.com / users /

     

When should I mount normal like this ...

     

mysite.co.uk/folder/site/users

     

mysite.co.uk/folder/site/index

Where do I need to change?

    
asked by anonymous 23.10.2017 / 18:43

2 answers

2

My suggestion is to check if the "file" (being real or "virtual") is only considered in URL after slash ( / ), this would avoid conflicts with files with different names (it is a bit difficult to detail the problem now), should be something like:

RewriteEngine On

# Redireciona
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s([^.]+)\.php [NC]
RewriteRule ^ %1 [R,NC,L]

# Reescreve
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)(/|)$ $1.php [L]

The (.*) gets the path and the (/|) is to "ignore" / , this will help to access the URL like this:

  • site.com/foo/bar/

That would cause this:

  • /pasta/public_html/foo/bar/.php

And this does not exist, if I understand you want /foo/bar and /foo/bar/ (example urls) to access /foo/bar.php , the above code should solve, now might not work , try this (ps: I did not have time to test):

RewriteEngine On

# Redireciona
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s([^.]+)\.php [NC]
RewriteRule ^ %1 [R,NC,L]

# Reescreve
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)([^/]+)(/|)$ $1$2.php [L]

If you have some POST form that sends to .php , this will fail:

RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s([^.]+)\.php [NC]
RewriteRule ^ %1 [R,NC,L]

Because it will conflict POST and redirection, what you can do is a check, something like:

RewriteEngine On

# Redireciona
RewriteCond %{REQUEST_METHOD} !^(PUT|POST)$ [NC] #ignora o POST e PUT
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s([^.]+)\.php [NC]
RewriteRule ^ %1 [R,NC,L]

# Reescreve
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)(/|)$ $1.php [L]

Still, it's best to tweak your FORM, get .php , like this:

<form class="m-t" role="form" method="post" action="validaacesso.php">

by

<form class="m-t" role="form" method="post" action="validaacesso">
    
23.10.2017 / 21:40
3

I would do so:

RewriteEngine On

#Sua definições aqui

RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule (.*)$ $1.php

This regular expression of RewriteRule tells anything to point to .php if it does not have the extension, but the current required uri should not be an existing file or folder on the server ( RewriteCond settings) / p>     

23.10.2017 / 20:40