Issue
I want to remove the .php extension from the URL using a .htaccess file located in the same folder as the php files (I want to remove the extension for all of the files, not just one of them). I use the following code:
Options +FollowSymLinks # I found this on the Internet trying to solve this problem, but nothing changed
RewriteEngine On
RewriteBase /app_tests/ # This is the folder where I want to delete the .php extension, it's located in the htdocs folder
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule !.*\.php$ %{REQUEST_FILENAME}.php [QSA,L] #I have tried without QSA as well (I don't really know what it does)
So theoretically, when I go to myweb.com/index I should see the index.php page but I get a 403 error (forbidden) when entering that address. Of course, there is no directory called index. I know that the code works (or at least does something) because when I write it wrong it says internal server error and because if I erase or comment it (#) the error I get is 404 (not found).
I'm using one of the lastest versions of XAMPP for Windows. I've uncommented the following line in the httpd.conf file to have the module loaded:
LoadModule rewrite_module modules/mod_rewrite.so
Do anyone know what I've done wrong or how to solve this problem? Thank you!
Solution
It's probably failing, because you have comments after the directives. In .htaccess comments must be on their own line, they cannot come after a rule/condition.
I will help you with the task at hand by sharing a snippet from my stack. Put this .htaccess file inside the folder where you want the .php-less file access to work.
<IfModule mod_rewrite.c>
Options +FollowSymlinks
RewriteEngine on
RewriteBase /app_tests/
# Set an S for HTTPS protocol
# helps for rewrites with protocol specific URL
RewriteCond %{SERVER_PORT}s ^(443(s)|[0-9]+s)$
RewriteRule ^(.+)$ - [ENV=SFORSSL:%2]
# remove trailing slashes if it's not a directory
# you will need this, because if a trailing slash is added wrongly
# the condition for the next group of rules will fail
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.+)/$ http%{ENV:SFORSSL}://%{HTTP_HOST}/$1 [R=301,QSA,L]
# internally rewrite to .php if a file exists
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule (.+) $1.php [QSA,L]
</IfModule>
QSA, stands for Query String Append, it basically appends to the URL all the request parameters that the GET request includes (everything after the ? in the URL).
Answered By - Shef Answer Checked By - Marilyn (WPSolving Volunteer)