繁体   English   中英

Apache RewriteRule 重写子目录不起作用

[英]Apache RewriteRule to rewrite subdirectories not working

我需要重写以下 URL 结构:

http://website.com/business-directory/25/administration/

到:

http://website.com/departments/administration/

我添加了这个 RewriteCond 和 RewriteRule 但我无法让它工作。

RewriteCond %{HTTP_HOST} website.com
RewriteRule ^business-directory/.*/(.*)/$ /departments/$1/

我一直在使用此在线工具进行测试,并获得了成功的结果。 但我不知道他们在验证规则的工具背后的逻辑。

https://htaccess.madewithlove.be?share=11bb2f4c-7d70-5ee9-9551-2d81d5a5d340

我还是 Linux 和 Apache 的新手,所以我的故障排除技能有限。 我尝试使用此命令搜索 error_log 文件并使用 grep 管道传输所有重写条目,但没有日志条目。 当我点击应该重写的 URL 时,我正在实时查看日志。

tail -f error_log|fgrep '[rewrite:'

这直接来自 Apache 用户手册。

我正在运行的堆栈是 Bitnami Wordpress Multisite。

你实际上有几个问题。 看起来您想将business-directory/NN/name/departments/name/ 正如我所说,您需要在部门名称后加一个斜杠。 这很好,但它只会重写与business-directory/NN/name/的完全匹配,并且会因business-directory/NN/name/whosdead.html 如果要转换所有 URL,则需要捕获部门名称的任何踪迹:

RewriteRule ^business-directory/.*?/(.*?)(/.+?)?$ /departments/$1$2

http://website.com/business-directory/62/coroner转换为http://website.com/departments/coroner

http://website.com/business-directory/62/coroner/转换为http://website.com/departments/coroner/

http://website.com/business-directory/62/coroner/whosdead.html转换为http://website.com/departments/coroner/whosdead.html

一旦你确认重写引擎实际上正在处理你的规则,试试这个 RewriteCond 看看它是否工作得更好。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM