Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

Probably a simple problem, but I cant seem to figure it out: I've recently created a new website from scratch to replace an older website. The old website had a good SEO ranking which is obviously something I want to keep.

Since the new website also has a new structure there are some directories from the old site/structure which I've dropped completely. However there are quite a lot of external links pointing to these directories, or more specifically files within the old directories. So my questions boils down to this: How do I redirect all/any hits on files within certain sub-directories?

For example:

www.mydomain.com/oldsubdirectory/somefile1.file --> mydomain.com/newdirectory www.mydomain.com/oldsubdirectory/somefile2.anothertypeoffile --> mydomain.com/newdirectory www.mydomain.com/oldsubdirectory/somefile3.fileofsometype --> mydomain.com/newdirectory

share|improve this question

2 Answers 2

Something like this would do the trick:

RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} ^www.mydomain.com$ [NC]
RewriteRule ^/oldsubdirectory/somefile1.file$ http://mydomain.com/newdirectory [L,R=301]
RewriteRule ^/oldsubdirectory/somefile2.anothertypeoffile$ http://mydomain.com/newdirectory [L,R=301]

The RewriteCond statement makes these rewrite only apply on the www.mydomain.com though, so if someone were to access mydomain.com/oldsubdirectory/somefile1.file it would NOT be rewritten. You can try both with and without the RewriteCond to see what works best for you.

If you want it to redirect to the same file in newdirectory you have to specify the filename (or match it with regular expression and then recall it later), for example:

RewriteRule ^/oldsubdirectory/(somefile1.file)$ http://mydomain.com/newdirectory/$1 [L,R=301]
share|improve this answer
    
Thank you for your reply. If I've understood you correctly I have to create one rule for every file I had in the old directory? That was sort of what I was hoping to avoid since there are hundres of files in the old directory (Thanks to a shotty CMS). –  KG Christensen Jan 8 '12 at 11:09

You can do rewrite rule with rewrite engine as suggested by @Mattias or you can use the more simple form redirect permanent <old url> <new url> Read up at httpd.apache.org for more information. The information there on this is written in very simple manner. While with redirect permanent there is nothing to learn just use it. There are hundreds of tutorial on url rewrite in apache httpd. Google it

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.