简体   繁体   中英

.htaccess redirect 301 doesn't work for all links

Recently I've been having weird issues with one of my htaccess files. Basically, I had to Redirect 404 pages to relevant subpage, and I checked few random ones and thought everything was working fine until I checked all of them and realised only about 60% of my rules did work.

I was unable to find solution to my problem and I tried quite few approaches.

Here's list of Examples of the Redirect 301s that I'm using:

Redirect 301 /products/sampleproduct http://www.mysite.co.uk/products/overhead-lifting-solutions
**Redirect 301 /products/sampleproduct/rail-system http://www.mysite.co.uk/products/overhead-lifting-solutions/rail-systems
**Redirect 301 /products/sampleproduct/floor-wall-mounted-cranes http://www.mysite.co.uk/products/overhead-lifting-solutions/jib-cranes
**Redirect 301 /products/sampleproduct/vacuum-lifter-easyhand-t http://www.mysite.co.uk/products/overhead-lifting-solutions/vacuum-lifters
Redirect 301 /products/sampleproduct/vacuum-lifter-easyhand-m http://www.mysite.co.uk/products/overhead-lifting-solutions/vacuum-lifters
**Redirect 301 /products/sampleproduct/vacuum-lifter-vacuhand-v http://www.mysite.co.uk/products/overhead-lifting-solutions/vacuum-lifters
**Redirect 301 /products/sampleproduct/end-effectors http://www.mysite.co.uk/products/overhead-lifting-solutions/end-effectors
Redirect 301 /products/sampleproduct/mechline http://www.mysite.co.uk/products/overhead-lifting-solutions/chain-hoists
**Redirect 301 /products/sampleproduct/mechchain http://www.mysite.co.uk/products/overhead-lifting-solutions/chain-hoists

Redirects that don't work were marked with **. I have over 160 Redirect where about ~60 do not work.

2nd Redirect rule is not working because first one is taking precedence. First rule is redirecting anything starting with /products/sampleproduct thus overriding /products/sampleproduct/rail-system all the time. You can either reverse the order of rules or better use RedirectMatch with regex support like this using regex anchors:

RedirectMatch 301 ^/products/sampleproduct/?$ http://www.mysite.co.uk/products/overhead-lifting-solutions
RedirectMatch 301 ^/products/sampleproduct/rail-system/?$ http://www.mysite.co.uk/products/overhead-lifting-solutions/rail-systems

You need to make this fix for rest of your rules and make sure to clear the browser cache before testing these.

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