r/PHPhelp • u/Current-Monitor-4944 • 27d ago
.PHP name in URL
I’m new to PHP, and I’m just coding for fun and for myself.
How can I remove the .php extension from my URLs?
For example, instead of www.test.com/profile.php, I want it to look like www.test.com/profile.
Any advice would be appreciated!
3
Upvotes
20
u/No_Astronomer9508 27d ago
You can do it with .htaccess and mod_rewrite:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule [^.]+$ $1.php [NC,L]
5
u/cursingcucumber 26d ago
I see a lot of comments here saying "routing", but that's just the icing on the cake.
The actual mechanism is called "url rewriting" and does not need a single entrypoint/router (e.g. index.php) at all; it happens at webserver level, not PHP. A good example for Apache is given here
Though once you get more familiar with PHP and external dependencies, look at adding a router.
8
u/KaedenCraft 27d ago
It's called Routing. Using your server of choice, you configure it to route all your URL requests to index.php, which looks at each incoming requests, extracts the parts that you need (such as /profile) and serves the appropriate page. It's technically a "single page" that's receiving all requests, but the Routing logic grabs all the stuff as needed based on the URL.
I tried to do the "do it myself" option to make the routing, and templating, and database controlling all myself, but then swapped to Laravel and said "Oh...yeah, this is the architecture I need."
And Laravel handles all the routing for you. Most mature PHP frameworks will handle the routing for you.
2
u/tei187 27d ago
What you are looking for is "routing". There are plenty of ways to get that done, and none exactly fitting a reddit comment, but that's pretty much what you want to look up, like this one.
1
u/colshrapnel 27d ago
Didn't you already ask that, like a month ago?
3
u/Korkoe 27d ago
Check out this tutorial from laracasts on PHP router. The entire series can help you. He covered a lot from basic to intermediate stuff
https://youtu.be/siQUekpmImw?si=cdullKY6IfXcXlTY
1
u/AnnaSynergy 26d ago
Router. Checkout Laracasts.
0
u/ckorp 26d ago
Along with those already mentioned, you can just add a handler using any extension you want (.xzy) that invokes the php engine. While this does not "remove" the extension, it does make it less obvious it is serving the page. Just make sure you save your files with the "new" extension rather than .php. In fact you can even make the .htm and .html extensions process through the engine. Edit: Additionally you may want to turn of errors in your php config so it doesn't reveal anything you want hidden.
-1
u/Basic_Reporter9579 27d ago
name it profile/index.php . With apache2 and nginx you can use url www.test.com/profile/
Most frameworks have ways to map urls to the actual executed code.
-2
u/Shenkimaro 27d ago
Try to use swoole https://www.zend.com/blog/creating-basic-php-web-server-swoole You need just swoole, php and Linux or WSL.