Home >Backend Development >PHP Tutorial >How Can I Remove File Extensions from Website URLs Using .htaccess?

How Can I Remove File Extensions from Website URLs Using .htaccess?

Linda Hamilton
Linda HamiltonOriginal
2024-12-06 09:40:12821browse

How Can I Remove File Extensions from Website URLs Using .htaccess?

Removing File Extensions from Website Addresses

In web design, removing file extensions can enhance the aesthetic appeal of URLs by concealing the programming language used behind the scenes. To achieve this, you can utilize the following steps:

Step 1: Create an .htaccess File

Navigate to the root folder of your website and create a file named ".htaccess" (ensure it starts with a period).

Step 2: Add the Rewrite Rules

Open the .htaccess file and insert the following rewrite rules:

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

Explanation of Rewrite Rules:

  • RewriteEngine on: Enables the rewrite engine.
  • RewriteCond %{REQUEST_FILENAME} !-d: Excludes directories from the rewrite rule.
  • RewriteCond %{REQUEST_FILENAME}.php -f: Checks if a PHP file with the same name exists.
  • RewriteRule ^(.*)$ $1.php: Redirects incoming requests with no file extension to their corresponding PHP files.

Example:

If a request is made for "http://something.example/profile," the rewrite rule will automatically redirect it to "http://something.example/profile.php," while the URL displayed in the address bar will remain "http://something.example/profile."

Additional Information:

  • This approach is highly versatile and can be adapted to remove other file extensions as needed.
  • It is important to note that this technique only affects the URL displayed in the address bar and does not alter the actual file path on the server.

The above is the detailed content of How Can I Remove File Extensions from Website URLs Using .htaccess?. For more information, please follow other related articles on the PHP Chinese website!

Statement:
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