Home » Php » html – Hide PHP file extension from the URL

html – Hide PHP file extension from the URL

Posted by: admin February 25, 2020 Leave a comment

Questions:

I am storing my website’s content in a database. It is like a CMS. One will add page content from an admin panel, and content will stored in the database.

In one page, I have added 10-12 links as <a href="page.php/abc" and my URLs looks like www.example.com/page.php/abc. I want to hide .php from page.php. How can I do this without using .htaccess?

I have tried with str_replace (added in page content from the CMS), but it seems like it does not recognise it as PHP code and take it as a string.

How to&Answers:

You can do this in the following way:

Either put this code in your htaccess file (which you don’t want), or put this code in your server’s configuration file:

<IfModule mod_rewrite.c>
  RewriteEngine on
  RewriteCond %{REQUEST_FILENAME} !-d
  RewriteCond %{REQUEST_FILENAME}\.php -f
  RewriteRule ^(.*)$ $1.php
</IfModule>

This will hide .php from all URLs of your website.

That’s it.

Answer:

You need to use URI rewriting, depending on your webserver. For Apache, you can specify it in a .htaccess file, but for nginx, you need to edit the server block of the domain to allow that.

Basically, you need to tell it to rewrite any request like www.example.com/page/abc to www.example.com/page.php/abc (internally, not displayed in the URI bar).