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

How Can I Hide File Extensions (like .php) from My Website\'s URL?

DDD
DDDOriginal
2024-11-25 21:30:15867browse

How Can I Hide File Extensions (like .php) from My Website's URL?

Hiding File Extensions in Website Addresses

When designing a website, you may prefer to conceal file extensions like .php or .jsp from the address bar for aesthetic reasons. This article provides a comprehensive solution to achieve this effect, based on an actual question and answer.

The Question:

A website developer sought a way to remove the file extension from their website address. They wanted the URL to appear as http://something.example/profile, rather than http://something.example/profile.php. This is commonly seen in websites like Stack Overflow.

The Solution:

To address this issue, you can create an .htaccess file in the root directory of your website. For instance, in /home/domains/domain.example/htdocs/, add an .htaccess file with the following content:

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

Explanation:

  • RewriteEngine on: Enables Apache's URL rewriting module.
  • RewriteCond %{REQUEST_FILENAME} !-d: Checks if the requested file is not a directory.
  • RewriteCond %{REQUEST_FILENAME}.php -f: Verifies if the requested file exists with a .php extension.
  • RewriteRule ^(.*)$ $1.php: Rewrites the request to include the .php extension in the background, maintaining the clean URL on the address bar.

By implementing this solution, your website address will appear without file extensions, providing a more polished and professional look.

The above is the detailed content of How Can I Hide File Extensions (like .php) from My Website\'s URL?. 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