简体   繁体   中英

Hide PHP file extension from the URL

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.

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.

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

RewriteEngine on
RewriteCond %{THE_REQUEST} /([^.]+)\.php [NC]
RewriteRule ^ /%1 [NC,L,R]

RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^ %{REQUEST_URI}.php [NC,L]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM