Htaccess 301 redirection for multiple files

I am wondering if multiple entries in htaccess would work for a 301 redirect.

The problem I see is that the old site files have a "html" extension and are named differently, so a simple global redirect won't work, it must be one rule for the filename.

These sites receive 1,000 visits daily, so you need to be careful not to punish the search engine.

RewriteEngine On
RewriteBase /
RewriteRule ^file1\.html$ [R=301,NC,L]
RewriteRule ^file2\.html$ [R=301,NC,L]
RewriteRule ^file3\.html$ [R=301,NC,L]
RewriteRule ^file4\.html$ [R=301,NC,L]


Rewriting the php header will not work as the old files are html type.


source to share

3 answers

I suppose you could use some kind of regex to reduce the number of different RewriteRules that you use, since they all look the same.

In your case, using just this might be okay:

RewriteRule ^(file1|file2|file3|file4)\.html$$1.php [R=301,NC,L]


This way you know exactly what you want to rewrite; but only has 1 RewriteRule.

Or, a little more general:

RewriteRule ^file([0-9]*)\.html$$1.php [R=301,NC,L]


Which lets you define that you want to rewrite each XXYZ.html file with an XYZ number. (Since I used '*' no number at all would be taken into account by this rewrite rule, if you want at least one number you must use '+')

You can also do something even more general - not sure if you want this, but something like this:

RewriteRule ^(.*?)\.html$$1.php [R=301,NC,L]


This is where you redirect anything ending in .html



Why not just ...

RewriteRule ^file([0-9]+)\.html$$1.php [R=301,NC,L]


Or, if you want to rewrite everything to the old site ...

RewriteRule ^(.*?)\.html$$1.php [R=301,NC,L]




Maybe you can try RewriteMap Directive



All Articles