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

How Can I Remove File Extensions from My Website URLs?

Susan Sarandon
Release: 2024-11-28 00:10:11
Original
441 people have browsed it

How Can I Remove File Extensions from My Website URLs?

Removing File Extension from Website Address

In the realm of web design, it's often desirable to conceal file extensions from URLs for aesthetic and accessibility reasons. For instance, instead of displaying an address like http://something.example/profile.php, you may prefer a more succinct format like http://something.example/profile.

To achieve this, you can employ the following approach:

In the root directory of your website (e.g., /home/domains/domain.example/htdocs), create an .htaccess file. This file is responsible for configuring Apache's handling of HTTP requests.

Within the .htaccess file, add the following code snippet:

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

This code performs the following actions:

  • Enables the Apache rewrite module.
  • Checks if the requested file path does not exist as a directory.
  • Checks if the requested file path with a .php extension exists as a file.
  • Rewrites the request to include the .php extension if it doesn't exist in the original request.

This setup effectively hides the .php extension from the URL while still allowing Apache to process PHP scripts as usual.

For further details on the functionality of this code, refer to the mod_rewrite guide and reference documentation.

The above is the detailed content of How Can I Remove File Extensions 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