Search code examples
phpurl-rewritingfriendly-url

How can I remove file extension from a website address?


I am designing a website. I want my website address to look like the following image:

File name extensions like (PHP/JSP) are hidden

I don't want my website to look like http://something.example/profile.php. I want the .php extension to be removed in the address bar when someone opens my website. In other words, I want my website to be like: http://something.example/profile

As a second example, you can look at the Stack Overflow website address itself.

How can I get this done?


Solution

  • Just add an .htaccess file to the root folder of your site (for example, /home/domains/domain.example/htdocs/) with the following content:

    RewriteEngine on
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_FILENAME}\.php -f
    RewriteRule ^(.*)$ $1.php
    

    More about how this works in these pages: mod_rewrite guide (introduction, using it), reference documentation