gpt4 book ai didi

regex - RedirectMatch 如何匹配任何单词但不匹配索引和什么都不匹配

转载 作者:行者123 更新时间:2023-12-03 14:19:50 25 4
gpt4 key购买 nike

我在我的网站中移动了许多网址:/folder/page.php至:/folder/page/ .
现在我想将旧网址重定向到新网址,所以我添加了 .htaccess这条规则:

RedirectMatch permanent /folder/(.*)\.php https://www.site.it/folder/$1/
但是不行,这个规则先重定向到 /folder/page/然后再次重定向到 /folder/page/index/所以我试过这个:
RedirectMatch permanent /folder/(?!index)\.php https://www.site.it/folder/$1/
但它的工作原理类似于上述规则。
我也测试过:
RedirectMatch permanent /folder/(?!index)(.*)\.php https://www.site.it/folder/$1/
又一样。
我用 .htaccess仅在根文件夹中,这是新规则之前的内容:
RewriteEngine On
RewriteBase /
RewriteCond %{HTTPS} off
RewriteCond %{HTTP:X-Forwarded-Proto} !https
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
重定向 http 需要这些规则流量到 https并且由我使用的网络托管服务建议。
我也测试错了 RedirectMatch没有任何其他规则。
如何重定向除 index.php 之外的所有页面?
这是 url 列表和所需的行为:
/folder/                      not match     no action
/folder/index.php not match no action
/folder/subfolder/ not match no action
/folder/subfolder/index.php not match no action
/folder/anindex.php match redirect to /folder/anindex/
/folder/indexfile.php match redirect to /folder/indexfile/
/folder/anindexfile.php match redirect to /folder/anindexfile/
/folder/anotherfile.php match redirect to /folder/anotherfile/
请不要建议使用 mod_rewrite 因为它不是所描述的最佳实践 here .

最佳答案

您可以在现有规则下方添加新规则:

RewriteCond %{THE_REQUEST} \s(/folder/[\w-]+)\.php[?\s] [NC]
RewriteRule ^ %1/ [L,R=301]
THE_REQUEST只匹配客户端发送的原始 URI,不匹配修改后的 REQUEST_URI .

关于regex - RedirectMatch 如何匹配任何单词但不匹配索引和什么都不匹配,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64632467/

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