如何隐藏PHP扩展在.htaccessPHP、htaccess

2023-09-02 00:18:06 作者:网易云音乐取什么昵称 网易云昵称独一无二

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

我试图隐藏PHP文件扩展名,但由于某种原因无法得到它的工作。我最新的尝试是如下:

I'm trying to hide the .php file extension but for some reason can't get it to work. My latest attempt was the following:

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

的code

我已经尝试了许多不同的差异我在网上找到,但仍然没有运气。 .htaccess文件放在根目录中。

I have tried many different variances of code I have found online but still no luck. The .htaccess file is placed within the root directory.

推荐答案

我用这样的:

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]

另见:这个问题