Welcome to WebmasterWorld Guest from 54.145.144.101

Forum Moderators: Ocean10000 & incrediBILL & phranque

Message Too Old, No Replies

301 Redirect File Series URLs

How to set up a redirect rule to redirect a series of file names

   
11:21 am on Jan 11, 2009 (gmt 0)

5+ Year Member



Friends,

I have some file names such as

example.com/filename1.html

example.com/filename2.html

and so on...

Though I can set up 301 redirects for individual file names such as

redirect 301 /filename1.html http://www.example.com/

redirect 301 /filename2.html http://www.example.com/

I was wondering if there can be a rule set that would redirect all the series of files from /filename1.html to /filename9.html at one go to the root domain.

I tried this one, but doesn't work:

RewriteCond %{HTTP_HOST} ^(www\.)?example\.com$
RewriteRule ^/filename([0-9])\.html http://www.example.com/ [R=301,L]

Thanks in advance!

12:22 pm on Jan 11, 2009 (gmt 0)

5+ Year Member



It definitely is doable, so don't give up, and you should be very close to the solution. I'm not sure if this is the only modification needed. I removed the forward slash before filename:

RewriteCond %{HTTP_HOST} ^(www\.)?example\.com$
RewriteRule ^filename([0-9])\.html http://www.example.com/ [R=301,L]

12:35 pm on Jan 11, 2009 (gmt 0)

5+ Year Member



OMG! Thanks a ton Steve :-)

It is working by just removing the forward slash before the filename.

Thanks again!

3:05 pm on Jan 11, 2009 (gmt 0)

5+ Year Member



You'd better try something different because your rule will redirect all files to the root index file. The following looks better to me :)

RewriteRule ^(filename[0-9]+\.html)$ http://www.example.com/$1 [R=301,L]

PS: Just didn't notice this was exactly your intent to 301 redirect all to the root index. So, your rule would be okay.

5:35 pm on Jan 11, 2009 (gmt 0)

WebmasterWorld Senior Member g1smd is a WebmasterWorld Top Contributor of All Time 10+ Year Member Top Contributors Of The Month



Notice the
+
in that final example.

It allows the number to be more than one digit if required.

The

( )
are not needed, and neither is the
$1
part.
7:26 am on Jan 12, 2009 (gmt 0)

5+ Year Member



Thanks a lot for your replies guys.

The code worked just be removing the forward slash and g1smd, I have noted down your suggestions as well.

Thanks again :)