Forum Moderators: phranque

Message Too Old, No Replies

Redirect 301 - how is this really done?

redirecting widget.com to www.widget.com

         

Shadow

2:30 pm on Jan 15, 2003 (gmt 0)

10+ Year Member



I'm trying to do a 301 redirect from widget.com to www.widget.com (google PR), but I just can't get it right. The server only loops, or doesn't respond.

Reading through the old posts, I've found a bunch of info, but no authoritative answer. This is what I've tried :

Redirect 301 [widget.com...] [widget.com...]
Redirect 301 [widget.com...] [widget.com...]
Redirect 301 / [widget.com...] [widget.com...]
Redirect 301 [widget.com...]
Redirect 301 / [widget.com...]

I have no trouble redirecting for directories, files or to another domain, like this...

Redirect 301 /dir [widget.com...]
Redirect 301 /oldpage.html [widget.com...]
Redirect 301 / [newdomain.com...]

... it's from http:// to [www...]
that doesn't work. It's on Apache of course. What am I doing wrong? I don't want to use rewriterule.

jdMorgan

4:33 pm on Jan 15, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Shadow,

The basic problem is that the "from" address (the URL on the left side) must be a local path. Including "http://www.yourdomain.com" is not allowed and won't work. Only the right side - the "to address" should include the full URL.

You should use:


Redirect 301 / http://www.widget.com/
Redirect 301 /somedir/ http://www.widget.com/somedir/
Redirect 301 /somedir/oldpage.html http://www.widget.com/somedir/newpage.html

This is from the Apache mod_alias documentation [httpd.apache.org]

Note that there are some strange behaviours if your site is hosted on something like geocities or an ISP account where "~username" is part of the path. If so, you have to include it in the Redirect:


RedirectPermanent /~useraccount/ http://www.yourdomain.com/~useraccount/

---

Now, the question comes up as to whether [domain.com...] and [yourdomain.com...] are actually the same URI space - the same actual files served from the same server account. If so, you have another problem: How to do the above redirects conditionally. You do want to redirect any requests for [yourdomain.com...] to [yourdomain.com,...] but you must not redirect requests for [yourdomain.com,...] or the server will get stuck in an endless redirect loop.

There may be some easy way to do this using mod_setenvif [httpd.apache.org] and RedirectMatch to make the redirect conditional, but I've never used that approach because I find using mod_rewrite to be much simpler. Since you said you don't want to use mod_rewrite, please post if you get a workable and compact solution using SetEnvIf - I would like to see it and save it for later use.

HTH,
Jim

Shadow

1:33 am on Jan 16, 2003 (gmt 0)

10+ Year Member



Morgan,
THAT's what I call an authoritative answer! Great reply, thanks. This one should be in the library.

Yes, the problem is the server is looping. So that means I'll have to explore mod rewrite. Can you tell me if this is correct:

RewriteEngine On
RewriteCond %{HTTP_HOST}!^http://.www.widget.com/.*$ [NC]
RewriteRule ^.*$ [widget.com...] [R=301,L]

I have also seen the third line written like this, without the uri request:
RewriteRule ^(.*) [widget.com...] [R=301,L]

Do I need the *$ at the end of the second line?
Is it HTTP_HOST or HTTP_REFERER in the second line?

And just to be sure, can I mix mod rewrite with simple rules in my htaccess, like:
Redirect 301 /oldpage.html [widget.com...]

Thanks for answering :)

jdMorgan

2:16 am on Jan 16, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Shadow,

Version 1:


RewriteEngine On
RewriteCond %{HTTP_HOST} !^www\.widget\.com
RewriteRule .* http://www.widget.com/%{REQUEST_URI} [R=301,L]

Leave the "/.*$" off the end of the pattern in the first line - it's not needed and can cause problems if someone appends a host port number, which is a legal thing to do. You must tell mod_rewrite that you want to match periods by preceding them with "\". Otherwise they take on a special meaning of "any single character". An example is the pattern in the rewrite rule above, where "." means "any single character," and "*" means "any number of the preceding".

Version 2:


RewriteEngine On
RewriteCond %{HTTP_HOST} !^www\.widget\.com
RewriteRule (.*) http://www.widget.com/$1 [R=301,L]

In this case, parenthesized pattern on the left side of the rewriterule is back-referenced by the "$1" on the right. Since it will contain the requested URI, this rewrite works identically to the first version above. Use whichever you like, but I suspect the second version is slightly faster, since it only has to look up the value of REQUEST_URI environment variable once. But that's just speculation on my part, I've never researched it or tested it.

Do I need the *$ at the end of the second line?

No

Is it HTTP_HOST or HTTP_REFERER in the second line?

Not sure what you mean. HTTP_HOST will have the domain name of your server. HTTP_REFERER will contain the URL of the page which referred this request, or it will be blank (and often is, plan for it). The variable tested against the pattern in RewriteRules (as above) is the REQUEST_URI - "/index.html" for example.

And just to be sure, can I mix mod rewrite with simple rules in my htaccess, like:
Redirect 301 /oldpage.html [widget.com...]

You can mix and match any Apache module directives which are specified as being available in .htaccess context - see the Apache documentation for each module and its directives.

I strongly suggest looking at the mod_rewrite documentation cited in this excellent post [webmasterworld.com], and researching why and how the above rewrites work. To continue a long tradition, mod_rewrite is like a very sharp knife - very useful, but dangerous if used without care... Start simple, test, build up from there, enjoy!

HTH,
Jim

[edited by: DaveAtIFG at 4:05 pm (utc) on Mar. 25, 2003]
[edit reason] Corrected code at Jim's request [/edit]

Shadow

1:43 am on Jan 17, 2003 (gmt 0)

10+ Year Member




Morgan, that's another great answer! And a very nice reference to that other thread.

I have been to swamped to try it today, but I'll get to it tomorrow. I'm confident this will work just nicely.

Again, thankyou ;)

andreasfriedrich

9:14 am on Jan 17, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Jim wrote
There may be some easy way to do this using mod_setenvif and RedirectMatch to make the redirect conditional

There actually might be such a way but I believe there is not. The only directives that use environment variables [httpd.apache.org] are Allow, CustomLog, Deny, LogFormat, RewriteCond, and RewriteRule. So one would be able to set an environment variable depending on the Host header field but there was no way other than mod_rewrite to make use of them.

Please post when you have found a way to do this w/o mod_rewrite.

Andreas

Ryders

3:02 am on Feb 5, 2003 (gmt 0)

10+ Year Member



simple, easy, fast, efficient... me? no! this :

<script>
var strValue = window.location.href;
var value = strValue.indexOf("www");
if (value == -1) {
document.location.href = "http://www.example.net";
}
else
{
document.write('<title>:: w w w . e x a m p l e . n e t ™ >></title>');
}
</script>

but I'd still be interested in knowing how to set those environement variables for the Redirect Permanent not to loop continually..! ;)

cheers..

Ryders

[edited by: Ryders at 3:10 am (utc) on Feb. 5, 2003]

[edited by: DaveAtIFG at 9:34 am (utc) on Feb. 5, 2003]
[edit reason] Removed unnecessary specifics [/edit]

jdMorgan

3:29 am on Feb 5, 2003 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Ryders,

I looked through the documentation, and andreasfriedrich was correct - no great surprise to anyone - the only solution within Apache's standard modules is to use mod_rewrite.

Examples of how to resolve the multiple-domains on one hosting account issues are plentiful here on WebmasterWorld, so I won't post yet another one. :)

Jim