gpt4 book ai didi

php - .htaccess 删除 .php 并添加尾部斜杠

转载 作者:行者123 更新时间:2023-12-03 18:30:06 24 4
gpt4 key购买 nike

我正在尝试从我的 URL 中删除 .php 并在末尾添加一个斜杠。我的 .php 删除工作正常,但我正在努力使尾部斜杠工作。

这是我到目前为止所得到的:

# Turn mod_rewrite on
RewriteEngine On
RewriteBase /

## hide .php extension snippet

# To externally redirect /dir/foo.php to /dir/foo
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s([^.]+)\.php [NC]
RewriteRule ^ %1 [R,L]

# To internally forward /dir/foo to /dir/foo.php
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.*?)/?$ $1.php [L]

提前致谢!

最佳答案

首先,这些代码和注释看起来很熟悉 :)

使用此代码:

RewriteEngine On
RewriteBase /

## hide .php extension snippet

# To externally redirect /dir/foo.php to /dir/foo
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s([^.]+)\.php [NC]
RewriteRule ^ %1/ [R,L]

# add a trailing slash
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} !/$
RewriteRule . %{REQUEST_URI}/ [L,R=301]

# To internally forward /dir/foo to /dir/foo.php
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.*?)/?$ $1.php [L]

关于php - .htaccess 删除 .php 并添加尾部斜杠,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21625985/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com