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


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]
  • 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). – Index 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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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