PDA

View Full Version : Need Apache Redirect to external site


drewprops
2013-08-29, 10:52
I've come so far, but I still can't *think* in Apache rewriting guide code (http://httpd.apache.org/docs/2.0/misc/rewriteguide.html) yet.

Someone has asked me to redirect one hosted domain to another site entirely.

So from foo.com to a specific page on another site, bar.com/cheese.

Right now I have this:


RewriteEngine on
RewriteRule ^(.*) http://www.bar.com/cheese


...and it works GREAT if you simply try clicking on foo.com.

But when you go to www.foo.com there is no redirect.

Can someone walk me through the mistake I'm making?



...

Brad
2013-08-29, 11:37
It's hard to say without seeing more of the config.

How do you have www.foo.com defined? Is that a ServerAlias for foo.com? Is it a Virtual Host? Do any other rules take precedence before this?

Also, you might want to add [R=301,L] to your RewriteRule. This will tell Apache that it should serve a 301 (Moved Permanently) redirect and that it should be the last rule to process if it matches.

drewprops
2013-08-29, 12:32
All I know right now is that www.foo.com is hosted on Earthlink (EARTHLINK?!?!?!?!) and that bar.com is hosted my MediaTemple.

I was wondering about the niceties of the 201,301,etc codes, thanks for that additional tip.

(and above when I said "clicking on foo.com" I meant "typing foo.com into your browser and going there"

Did any of this help at all?


...

Brad
2013-08-29, 13:16
Not really...

What kind of access do you have to the configs at Earthlink? I'm guessing you don't get the full Apache config. Is there something like a .htaccess file it uses? Or some online configuration panel?

drewprops
2013-08-29, 18:08
Oh yes, I'm so sorry - yes, I'm using an .htaccess file.

And thank you for taking the time to consider this, by the by.

#embarrassed



...

turtle
2013-08-29, 21:08
Try this:
RewriteEngine on
RewriteCond %{http_host} ^foo\.com [OR]
RewriteCond %{http_host} ^www\.foo\.com
RewriteRule ^(.*)$ http://bar.com/cheese [R=301,L]

This covers both the www and non-www traffic and points it all to the URL specified. I'm having a hard time finding the guide on this, but the 301 indicates that this is a permanent redirection to search engines.

drewprops
2013-08-29, 23:08
Well darn.

Turtle that looked like THE THING that would work.

I carefully copied it in and only replaced the bits that needed changing to the real sites.

Going to "foo.com" does a great job with a redirect to "bar.com", but I still get an "Oops!" page from Google Chrome when trying to go to "www.foo.com".

The Chrome error reads:



Oops! Google Chrome could not find www.foo.com

Access a cached copy of www.foo.­com




It's plain old kooky.

...

Brad
2013-08-30, 00:46
I'm having a hard time finding the guide on this, but the 301 indicates that this is a permanent redirection to search engines.
Correct, and to some clever web browsers too. Google Chrome, for instance, may remember pages that serve 301 redirects, and on subsequent visits it will go directly to the redirected page instead of requesting the first page again. This can make for tricky debugging if you are deliberately implementing redirects and want to ensure that they're working correctly. :)

So, it's very important that you only use 301 when you really truly mean that it's a permanent redirect. If you want a temporary redirect, use 302.

As usual, Wikipedia has a good rundown of the codes: https://en.wikipedia.org/wiki/List_of_HTTP_status_codes

I actually use this site for referencing some of the less common ones since the URL is so memorable: http://httpstat.us/

One last gotcha to remember with 301 and 302 redirects: post data payloads are *not* preserved across the redirection. I'm guessing this isn't something you (Drew) care about since you're just cutting traffic off and redirecting early in the user's session, but it could bite you down the road if you're not careful with how the destination site is coded (for instance if there are any forms that post to URLs on the old domain).

drewprops
2013-08-30, 07:26
I imagine that serving up the right codes is incredibly important to someone who codes pages for a site that sells on-demand anything.

Here's a curiosity about the .htaccess file that I did not feel needed mentioning previously. As part of Earthlink's setup (old-timey?) it appears they needed to predefine certain variables for the web server to use.


DirectoryIndex index.html index.webc home.html index.htm index.shtml home.htm index.cgi home.cgi index.php4 index.php5 index.php default.htm default.html index.phtml
AddType application/x-httpd-php .php .phtml .php3 .php5 .php4


So I'll poke around with this some more when I wake up, but it sure LOOKS like T's suggestion ought to work right out of the box.



...

turtle
2013-08-30, 13:44
You'll also want to be sure www is pointing to the same place and non-www. In the servers I work with, www is a symlink to the non-www document root. It's possible that the www on that server has it's own folder and own .htaccess.

Thanks for that link Brad, didn't think of looking at wikipedia since I generally avoid it for most searches. :p

drewprops
2013-09-03, 17:16
Could it be as simple as the DNS not being set up to go to a "www" directory?

Is that a thing?



...

turtle
2013-09-03, 17:51
Yes, PM me the actual domains you are working with and I'll help you figure out exactly what's going on with this.

drewprops
2013-09-04, 23:12
RESOLVED!

The problem was indeed a DNS issue.

Earthlink handles this stuff strangely.

Turtle is a BOSS.

...