简体   繁体   English

Apache RewriteRule 重写子目录不起作用

[英]Apache RewriteRule to rewrite subdirectories not working

I need to rewrite the following URL structure:我需要重写以下 URL 结构:

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

to:到:

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

I added this RewriteCond and RewriteRule but I cannot get this to work.我添加了这个 RewriteCond 和 RewriteRule 但我无法让它工作。

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

I have been testing with this online tool and I get successful results.我一直在使用此在线工具进行测试,并获得了成功的结果。 But I don't know the logic they have behind the tool to validate rules.但我不知道他们在验证规则的工具背后的逻辑。

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

I'm still new to Linux and Apache so my troubleshooting skills are limited.我还是 Linux 和 Apache 的新手,所以我的故障排除技能有限。 I have tried to search the error_log files and pipe all the rewrite entries with grep using this command, but there are no log entries.我尝试使用此命令搜索 error_log 文件并使用 grep 管道传输所有重写条目,但没有日志条目。 I am watching the logs in real time as I hit URLs that should be rewritten.当我点击应该重写的 URL 时,我正在实时查看日志。

tail -f error_log|fgrep '[rewrite:'

That comes directly from the Apache user manual.这直接来自 Apache 用户手册。

The stack I am running is Bitnami Wordpress Multisite.我正在运行的堆栈是 Bitnami Wordpress Multisite。

You've actually got a couple of problems.你实际上有几个问题。 It looks like you want to convert business-directory/NN/name/ to departments/name/ .看起来您想将business-directory/NN/name/departments/name/ As I said, you're requiring a trailing forward slash after the department name.正如我所说,您需要在部门名称后加一个斜杠。 which is fine, but it will only rewrite the exact match to business-directory/NN/name/ and will fail with business-directory/NN/name/whosdead.html .这很好,但它只会重写与business-directory/NN/name/的完全匹配,并且会因business-directory/NN/name/whosdead.html If you want to convert all URLs, you will need to capture whatever trails the department name:如果要转换所有 URL,则需要捕获部门名称的任何踪迹:

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

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

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

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

Once you verify the rewrite engine is actually processing your rules, try this RewriteCond to see if it works better.一旦你确认重写引擎实际上正在处理你的规则,试试这个 RewriteCond 看看它是否工作得更好。

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

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