Forum Moderators: phranque
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.
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
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
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 :)
Version 1:
RewriteEngine On
RewriteCond %{HTTP_HOST} !^www\.widget\.com
RewriteRule .* http://www.widget.com/%{REQUEST_URI} [R=301,L]
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?
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...]
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]
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
<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]
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