How to hide my .html and .php extensions using .htaccess? - .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]

Related

htaccess redirect for not existing files

I want to redirect on my website if a file does not exsist as .php file but it does as .html file. For exmaple:
If page.php does not exsist i want to redirect to page.html
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^.*$ /default.php [L]
So far i managed to redirect to default.php if any file does not exsist. How can i redirect to the html file if one exsists and how do i limit this on redirection on php files ?
I have always been a fan of explicit and transparent rules:
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^/?(.*)/?$ /$1.php [L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME}.html -f
RewriteRule ^/?(.*)/?$ /$1.html [L]

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]

drop .php extension for folders - 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

rewrite to another file if .php file not exists

I'm using the following rule to serve the associate .php file
All last directory pathname will go to the associate .php file
Ex. /about/ or /about will go /about.php
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ $1.php`
So when I use http://example.com/abc it points to /abc.php, this is working fine. But if /abc.php file does not exist in that directory then I like to forward that to /listing.php file. How can I do that? please help
Also, I need this to apply to the root directory only. means
http://example.com/..*here*.. not for any subdirectory.
Thanks
Try this, this should be tested:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond $0\.php !-f //check if (for example) abc.php not exists then
RewriteRule ^(.*)$ /listing.php
RewriteRule ^(.*)$ $1.php
Update: I tested this on my local server, note that: the folder is test you can adjust for your own
Options All -Indexes
RewriteEngine on
RewriteBase /test
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond test\/$0\.php !-f
RewriteRule ^(.*)$ listening.php
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ $1.php

Htaccess :Rewrite rule issue

I've following code in a .htaccess file. The file is in a subfolder, lets say /content/.
RewriteCond %{REQUEST_FILENAME} -s [OR]
RewriteCond %{REQUEST_FILENAME} -l [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^([^/]+)(/([^/]+))?(/(edit)+)(/([^/]+))?/?$ edit.php?category=$1&slug=$3&edit=$5&password=$7 [L]
RewriteRule ^([^/]+)(/([^/]+)?/?)$ content.php?category=$1&slug=$3 [L]
I'm expecting above code will rewrite,
mydomain.com/content/text1/text2/edit/my_password to mydomain.com/content/edit.php?category=text1&slug=text2&password=my_password
mydomain.com/content/text1/text2 to mydomain.com/content/content.php?category=text1&slug=text2
mydomain.com/content/text1 to mydomain.com/content/content.php?category=text1
mydomain.com/content/ to mydomain.com/content/content.php
The code rewrites above correctly, But it breaks the asset urls.
e.g. It rewrites http://mydomain.com/content/js/tms.js to something else, it is same for the css files. My assets folders are /img, /css, /js.
If i commented RewriteRule ^([^/]+)(/([^/]+))?(/(edit)+)(/([^/]+))?/?$ edit.php?category=$1&slug=$3&edit=$5&password=$7 [L] line it works for the assets
Any ideas how to fix this?
The condition RewriteCond %{REQUEST_FILENAME} -d means that "the request is for an existing directory". If you add a ! in front of the -d it changes it to "the request is not for an existing directory".
Try:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)/([^/]+)(/edit)+/([^/]+?)/?$ edit.php?category=$1&slug=$2&password=$4 [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)/([^/]+?)/?$ content.php?category=$1&slug=$2 [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+?)/?$ content.php?category=$1 [L]

Resources