.htaccess change extensions . php and . html automatically?

Asked

Viewed 1,179 times

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 access meusite.com.br/folder/site/test.php OK he mounts my url like ......... ( meusite.com.br/folder/site/test ) and is without the extension, however if I access so ...

meusite.com.br/folder/site/users.php === Gives 404 error

meusite.com.br/folder/site/index.php === Gives 404 error

'cause he rides those 2 urls up there like this ...

meusite.com.br/index/

meusite.com.br/usuarios/

When I should ride normal like this .......

meusite.com.br/folder/site/users

meusite.com.br/folder/site/index

Where I need to change ?

2 answers

3

I would do so:

RewriteEngine On

#Sua definições aqui

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

That regular expression of RewriteRule says anything that points to .php if you do not have the extension, however the current Uri request should not be an existing file or folder on the server (settings for RewriteCond).

  • On which of the friendly lines ?

  • I fixed the code. Replace the section below ##Internal Redirect

  • Okay, replace in ##Internal Redirect but now does not run any. Says ERR_TOO_MANY_REDIRECTS this page is not working , for all now !

  • It didn’t work because ?

  • @Would Paulomaia be able to check the Apache log? Because simply knowing it didn’t work doesn’t help much in identifying the problem.

  • @Paulomaia you must have mixed two Rewrites, it’s quite likely that yours . htaccess has more things, post on http://pastebin.com it complete and send the link here to Wallace analyze.

  • @Paulomaia trade the line for this RewriteRule ^(.*)$ $1.php [L]

  • it is not generating error.log in folder

  • There is no other . htaccess in the /site/ === folder which is where all the files are.

  • @Guilherme Nascimento changed the line and in the same no page now access !

  • @Guilherme Nascimento === https://pastebin.com/vZzyLmBF

  • @Paulomaia so it’s giving problem, you mixed the code of Wallace with yours, it doesn’t make any sense to do.

  • @Guilherme Nascimento I changed and now does not remove the extensions . php no === https://pastebin.com/HQqsRyCN

  • @Paulomaia this and it has nothing to do with Wallace’s code, where is the RewriteEngine On?

  • @Guilherme Nascimento === Same thing does not remove extensions no https://pastebin.com/fLNX8aWy

  • @Paulomaia I have no way to test, but make it work: https://pastebin.com/6uaj80dJ (let me know)

  • @Guilherme Nascimento === https://pastebin.com/6uaj80dJ === OK this now with the topic problem with those two url only they that do not work , and if I leave the system and try to login again does not find the user and password . not log tbm

Show 12 more comments

2


