how to mod rewrite whole directories question

New to mod_rewrite? This is a good place to start.

how to mod rewrite whole directories question

Postby awebsmith » Sun Jul 12, 2009 4:48 pm

Hello everybody. I have a particular situation that has been kicking me in the pants for a couple of weeks and hope someone can help me out here.

I have a site that had old dynamic urls. I switch to sef urls, and didnt have the time to 301 redirect the old urls to new sef urls. There were also roughly 700 articles or news items and pages and I didnt want to write than many individual rules.

With that being said, I now have over 600 404 page not found errors. Many of them come from directories, and if I can apply a mod rewrite to a handful of folders, it would take care of 90% of my 404 page not found problem.

The problem Im facing right now is that the old trailing url is being applied to new working live folder or directory. I need the rewrite to not append the old trailing url, just redirect to a new directory like in the following example.


RewriteRule ^/forum/(.*) http://www.mydomain.com/articles/ [L,R=301]
RewriteRule ^/content/.+ http://www.mydomain.com/articles/ [L,R=301]
RewriteRule ^/component/.+ http://www.mydomain.com/articles/ [L,R=301]
RewriteRule ^/gallery/.+ http://www.mydomain.com/photography/ [L,R=301]
RewriteRule ^/gallery2/.+ http://www.mydomain.com/photography/ [L,R=301]
RewriteRule ^/articles/category/(.*) http://www.mydomain.com/articles/ [L,R=301]
RewriteRule ^/articles/1/(.*) http://www.mydomain.com/articles/ [L,R=301]

As you can see, Ive tried /.+ and /(.*) and i still tries to append to the new url.

for example:
/forum/1/2/24 should redirect to http://www.mydomain.com/articles
NOT http://www.mydomain.com/articles/1/2/24
I dont have an articles/1/2/24
I would just like it to redirect all old urls from /forum to the new live folder at http://www.mydomain.com/articles/

I know this must be possible but have no idea how to do this. Any help would be greatly appreciated. or, if theres a better way than having to hand write over 700 301 redirects which I dont have time nor desire to do. Thank you in advance for any assistance with this.
awebsmith
 
Posts: 1
Joined: Sun Jul 12, 2009 3:43 pm

Postby richardk » Wed Jul 15, 2009 12:23 pm

Do you mean the query string is appended to the new URL?

Try (adding ? to the end of the new URL)
Code: Select all
Options +FollowSymLinks

RewriteEngine On

RewriteRule ^/(forum|articles/(category|1))/.*$ http://www.mydomain.com/articles/?    [R=301,L]
RewriteRule ^/(component|content)/.+$           http://www.mydomain.com/articles/?    [R=301,L]
RewriteRule ^/gallery2?/.+$                     http://www.mydomain.com/photography/? [R=301,L]
richardk
 
Posts: 8800
Joined: Wed Dec 21, 2005 7:50 am


Return to Beginner's Corner

Who is online

Users browsing this forum: No registered users and 86 guests

cron