简体   繁体   中英

Catchall rewrite rule after 1:1 301 redirect stops 1:1 redirect from working

I am implementing a 1:1 redirect in htaccess with something like this:

Redirect 301 /1.html site.com/folder/1
Redirect 301 /2.html site.com/2
Redirect 301 /3 site.com/vw-vans/another-folder/2
Redirect 301 /3.html site.com/3

RewriteCond %{REQUEST_URI} .*\.(html)
RewriteRule ^(.+\.html)$ site.com/folder? [R=301]

What I was expecting here was if 1.html matches it will be redirected to site.com/folder/1 and not site.com/folder. But unless I remove the rewrite rule, that is what is happening. Even with a 'L' flag.

Is it not possible or am I doing something wrong if I want the 1:1 redirect to work and if an url is not part of those 1:1 redirects, it will then apply the rewrite condition. I don't think there is a [L] flag for the 'Redirect' method.

Don't mix mod_alias rules and mod_rewrite one as they both get invoked at different times by Apache.

Try this in your .htaccess:

RewriteEngine On

RewriteRule ^1\.html$ /folder/1 [R=301,L,NC]
RewriteRule ^2\.html$ /2 [R=301,L,NC]
RewriteRule ^3\.html$ /3 [R=301,L,NC]
RewriteRule ^3/?$ /vw-vans/another-folder/2 [R=301,L,NC]

# catchall rule for .html files
RewriteRule ^.+?\.html$ /folder? [R=301,L,NC]

Make sure to clear your browser cache before testing this.

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