简体   繁体   中英

Apache RewriteRule to rewrite subdirectories not working

I need to rewrite the following URL structure:

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 %{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. 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. I am watching the logs in real time as I hit URLs that should be rewritten.

tail -f error_log|fgrep '[rewrite:'

That comes directly from the Apache user manual.

The stack I am running is 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/ . 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 . If you want to convert all URLs, you will need to capture whatever trails the department name:

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

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

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

Converts http://website.com/business-directory/62/coroner/whosdead.html to 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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