简体   繁体   中英

.htaccess drop down to parent directory if 301 folder redirect doesn't exist

I would like to be able to redirect a URL down to its parent directory if the folder redirect doesn't exist.

For Example

If the 301 folder redirect:

RewriteRule ^CarHire/(.*)/(.*)$ drive-car/car-hire-locations/$1/$2 [L,QSA]

The rule above will mean that if this URL below is entered:

/CarHire/Africa/Botswana

it will redirect to

/drive-car/car-hire-locations/Africa/Botswana

However I need a rule in place so that if lets say

A URL such as:

/CarHire/Africa/london (which doesn't exist)

is entered it will redirect to

/drive-car/car-hire-locations/Africa/ (Dropping down a level)

Optionally being able to check if the parent URL exists before redirecting, if not drop down another level until a page does exist (not sure if this is possible, would be great to have)

You can add these checks like this:

RewriteCond %{DOCUMENT_ROOT}/drive-car/car-hire-locations/$1/$2 -f
RewriteRule ^CarHire/(.*)/(.*)$ /drive-car/car-hire-locations/$1/$2 [L]

RewriteCond %{DOCUMENT_ROOT}/drive-car/car-hire-locations/$1/$2 !-f
RewriteRule ^CarHire/(.*)/(.*)$ drive-car/car-hire-locations/$1 [L,QSA]

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