Forum Moderators: phranque

Message Too Old, No Replies

Redirect root to folder

         

AudiS2

8:58 pm on Feb 21, 2009 (gmt 0)

10+ Year Member



My google search was unsuccessful (hence the topic name for next poor soul).

I want to redirect all requests coming to site.com/anything to site.com/blog/anything

Of course if request is already coming to site.com/blog/anything it should be kept unchanged.

AudiS2

9:31 pm on Feb 21, 2009 (gmt 0)

10+ Year Member



And while at it, how do I do it in reverse eg. redirect site.com/blog/anything to site.com/anything

Thanks!

jdMorgan

1:53 pm on Feb 22, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Which direction you want to redirect? If you try to put both functions in place, then you would get an 'infinite loop' of redirection.

Also, do you really want a redirect, or do you simply want to serve content from the files in /blog/ when URLs in root are requested?... Which URL do you want in the search results and the user's browser address bar?

Once you decide, you might want to try a search on your thread title in our WebmasterWorld site search; This topic comes up frequently.

Jim

AudiS2

2:06 pm on Feb 22, 2009 (gmt 0)

10+ Year Member



Hi jdMorgan

I want URLs with blog/ in them.

Old site structure was site.com/urls. But then moved all files to site.com/blog/urls. SO we want all incoming requests with olf structure to be redirected to new structure (with blog/ in it).

I do not want both functions at the same time, I am looking how to do the opposite thing if I need it.

In the meantime I made this and it seems to work, may be very rudimentary though.

Options +FollowSymlinks
RewriteEngine on
RewriteBase /
RewriteRule ^(.*) [site.com...] [NC]

jdMorgan

9:13 pm on Feb 22, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Understand that there is no need at all to change your URLs just because the files are now in /blog. Be very sure you understand what you are doing by changing URLs: Your site may drop badly in search rankings for a period of several weeks to many months. URLs and filepaths are not the same thing, and a URL of example.com can easily return content from example.com/blog:

Options +FollowSymlinks
RewriteEngine on
#
RewriteCond $1 !blog/
RewriteRule (.*) /blog/$1 [L]

However, if you insist on changing the URLs, and don't mind the temporary drop in search rankings, then you should use a 301-Moved Permanently redirect, again to avoid trouble with the search engines:

Options +FollowSymlinks
RewriteEngine on
#
RewriteCond $1 !blog/
RewriteRule (.*) http://www.example.com/blog/$1 [R=301,L]

Jim

AudiS2

9:38 pm on Feb 22, 2009 (gmt 0)

10+ Year Member



Thank you for the script Jim, I could not get a better explanation.

Your answer intrigued me and I hope you do not mind me following.

If I do a 301 redirect shouldn't the search ranking be preserved as this should be the only and best way of moving urls?

If I am not to change URL's and thus just serve the content from new path, wouldn't I have a problem with duplicate content? Further I am at risk of actually splitting all my rankings between what would in practice be two sites, one leaving at / and the other one at /blog/ ?

jdMorgan

9:57 pm on Feb 22, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



You won't have "two sites" -- All requests for example.com/<something> will get files from example.com/blog/<something>

If you change the URLs, you may expect to lose your search engine rankings for a few weeks up to nine months, depending on your current PageRank/Link-popularity. I can't tell you how long it will take, because it is determined by the search engines.

This delay is the results of search engines trying to figure out that *all* of your URLs have changed. And as Sir Tim Berners-Lee (co-inventor of the hyperlink) observed, "Cool URIs don't change [w3.org]" -- Search engines (and Web users) are happiest if your URLs *never* ever change. Another way to put it is to think of Web sites as books in a library, not as newspapers for sale on the street. Most Webmasters are *far* too cavalier about changing URLs; If they were properly designed (yes, I said designed), they would not have to change.

You can internally rewrite example.com/<something> URL requests to /blog/<something> filepaths, and then, if desired, redirect *only* direct HTTP client requests for example.com/blog/<something> URLs back to example.com/<something> URLs. That code has been posted here many times.

In this scenario *all* URLs point to example.com/<something>, and all files are stored at /blog/<something> -- The filepaths used inside the server to store files simply have "/blog" added to them, and the URLs don't need to show that.

Jim

AudiS2

11:06 pm on Feb 22, 2009 (gmt 0)

10+ Year Member



I hear you Jim.

However my friend (I am doing this for a friend of mine) has already moved the site to blog/ 2 months ago and it has been indexed by Google, linked by other sites etc.

Now she installed Webmaster tools and she gets a lot of 404 errors inside - because some sites still have old links.

So I am trying to help in best way I can. Do you think that in this case 301 redirecting / to blog/ is probably best solution?

jdMorgan

12:52 am on Feb 23, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Yes, go ahead and 301-redirect, since the damage has already been done. :)

Jim

AudiS2

9:17 am on Feb 23, 2009 (gmt 0)

10+ Year Member



Thanks Jim you've given me great insight.

She is now talking about returning everything back to / so that's why I asked for a return .htaccess. 301 All from blog/ to /. How would I do that?