Home > Backend Development > PHP Tutorial > How to Hide .php File Extensions with .htaccess?

How to Hide .php File Extensions with .htaccess?

DDD
Release: 2024-11-08 03:25:02
Original
537 people have browsed it

How to Hide .php File Extensions with .htaccess?

Hiding .php File Extensions in .htaccess

Users often seek to conceal the .php extension from their website's URLs. The .htaccess file, a powerful configuration file, aids in this endeavor.

One unsuccessful attempt at this involved the following code:

<IfModule mod_rewrite.c>
  RewriteEngine on
  RewriteRule ^folder/([a-zA-Z_\-0-9]+)/?$ /folder/.php
</IfModule>
Copy after login

Solution

A more effective approach utilizes the following code:

RewriteEngine On

# Unless directory, remove trailing slash
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)/$ http://example.com/folder/ [R=301,L]

# Redirect external .php requests to extensionless URL
RewriteCond %{THE_REQUEST} ^(.+)\.php([#?][^\ ]*)?\ HTTP/
RewriteRule ^(.+)\.php$ http://example.com/folder/ [R=301,L]

# Resolve .php file for extensionless PHP URLs
RewriteRule ^([^/.]+)$ .php [L]
Copy after login

This code:

  • Removes trailing slashes from requests that are not directories.
  • For requests targeting external .php files, redirects them to the extensionless URL.
  • Translates extensionless PHP URLs to their .php counterparts.

The above is the detailed content of How to Hide .php File Extensions with .htaccess?. 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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template