Jay
Jay

Reputation: 1104

.htaccess regex, redirect to new folders

I'm trying to redirect some old files/folders that used to follow this pattern:

foldername/filename.extension

foldername has: (2010 or 2011)(text A-Z a-z 0-9 _ or -)

filename has: (text A-Z a-z 0-9 _ or -).extension

Ex: 2011aug_SomeNameHere/image.jpg

The new folder tree organizes the files by year (so everything is one level deeper): 2010/foldername/filename.extension and 2011/foldername/filename.extension

Ex: 2011/2011aug_SomeNameHere/image.jpg

And I have the following for my rewriterule:

<IfModule mod_rewrite.c>

    RewriteEngine On
    RewriteBase /

    RewriteRule ^http://(www\.)?domain.com/path/to/+(2010|2011)+([A-Za-z\_\-])+/([A-Za-z0-9\_\-]\.([A-Za-z]))$ http://domain.com/path/to/$1/$1$2/$3 [L,R=301]

</IfModule>

In Firefox I'm getting a redirect to: domain.com/path/to/2011aug_SomeNameHere/image.jpg/2011aug_SomeNameHere/image.jpg/2011aug_SomeNameHere/image.jpg...

In Chrome I'm getting a 404 error with the url at: domain.com/path/to/2011aug_SomeNameHere/image.jpg

Does anyone have any ideas/tips?

Upvotes: 0

Views: 811

Answers (1)

xthexder
xthexder

Reputation: 1565

Try this:

RewriteRule ^path/to/(2010|2011)([A-Za-z\_\-]+)/([A-Za-z0-9\_\-]+\.[A-Za-z]+)$ /path/to/$1/$1$2/$3 [L,R=301]

It is not required to use have the domain in your rewrite unless you are handling multiple domains with one rewrite. Your first group being $1 would have been your (www.) if you went to your domain with www infront. I suggest you look up your use of +'s and groups to understand more of the regex. Having your +'s outside the groups allows for matching of the same group more than once. In your case you want to match all of it in one group. You also had a nested group for your file extension match where it wasn't need.

Upvotes: 1

Related Questions