使用 .htaccess 更改 URL .php 扩展名

Bru*_*uno 2 php regex .htaccess mod-rewrite clean-urls

我想知道是否可以将扩展名 .php 更改为“/”。

例子:

http://localhost/website/example.php?id=19

转换为,

http://localhost/网站/示例/?id=19

或者,如果你们对地址有更好的想法,我想知道我应该使用什么。

我是干净 URL 的初学者。

坎普斯。

anu*_*ava 5

您可以在您的文件中使用此代码DOCUMENT_ROOT/.htaccess

RewriteEngine On
RewriteBase /

# To externally redirect /dir/foo.php to /dir/foo
RewriteCond %{THE_REQUEST} \s/+(.+?)\.php[\s?] [NC]
RewriteRule ^ %1 [R=302,NE,L]

# To internally forward /dir/file to /dir/file.php
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{DOCUMENT_ROOT}/$1\.php -f [NC]
RewriteRule ^(.+?)/?$ $1.php [L]
Run Code Online (Sandbox Code Playgroud)