gpt4 book ai didi

php - htaccess 为带有或不带有斜杠的 url 添加 .html 扩展名

转载 作者:行者123 更新时间:2023-12-04 04:42:28 28 4
gpt4 key购买 nike

因此,首先我有一个自定义 url 重写,它将请求变量发送到 php 脚本

重写规则如下:

RewriteRule ^([\w\/-]+)(\?.*)?$ test/index.php?slug=$1 [L,T=application/x-httpd-php]

因此,如果您访问类似 domain.com/slug-text 的内容它发送 slug-textindex.php位于名为 test 的文件夹中。

我想要的是我所有的网址看起来像 domain.com/slug-text.html , 但是 slug-test变量仍应发送至 index.php文件。



我无法弄清楚的是重定向。我希望从 domain.com/slug-text 重定向所有旧网址或 domain.com/slug-text/domain.com/slug-text.htmlslug-text发送至 index.php文件位于测试文件夹中。

搜索了很多,但在互联网上的任何地方都找不到这个问题的答案。

谢谢大家的帮助。

更新:
我的新代码是:
RewriteEngine On
Options +FollowSymlinks

RewriteCond %{SCRIPT_FILENAME} !-f
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteCond %{SCRIPT_FILENAME} !-l
RewriteRule ^(([\w/\-]+)?[\w-])(?!:\.html)$ http://domain.com/$1\.html [L,R=301]
RewriteRule ^(([\w/\-]+)?[\w-])(/|\.html)?$ test/index.php?slug=$1 [L]
domain.com/slug-text/不会重定向到 domain.com/slug-text.html domain.com/slug-text按预期工作,重定向到 domain.com/slug-text.html
我需要改变什么?

最佳答案

这条规则:

RewriteRule ^(([\w/\-]+)?[\w-])(/|\.html)?$ test/index.php?slug=$1 [L]

会陷阱 domain.com/slug-text , domain.com/slug-text/domain.com/slug-text.html并发送 slug-text/test/index.php里面 slug参数。

如果您真的想使用 [R=301] 进行重定向从旧网址到新网址,然后使用:
RewriteRule ^(([\w/-]+)?[\w-])/?(?!:\.html)$ http://domain.com/$1.html [L,R=301]
RewriteRule ^(([\w/-]+)?[\w-])\.html$ test/index.php?slug=$1 [L]

另请注意,使用显式重定向底部规则已修改为捕获以 .html 结尾的 url

还建议(如果您的 .htaccess 尚未包含此内容)过滤现有文件和文件夹的条件,以免被重定向规则捕获。只需在 RewriteRule 之前添加这些行行:
# existing file
RewriteCond %{SCRIPT_FILENAME} !-f
# existing folder
RewriteCond %{SCRIPT_FILENAME} !-d

如果使用符号链接(symbolic link):
# enable symlinks
Options +FollowSymLinks
# existing symlink
RewriteCond %{SCRIPT_FILENAME} !-l

//添加
您的 .htaccess 文件应如下所示:
RewriteEngine on
Options +FollowSymLinks
RewriteCond %{SCRIPT_FILENAME} !-f
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteCond %{SCRIPT_FILENAME} !-l
RewriteRule ^(([\w/-]+)?[\w-])/?(?!:\.html)$ http://domain.com/$1.html [L,R=301]
RewriteRule ^(([\w/-]+)?[\w-])\.html$ test/index.php?slug=$1 [L]

关于php - htaccess 为带有或不带有斜杠的 url 添加 .html 扩展名,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18682420/

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