Help with 301 redirect (Simple concept, per directory control)
-
18-09-2019 - |
Question
I'm trying to get a set of 301 redirects working for a client site, and I'm getting a bit stuck.
I have about 17 different directories that have .html content fileswhich I am moving across to Wordpress. What I am trying to do is create a simple, single .htaccess file with redirects in that will allow me to redirect all the .html files within a directory to wordpress URLs without an extension:
Manual redirect example:
redirect 301 /blog/2009/04/post1.html /blog/2009/04/post1/
redirect 301 /blog/2009/04/post2.html /blog/2009/04/post2/
Trying to create a rule like:
RewriteRule ^/blog/2009/04/(.*?).html$ /blog/2009/04/$1/ [R=301,NC,L]
Where the $1 is the filename (minus the .html) suffixed with a /
I can't determine whether the ^/blog/2009/04/(.*?).html$ means it will only redirect for this particular directory, and will redirect to the right location.
I would obviously want to duplicate this ReWriteRule for all my designated content directories...
Can anyone point me in the right direction?
Thanks,
Lee
Solution
Yes, your rule will only redirect for the 2009/04
directory. I think you want:
RewriteRule ^/blog/([0-9]+)/([0-9]+)/(.*?).html$ /blog/$1/$2/$3/ [R=301,NC,L]
This will now also redirect /blog/2008/12/post.html
to /blog/2008/12/post/
and so on. Although since you have almost exactly the same format, maybe this will be easier?
RewriteRule ^/blog/(.*?).html$ /blog/$1/ [R=301,NC,L]