How do you remove .php from a URL?

Hey! So I’ve tried every soultion I could find on google for how I can remove .php from my website url.
Currently I’m trying to remove it from scoresmasher,net/submit.php to make it scoresmasher,net/submit

I found this bit of code to put in my .htaccess file

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^.]+)$ $1.php [NC,L]

and it worked! Then after about 5 minutes it randomly stopped working… I used this website https://htaccess.madewithlove.be/ to test the code and it shows

RewriteRule ^([^.]+)$ $1.php [NC,L]
This rule was not met.

Any idea how to fix this? I need to have file extentions removed before I’m able to launch the website.

Modern practise for Wordpress, CMS, PHP Frameworks is to use the following which redirects everything to index.php. All requests are then validated and the relevant files called tp produce the web page UNLESS the URL is an exact filename match or the file is a directory:

  RewriteEngine On
	RewriteCond %{REQUEST_FILENAME} !-f
	RewriteCond %{REQUEST_FILENAME} !-d
	RewriteRule ^(.*)$ index.php/$1 [L]

  ErrorDocument 404   ERROR_NOT_FOUND_PAGE.html

Edit:

Also note that in order for RewriteEngine On to work the following Apache2 Module must be loaded:

sudo a2enmod rewrite

Thank you for your reply! I tried putting the code at the end of my .htaccess file and it still won’t work. I installed wordpress through hostgator and removed everything in all of the .php files to i can write it all myself and use this website as a learning experience. Which so far has been very successful! I just don’t know much about apache or working with the .htaccess file.

Is there anything I need to add in to my index.php for the code to work? Also how would I check if my wordpress install is even running apache?

NOTE: I’m running an install of wordpress on hostgator if that changes anything.

I dont know if this will cause issues in the future but for now I just removed .php from the actual file via FTP and now it loads fine when doing scoresmasher.net/submit-score i can still edit it normally.

It looks like WordPress redirection is working ok and all URLs are redirecting to the default index.php page. Try the following:

https://scoresmasher.net/index.html
https://scoresmasher.net/non-existing-page.php
https://scoresmasher.net/and-another-non-existing-page

Wordpress redirection has been thoroughly tested and following their recommendations for adding new pages should not cause any problems.

All my files are .php so the first one works when changing html to php but the other 2 404 as they should. I’m not using wordpress to add new pages I’m doing it manually. It’s been working fine removing the file extension from the end of the file name so far.

Thank you for your time!

1 Like

I just tried the links again and 494 was returned" Perhaps my previous attempts were cached?

Hi, Just want to mention that you would achive the same URL by using directory names as destinations. You could simply put the “submit.php” in a directory named “submit” and rename it to the default “index.php”

Then the URL you want to use, like “scoresmasher,net/submit” would go to “scoresmasher,net/submit/index.php”.

Of course the RewriteRule is more convenient then going back and adjust the site structure, especially if it’s a late client request. :slight_smile: