如何在 .htaccess 中隐藏.php扩展名

2022-08-30 12:09:55

可能的重复:
使用.htaccess删除.php扩展名

我试图隐藏.php文件扩展名,但由于某种原因无法使其工作。我最近的尝试如下:

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

我已经尝试了许多不同的代码变体,但我在网上找到,但仍然没有运气。.htaccess 文件位于根目录中。


答案 1

我用过这个:

RewriteEngine On

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

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

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

另请参阅:此问题


答案 2

使用PHP脚本sans扩展的另一个选项是

Options +MultiViews

或者甚至只是在目录中跟随:.htaccess

DefaultType application/x-httpd-php

后者允许将所有没有扩展名的文件名视为PHP脚本。虽然MultiViews使Web服务器寻找替代方案,但当仅提供基名时(但是,性能会受到影响)。script


推荐