homepage Welcome to WebmasterWorld Guest from
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Become a Pro Member

Visit PubCon.com
Home / Forums Index / Code, Content, and Presentation / Apache Web Server
Forum Library, Charter, Moderators: Ocean10000 & incrediBILL & phranque

Apache Web Server Forum

Simple 301 (external) Redirect Gone Awry
301 redirect one set of site pages to corresponding pages on new domain

 6:44 pm on Apr 7, 2011 (gmt 0)

I *thought* what I was trying to do here was easy, but I've run into a problem that I believe is related to a lack of understanding on syntax.

I'm trying to redirect a handful of pages from one domain/site to a corresponding set of pages on a new domain/site. I'm forwarding the home page of the old to the home page of the new, then about a dozen other pages in the old site to corresponding pages on the new site.

Here's what I've got:

redirect 301 / [newsite.com...]
redirect 301 /about/ [newsite.com...]
redirect 301 /file1/ [newsite.com...]
redirect 301 /file2/ [newsite.com...]

Simple as that, but only the home page of the old site redirects to the proper page, i.e. - the home page of the new site.

The second directive, for example, redirects to: http://www.newsite.comabout, leaving off the trailing slash between the domain and the file name, thus leading to a 404 error.

I've tried escaping that forward slash (though I firmly believed this was unnecessary) as well as wiping out my old site (temporarily) that it consisted ONLY of the .htaccess file. Neither worked, but any input is much appreciated.




 6:51 pm on Apr 7, 2011 (gmt 0)

The first rule redirects example.com/<anything> to newsite.com<anything> automatically re-appending the path information from the original request.

None of the other rules get to run.

Fix this by using RewriteRule syntax, a more precise pattern, the new domain name as before, and the [R=301,L] flags to signify the redirect.

Order the rules from most specific, to more general. For the root home page redirect, redirect to newsite.com/ with a trailing slash and NOT to any named index page.


 8:10 pm on Apr 7, 2011 (gmt 0)

Thanks g1smd - off to do some research. Working with regular expressions and .htaccess is just not my forte.



 8:16 pm on Apr 7, 2011 (gmt 0)

Try the resources listed and linked from the forum charter and the thousands of examples here in the forum.


 8:30 pm on Apr 7, 2011 (gmt 0)

Hmmm...I tried something that doesn't test my limited (nonexistent) abilities on this front and it appears to work, namely:

RedirectMatch 301 /about/ [newsite.com...]
RedirectMatch 301 /file1/ [newsite.com...]
RedirectMatch 301 /file2/ [newsite.com...]
RedirectMatch 301 / [newsite.com...]

I just made sure the home page redirect was last (as noted in the example) and it appears to forward fine. I only have 28 pages to forward, so it's pretty easy to manage, but I don't know *for sure* that this is "clean." Any suggestions for testing, or does this look legitimate?


 8:52 pm on Apr 7, 2011 (gmt 0)

Yes, but you're better off in the long run ignoring Redirect and RedirectMatch and using RewriteRule instead. The syntax is similar, but there is a LOT more power in mod_rewrite.


 9:35 pm on Apr 7, 2011 (gmt 0)

Thanks - I'll use what I have for now until I can learn more, then improve when I have to do something like this again. I appreciate the input and the guidance!


 9:55 pm on Apr 7, 2011 (gmt 0)

In particular check what happens if you request example.com/index.html from the old site.


 2:48 pm on Apr 9, 2011 (gmt 0)

Sorry - been away from the computer for a few days (an oddity). Now, I'm assuming that a request for oldsite.com/index.html would present some problems with the format I've used?

Luckily (though not for purposes of illustration), I don't have a file structure like that. The nature of the previous .htaccess file for the old site issued a 301 redirect (I believe it was a 301) back to the domain itself - without the file extension - if that particular file (home page) was requested, i.e. - oldsite.com/index.html always went back to oldsite.com. I also didn't use any file extensions at all, so that the oldsite.com/about/ page I referenced is, in fact, a "page" in the site and not a directory. The new site is the same, though without the trailing slash on individual pages/files.

Global Options:
 top home search open messages active posts  

Home / Forums Index / Code, Content, and Presentation / Apache Web Server
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Home ¦ Free Tools ¦ Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About ¦ Library ¦ Newsletter
WebmasterWorld is a Developer Shed Community owned by Jim Boykin.
© Webmaster World 1996-2014 all rights reserved