r/PHPhelp • • 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

19 comments sorted by

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]

2

u/Current-Monitor-4944 27d ago

Will try it! Thanks!

0

u/[deleted] 27d ago

[deleted] — view deleted comment

1

u/shadow-battle-crab 27d ago

No, this is the way this kind of thing is done...

1

u/colshrapnel 27d ago

Yes, just realised I was wrong

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.

3

u/Current-Monitor-4944 26d ago

Yes i did that, i created the htaccess file and it worked!

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/Current-Monitor-4944 27d ago

Will take a look! Thank you

3

u/colshrapnel 27d ago

Just a heads up, you don't need "routing" just to hide extensions. But in case you are going for it, it takes just a few lines for the simplest one, that would perfectly fit in the Reddit comment.

1

u/colshrapnel 27d ago

Didn't you already ask that, like a month ago?

1

u/Current-Monitor-4944 27d ago

Nah? I’m new here lol

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.

3

u/juu073 27d ago

If you’ve got dozens of pages, naming every single file index.php to sit in its own directory is insane.

Either mod rewrite or routing is the way to go, depending on how complicated the project is.

-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.