Remove .php extension (explicitly written) for friendly URL
you can remove .php from requests like so
RewriteCond %{REQUEST_URI} ^/(.*).php$
RewriteRule ^(.*)$ %1 [L,QSA]
Remove php extension from subfolder url
You need additional rules for removing .php
extension.
Use these rules:
Options +FollowSymLinks -MultiViews
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_METHOD} !POST
RewriteCond %{THE_REQUEST} \s/+(.+?)\.php[\s?] [NC]
RewriteRule ^ /%1 [R=302,NE,L]
RewriteCond %{REQUEST_METHOD} !POST
RewriteCond %{THE_REQUEST} /index\.php [NC]
RewriteRule ^(.*)index\.php$ /$1 [L,R=302,NC,NE]
RewriteCond %{REQUEST_FILENAME} -d [OR]
RewriteCond %{REQUEST_FILENAME} -f
RewriteRule ^ - [L]
RewriteRule ^status/([\w-]+)/?$ status.php?msgID=$1 [L,QSA]
RewriteRule ^(following|followers|friends)/([\w-]+)/?$ $1.php?username=$2 [L,QSA]
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.+?)/?$ $1.php [L]
RewriteRule (?:^|/)([\w-]+)/?$ profile.php?username=$1 [L,QSA]
Remove php extension from url
This might work:
RewriteRule ^(.+)\.php/(.+)$ $1/$2
The first backreference would capture lt from lt.php and the second backreference would have example and php would be added to it.
Remove .php from urls with htaccess
Try this code for hiding .php (will work both ways):
Options +FollowSymLinks -MultiViews
# Turn mod_rewrite on
RewriteEngine On
RewriteBase /
## don't touch /forum URIs
RewriteRule ^forums/ - [L,NC]
## hide .php extension snippet
# To externally redirect /dir/foo.php to /dir/foo
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s([^.]+)\.php [NC]
RewriteRule ^ %1 [R,L]
# To internally forward /dir/foo to /dir/foo.php
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.*?)/?$ $1.php [L]
remove php extension, stop access of url with .php extension and remove trailing slashes
You're looking at things backwards: the first rule you have doesn't "remove the php extension", it adds it to URLs that don't already have it (technically, any that don't contain a period).
I think you want something more like this:
RewriteEngine On
RewriteBase /
# Remove .php from any URLs that contain it, using an external 301 redirect
RewriteCond %{QUERY_STRING} !^no-redirect-loop(&|$)
RewriteRule ^(.*)\.php$ $1 [NS,R=301,L]
# Now add it back internally
RewriteCond %{QUERY_STRING} !^no-redirect-loop(&|$)
RewriteRule ^(.*)$ $1.php?no-redirect-loop [NS,QSA]
Edit: While debugging another similar answer, I realized that the previous solution I posted here wasn't going to work in an .htaccess file. I've edited the example code above to use a rather ugly kluge for breaking redirect loops instead. A side effect of the kluge is that all scripts will see an extra empty URL parameter named no-redirect-loop
.
Trying to remove .php file extension
You have asked many questions here. Let me answer first one:
www.mysite.com/page
works without any rewrite rule due to Options MultiViews
. Option MultiViews
is used by Apache's content negotiation module
that runs before mod_rewrite
and and makes Apache server match extensions of files. So /file
can be in URL but it will serve /file.php
.
If you place this line at top of your root .htaccess:
Options -MultiViews
Then www.mysite.com/page
will issue 404.
Also to automatically remove .php
from your URLs use this code in your root .htaccess:
Options -MultiViews
RewriteEngine On
# To externally redirect /dir/file.php to /dir/file
RewriteCond %{THE_REQUEST} \s/+(?:index)?(.*?)\.php[\s?] [NC]
RewriteRule ^ /%1 [R=301,L,NE]
# To internally forward /dir/file to /dir/file.php
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{DOCUMENT_ROOT}/$1\.php -f [NC]
RewriteRule ^(.+?)/?$ /$1.php [L]
Related Topics
Rotate Array Elements to the Left (Move First Element to Last and Re-Index)
Mechanisms for Tracking Db Schema Changes
Codeigniter - How to Catch Db Errors
Laravel: Connect to Databases Dynamically
Send PHP Date to JavaScript Date Format
Use a Variable to Define a PHP Function
When Using Gmail for Smtp, Can You Set a Different "From" Address
Checking If All the Array Items Are Empty PHP
My Magento Extension Install Script Will Not Run
Find Common Values in Multiple Arrays with PHP
How to Convert String to Boolean PHP
Reconstruct/Get Source Code of a PHP Function
Performance of Try-Catch in PHP
PHP Get Url of Redirect from Source Url
Destroy PHP Session on Closing