Reputation: 553
I've been trying to figure out how to remove .php extension, I've searched everywhere and it seems most of the code are not working anymore. The code below is what I am using now to remove the .php extension but it is not working.
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !- f
RewriteRule ^([^\.]+)$ $1.php [NC]
Upvotes: 1
Views: 9967
Reputation: 263
This one works especially if you wanted to remove the .php from the url and redirect the user to the non .php url without changing or removing the .php in a url or inside the href:
RewriteEngine On
RewriteCond %{THE_REQUEST} /([^.]+)\.php [NC]
RewriteRule ^ /%1 [NC,L,R]
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^ %{REQUEST_URI}.php [NC,L]
Although, if you are using an ajax and fetch, you might need to remove .php on it, which is what I did.
Upvotes: 0
Reputation: 262
I'm using XAMPP on Windows 10. I also tried many times to remove the .php
extension but the result was the same. But after a long while finally, I got how we can do this. If you want the same results, just follow the steps given below...
To remove the .php
extension from a PHP file, e.g. if you want to change mywebsite.com/about.php
to mywebsite.com/about
you have to add the following code inside your .htaccess
file:
First of all, make sure you've created a new file named .htaccess
and placed it at your root-level/root-directory
where your index
file is placed.
Now, open this (.htaccess) file with any editor of your choice, copy/paste the given code and save it...
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^.]+)$ $1.php [NC,L]
Now, if you (any user) will access mywebsite.com/about
in the browser, the user will see the content of mywebsite.com/about.php
page.
But still, if you (any user) will access the URL as mywebsite.com/about.php
, this will not redirect the user to this mywebsite.com/about
page. But will go to this mywebsite.com/about.php
page which means the user can still visit mywebsite.com/about.php
page. Don't worry about it. If you want to avoid this, you can simply follow the next step.
To avoid the above problem, now you need to add some more rules in the .htaccess
file. For this, you've to replace your old code with this new one given below, save your file and check it out...
RewriteEngine On
RewriteCond %{THE_REQUEST} /([^.]+)\.php [NC]
RewriteRule ^ /%1 [NC,L,R]
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^ %{REQUEST_URI}.php [NC,L]
That's all and you're all set. I hope this will fix everyone's problem.
Have a nice day :)
Upvotes: 5
Reputation: 11
I think you need to change internal website coding as well with htaccess code above and remove the file name URL extension from where it is mentioned in your web coding. Eg:-
changed to
htaccess code
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.html -f
RewriteRule ^(.*)$ $1.html [NC,L]
Upvotes: 1
Reputation: 4578
To remove the .php extension from a PHP file
for example yoursite.com/wallpaper.php
to yoursite.com/wallpaper
you have to add the following code inside the .htaccess file:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ $1.php [NC,L]
Or
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule !.*\.php$ %{REQUEST_FILENAME}.php [L, QSA]
Or
see removing .php extension from URL
Also, make sure that you've mod_rewrite on.
Also see how to create .htacess file
Upvotes: 1