how to use htaccess to respond from folder or file with same name?

Viewed 36

I have this on my /html directory:

.htaccess
info/
info/public.php 
info.php 

htaccess file:

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ $1.php [NC,L]

One app call to info/public?format=json

Another app call to info?format=json

I can't figure out how to make both request get response since second request is returning 404. Any idea?

1 Answers

Suggest you to replace your rule with this code in site root .htaccess:

DirectorySlash Off
Options +FollowSymLinks -MultiViews -Indexes
RewriteEngine On

RewriteCond %{DOCUMENT_ROOT}/$1.php -f
RewriteRule ^(.+?)/?$ $1.php [L]

Here:

  • RewriteCond %{DOCUMENT_ROOT}/$1.php -f: Checks for the presence of corresponding .php file as the value of $1. Your condition RewriteCond %{REQUEST_FILENAME} !-f was only checking if the request is not for a file.
  • Options +FollowSymLinks -MultiViews -Indexes: Turns off options MultiViews (content negotiation) and Indexes (disables directory listing)
  • DirectorySlash Off: avoids trailing / being added to a directory since you have info/ as subdirectory and info.php also exists.
Related