drop .php extension for folders - htaccess - .htaccess

Currently I drop the .php extension in the URL.
I use:
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^([^/]+)/$ $1.php
# Forces a trailing slash to be added
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !(\.[a-zA-Z0-9]{1,5}|/)$
RewriteRule (.*)$ /$1/ [R=301,L]
The problem is that if I have a folder it doesn't work. I have to add it to every folder group. I know there is an easier way.
Can anyone help?

You just need to change the regex so that you aren't excluding requests that have a / in it.
Change your first rule to:
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.+)/$ $1.php

Related

After url rewrite, the system adds the extension

With the following rules in my .htaccess, the system adds a .php at the end of my url's.
RewriteEngine On
# Removing extension
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ $1.php [NC,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^app/([^/]+)$ app.php?module=$1 [L]
Why ?
Thanks.
Have your first rule add .php only after checking if .php file exists:
RewriteEngine On
# add extension
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^([^.]+?)/?$ $1.php [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^app/([^/]+)$ app.php?module=$1 [L,QSA,NC]

How to hide my .html and .php extensions using .htaccess?

I have this .htaccess:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([a-z,0-9,A-Z,_-]+)$ ./$1.php
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([a-z,0-9,A-Z,_-]+)$ ./$1.html
The problem is: Its just hidding .php
Shouldn't it hide .php and .html?
Change your rules to:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.+?)/?$ $1.php [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.html -f
RewriteRule ^(.+?)/?$ $1.html [L]
1./ your html files are still accessible because your second set of rule will never be used. Besides, your second line specifically tells the server to leave "real" files aside.
2./ You only need RewriteEngine On once .
3./ You should use the flags to tell your rewriting when to stop.
RewriteEngine On
# if the requested file does not exist
RewriteCond %{REQUEST_FILENAME} !-f
# if the requested folder does not exist
RewriteCond %{REQUEST_FILENAME} !-d
# sends all urls except home to its corresponding php file
RewriteRule ^([a-z,0-9,A-Z,_-]+)$ ./$1.php [R=301,L]
But if you want it to work for all file extensions, remove the first rule, leaving only.
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([a-z,0-9,A-Z,_-]+)$ ./$1.php [R=301,L]

multiple rules of URL rewriting

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.html -f
RewriteRule ^(.*)$ $1.html[L,QSA]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ $1.php
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([a-zA-Z0-9_-]+)$ /profile.php?name=$1
RewriteRule ^([a-zA-Z0-9_-]+)/$ /profile.php?name=$1
This is currently .htaccess of my website. The rewriting supposes to have two functions, makes the internal url example/profile.php/name=xyz into example/xyz as external url, and also removes the extensions of html files.
But the extension removing is not working, and such as for a file named xyz.html, it gets 404 and
example.com/xyz/?name=xyz
At the address bar. I think there is some conflicts between two rules.
RewriteCond is only applicable to very next RewriteRule hence your last rule is running without any RewriteCond. Also you need to use [L] (Last) flag in all of your rules.
Use this code:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.html -f
RewriteRule ^(.*)$ /$1.html [L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ /$1.php [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([\w-]+)/?$ /profile.php?name=$1 [L]

htaccess remove html and php duplicated is not working

this snippet works fine for removing php extension
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ $1.php
However, if I add html condition, it will not work
RewriteCond %{REQUEST_FILENAME}\.html -f
RewriteRule ^(.*)$ $1.html
Did I do something wrong?
You may try this instead, in one .htaccess file at root directory:
Options +FollowSymlinks -MultiViews
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteCond %{REQUEST_URI} !\.php [NC]
RewriteRule .* %{REQUEST_URI}.php [NC,L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.html -f
RewriteCond %{REQUEST_URI} !\.html [NC]
RewriteRule .* %{REQUEST_URI}.html [NC,L]
You should be aware the implementation in your code works only when the request doesn't have a trailing slash. I just modified your code to work, but I think it's worth nothing that problem. For example:
For a request like http://example.com/filename/, the file to match with condition RewriteCond %{REQUEST_FILENAME}.php -f is filename/.php. That file can't exist, therefore, the respective rule will be never used.
Option to remove the trailing slash when present:
Add these lines after RewriteEngine On in the previous code:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} /(.*[^/]+)/$ [NC]
RewriteRule .* /%1 [NC,L]

mod-rewrite force ?

I need to be able to post links that look like "mysite.com/?d=48YSWD96" & go to file path "/d=48YSWD96.php" So, I need "mysite.com/?d=48YSWD96" to go to "mysite.com/d=48YSWD96". I can't start my file name with a "?" because it will just turn into an index page for the root folder and disregard the content in the file. How do I do this? this is my htacces:
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^([^/]+)/$ $1.php
RewriteRule ^([^/]+)/([^/]+)/$ /$1/$2.php
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !(\.[a-zA-Z0-9]{1,5}|/)$
RewriteRule (.*)$ /$1/ [R=301,L]
RewriteBase /
Same thing as the query string match from this question (which has the same .htaccess): url construction htaccess
RewriteCond %{QUERY_STRING} (^|&)d=([A-Za-z0-9]+)($|&)
RewriteRule ^$ /d=%2.php [L]

Resources