My suggestion is to check if the "file" (being real or "virtual") is considered only in URL after Slash (/), this because it would avoid conflicts with files with different names (it is a little difficult to detail the problem now), it 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 (.*) takes the path and the (/|) is to "ignore" the /, this will help random 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) access /foo/bar.php, the above code should solve, now chance does not work, try this (ps: I didn’t 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 any POST form you send to .php, that will fail:

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

Because it will conflict the POST and the redirect, 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 the best is to adjust your forms, take the .php, thus:

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

for

<form class="m-t" role="form" method="post" action="validaacesso">
  • OK I tried the two ways presented and this now with the topic problem with those two url only they that do not work , and if I quit the system and try to login again does not find the user and password . not log tbm

  • Not if I take . htaccess out of the folder , everything works fine ! Of course === showing the file extension .

  • ok, you entered the normal system OK , now I will go up the htaccess that guided me and you try to access for example users , main panel in the menu and come out and try to login again , ok, I’m going up here htaccess

  • Okay, you can test now

  • I get it, but this htaccess should not automatically remove extensions ?

  • is what I asked in the post, if there’s any way htaccess can remove extensions from files automatically without me having to redo all my script

  • ok, and for example why it does not access users nor the main panel via side menu, but the others access normally

  • ok, but users and main menu === in the side menu are not Forms, are pages like the others in the side menu , the others access these not there

  • It is necessary to edit all links then

  • Please remove comments where there are links ! from my system here ,

  • But to summarize === There is no htaccess that automatically hides extensions then ?

  • @Paulomaia for a better functioning yes, after all url are virtual paths, if using a Framework as Laravel the urls would be written via routes and would not even exist .php. As I said all urls work here, except the pages with POST because obviously they conflict, because you are affecting the behavior, understand that redirections will not do magic for you, the ideal of having something that removes the extension (as I said urls are all "virtual") Make sure you don’t have duplicate pages. The problem is that you don’t understand HTTP and servers to understand the concept.

  • 1

    @Paulomaia Things work via Requisition and Answer and Urls do not access "files directly", what is done there is a mapping to assimilate something like c:/wamp/htdcos/foo.php for something like http://localhost/foo.php. This is the basics of what HTTP is.

  • The check there that you guided in your reply, gives ERROR 500 for everything now

  • this way = https://pastebin.com/MJcfmiUP ERROR 500 for everything ...

  • @Paulomaia can not say what happened, try to trade RewriteCond %{REQUEST_METHOD} !^(PUT|POST)$ for RewriteCond %{REQUEST_METHOD} (PUT|POST)

  • @Paulomaia face need not send 2 comment, I already read the first :/ ... then trade for it RewriteCond %{REQUEST_METHOD} !^(PUT|POST)$ [NC], was missing the flag NC

  • ok, acessa ai pra vc ver usuarios e painel principal nada ainda https://pastebin.com/n6FSyDeN

  • @Paulomaia para esta normal http://rpmon.com.br/SiteSys/controle/usuarios see a print: https://i.stack.Imgur.com/Jmyp1.png

  • @Paulomaia yes, all links I clicked through the side menu, navigated on all possible pages clicking on everything, worked normally for me.

  • Strange here for me rpmon.com.br/Sitesys/control/users ERROR 404

  • @Paulomaia presses Ctrl+F5 (or only F5 twice).

  • login logou normal , fixed error with your check here ta ok login

  • I understood - it may be yes I was doing tests here previously .... I will clear the cache because the index is already normal = by the main panel menu === you tried to change in parameters , from the above message, which I think is missing the encoding

  • ok, I’ve cleared the cache and the normal user now !

  • @Paulomaia blz, so I guess now it’s okay.

  • try to change in parameters the body field of the email and a save it saved but it seems to lose the encoding UTF in the message !

  • @Paulomay this UTF is very broad, it can be anything, follow the step by step that should help: https://answall.com/a/43205/3635

  • You came to test on the page there, and saw how it shows the message of SUCCESS ? I’ve seen htaccess that automatically adds utf to pages, you know this line ?

  • @Paulomaia ah, got it, has nothing to do with with UTF, the problem is the querystring that is being reformatted, I will try to adjust and I warn you.

  • Adjust where ? in htaccess , dai vc poe there in pastebiin ?

  • @Paulomaia I (Guilherme Nascimento) go "try to" adjust, I don’t know if it’s . htaccess, it could be your script, it could be anything, there’s no way to know

  • OK, I wait ! because before htaccess it was not like this the message full of strange characters .

  • @Paulomaia It, but it has nothing to do with UTF, the problem is that the URL escape is being caused twice, these characters with % indicates URL-Encoded, I will "try" to find out, but as I already said, may be problem in your .php, whether it previously worked or not, because there are problems that only occur in specific scenarios and do not fail in others by simple "luck".

  • I got it Buddy ! I’m waiting for you ...

  • To hide html and php is htaccess ? https://pastebin.com/xL15Nh7c

  • Remove comments that contain url to the system.

  • this around, I’m not able to hide php and html in the same htaccess

  • And add the / at the end of the url

  • Please avoid long discussions in the comments; your talk was moved to the chat

Show 35 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.