Forum Moderators: phranque

Message Too Old, No Replies

Redirect temporary hosting site to main site?

         

mountainsoft

2:29 pm on Apr 15, 2011 (gmt 0)

10+ Year Member



I recently moved my web site to a new web host, and the temporary address on my new host is still accessible.

For instance, any page at:

www.mountain-software.com

Can also be accessed at:

www.mountain-software.com/~mtnsoft2/

Both variations show up in Google listings as well, so I would like to redirect (via a RewriteRule) any request for the second form so it is sent to the first form. In other words, I don't want anyone (especially Google) accessing the second internal address.

For example, any access to:

www.mountain-software.com/~mtnsoft2/index.htm

Should redirect to:

www.mountain-software.com/index.htm

Any ideas?

Thanks,

Anthony Watson
Mountain Software
[url]www.mountain-software.com[/url]

wilderness

2:41 pm on Apr 15, 2011 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



Pretty much the same question [webmasterworld.com] and same answer in two recently active threads [webmasterworld.com], and currently in the top listed threads.

mountainsoft

6:20 pm on Apr 15, 2011 (gmt 0)

10+ Year Member



Wilderness,

I'm basically using a variation of the examples on the pages you mentioned:

RewriteCond %{HTTP_HOST} !^(www\.mountain-software\.com)?$ [nc]
RewriteRule (.*) [mountain-software.com...]

Unfortunately, this does not solve my problem because the address DOES start with www.mountain-software.com. What I need is a way to strip out the ~mtnsoft2 folder from the URL.

Thanks,

Anthony

g1smd

6:39 pm on Apr 15, 2011 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



The (.*) pattern matches the part of the URL that you want to save and re-use.

Adjust the pattern so that parts you do not want to save and re-use are outside of the parentheses.

RewriteRule ^~mtnsoft2/(.*) http://www.example.com/$1 [R=301,L]


This rule is a separate rule and it goes ahead of your non-www to www canonicalisation rule.

g1smd

6:47 pm on Apr 15, 2011 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



The full ruleset.

RewriteRule ^~mtnsoft2/(([^/]+/)*)index\.(html?|php)$ http://www.example.com/$1 [R=301,L]
#
RewriteRule ^~mtnsoft2/(.*) http://www.example.com/$1 [R=301,L]
#
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /([^/]+/)*index\.(html?|php)\ HTTP/
RewriteRule ^(([^/]+/)*)index\.(html?|php)$ http://www.example.com/$1 [R=301,L]
#
#RewriteCond %{HTTP_HOST} !^(www\.example\.com)?$
RewriteRule (.*) http://www.example.com/$1 [R=301,L]

mountainsoft

8:45 pm on Apr 15, 2011 (gmt 0)

10+ Year Member



Unfortunately, none of the offered solutions worked. They either had no effect, or caused redirection errors. I tried many variations and could not achieve what I wanted using a RewriteRule.

Thankfully, I got it to work by using Redirect instead:

Redirect 301 /~mtnsoft2/ http://www.example.com/

For example:

www.example.com/~mtnsoft2/page.htm

now redirects to:

www.example.com/page.htm

g1smd

10:14 pm on Apr 15, 2011 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



In what way did the supplied code "not work"? Post #4298426 was a "complete solution" with four rules.

The code might need small adjustments depending on where in the filesystem you place the .htaccess file.

Be warned. If you use RewriteRule anywhere within your site you must not use Redirect or RedirectMatch within the same site. If you use RewriteRule for any of your rules, use it for all of your rules, otherwise you can get weird results that can trash your search rankings.

mountainsoft

10:30 pm on Apr 15, 2011 (gmt 0)

10+ Year Member



By "Not Work" I mean it had no apparent effect.

www.example.com/~mtnsoft2/page.htm still resolved to that address.

Oddly, my "Redirect" version works fine, but if I try to code the exact same thing with RewriteRule it does not work (I still get the original ~mtnsoft2 address).

Can you elaborate on the weird results using Redirect and RewriteRule in the same .htaccess file? I've been using a combination of the two for years with no apparent side effects.

g1smd

10:50 pm on Apr 15, 2011 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



You can end up with multiple step redirection chains and/or expose previously rewritten pointers back out on to the web as new URLs.

Have you got "RewriteEngine On" ahead of all of the RewriteRule code? Did you flush the browser cache before each test?

Are there any other RewriteRules in the file interacting with the supplied code?

I would add RewriteCond lines before the first two rules, each one testing THE_REQUEST. There's the possibility of a redirect loop without them.

Before changing the code, use the Live HTTP Headers extension for Firefox to check whether there is an infinite redirect loop.

RewriteEngine On
#
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /~mtnsoft2/([^/]+/)*index\.(html?|php)\ HTTP/
RewriteRule ^~mtnsoft2/(([^/]+/)*)index\.(html?|php)$ http://www.example.com/$1 [R=301,L]
#
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /~mtnsoft2/[^\ ]+\ HTTP/
RewriteRule ^~mtnsoft2/(.*) http://www.example.com/$1 [R=301,L]
#
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /([^/]+/)*index\.(html?|php)\ HTTP/
RewriteRule ^(([^/]+/)*)index\.(html?|php)$ http://www.example.com/$1 [R=301,L]
#
#RewriteCond %{HTTP_HOST} !^(www\.example\.com)?$
RewriteRule (.*) http://www.example.com/$1 [R=301,L]

mountainsoft

11:17 pm on Apr 15, 2011 (gmt 0)

10+ Year Member



Thanks, but even starting with your rules in an empty .htaccess file I get the "page is not redirecting properly" error in Firefox. Yes, I cleared the cache, yes I had RewriteEngine On, and yes I changed example.com to my own domain.

No biggy. My Redirect version accomplishes my goal in a single line and is easy to understand. Problem solved as far as I'm concerned.

g1smd

11:20 pm on Apr 15, 2011 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



OK. The "not redirecting properly" error message indicates an infinite redirect loop.

Using the Live HTTP Headers extension for Firefox will reveal in seconds what the URL being redirected to is, and that will show where in the pattern and logic the error is likely to be.

It's probably a very simple typo in the code.