Home > Backend Development > PHP Tutorial > How Can I Hide File Extensions (like .php) from My Website URLs?

How Can I Hide File Extensions (like .php) from My Website URLs?

Patricia Arquette
Release: 2024-11-25 01:29:13
Original
982 people have browsed it

How Can I Hide File Extensions (like .php) from My Website URLs?

Concealing File Extensions in Website URLs

It's oft-desired to present a more aesthetically pleasing website address by concealing the file extension (such as .php or .jsp). Here's how to achieve this:

Solution:

To remove the file extension from your website's URL, create a file named ".htaccess" in the root directory. Enter the following code within the file:

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

Explanation:

  • RewriteEngine on: Enables the rewrite engine module.
  • RewriteCond %{REQUEST_FILENAME} !-d: Checks if the requested file is not a directory.
  • RewriteCond %{REQUEST_FILENAME}.php -f: Checks if the requested file with the .php extension exists.
  • RewriteRule ^(.*)$ $1.php: Rewrites the URL to remove the file extension.

Example:

If you access "http://something.example/profile.php" in your web browser, the .htaccess directive will rewrite it to "http://something.example/profile".

The above is the detailed content of How Can I Hide File Extensions (like .php) from My Website URLs?. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template