Forum Moderators: phranque

Message Too Old, No Replies

Correctly Doing A 301 Error With .htaccess

         

jshpik1

8:21 am on Aug 15, 2007 (gmt 0)

10+ Year Member



I'm so angry right now. I've tried every variation of the following to go from http://example.com to http://www.example.com and nothing! I get 302 errors. Note that I get 302 even without the code in htaccess. RedirctPermanent or Redirect 301 would make this so much easier, but the host doesn't allow that for "security reasons."


RewriteEngine On
RewriteBase /

# You could need to un-comment the following line, gives 500 server error
# Options +FollowSymlinks

# Redirect from http://example.com to http://www.example.com

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

# rewritecond %{http_host} ^example.com [nc]
# rewriterule ^(.*)$ http://www.example.com/$1 [r=permanent,nc]

# This one did a 301 from http://www.example.com to http://example.com properly
# RewriteCond %{HTTP_HOST} .
# RewriteCond %{HTTP_HOST} !^example\.com
# RewriteRule (.*) http://example.com/$1 [R=301,L]

# RewriteCond %{HTTP_HOST} ^example.com [NC]
# RewriteRule ^(.*)$ http://www.example.com/$1 [L,R=301]

# rewritecond %{http_host} ^example.com [nc]
# rewriterule ^(.*)$ http://www.example.com/$1 [r=301,nc]

# RewriteCond %{HTTP_HOST} ^example.com
# RewriteRule (.*) http://www.example.com/$1 [R=301,L]

# Don't even know where to put this... in the header file? Didn't do anything...
# <?php
# if(!stristr($_SERVER["HTTP_HOST"], 'www')){
# header("HTTP/1.1 301 Moved Permanently");
# header("Location: http://www.example.com/" . $_SERVER["REQUEST_URI"]);
# exit();
# }
#?>

# Statements Not Supported By Host
# Action
# AddCharSet
# AddEncoding
# AddLanguage
# AddType
# BrowserMatch
# BrowserMatchNoCase
# CookieTracking
# DefaultLanguage
# DefaultType
# ErrorDocument
# ErrorHeader
# Header
# LanguagePriority
# Options
# PassEnv
# Redirect
# RedirectMatch
# RedirectPermanent
# RedirectTemporary
# SetEnv
# SetEnvIf
# SetEnvIfNoCase
# UnsetEnv

Any help is greatly appreciated.

[edited by: jdMorgan at 11:23 pm (utc) on Aug. 19, 2007]
[edit reason] example.com [/edit]

jdMorgan

2:18 pm on Aug 15, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Assuming that you uncommented each in turn, most of your trial code snippets should have worked.

Begging your pardon for being forthright, but you need a new host, as this one appears to be both too restrictive and --based on their given reason for not allowing mod_alias-- incompetent.

If you want to modify your server configuration with .htaccess, consider spending the $14.95 a month (this should be a maximum price for a single account) it takes to move up to a decent class of shared hosting -- It will be well worth it in your time-savings alone. Eschew hosting resellers and go straight to a provider that owns its own datacenters. Note that I don't have any particular prejudice against resellers, but if you have a problem, you'll want to be able to go straight to the people who own/administrate the hardware, and not have to deal with extra problems like a reseller who's out of the country on holiday for a month...

Considering the time spent on the problems it causes, cheap hosting is the most expensive hosting you can buy.

The code snippet you've included that starts with "<?php" is a PHP script. You should give it a filename and placed in a file anywhere within your Web-accessible space where scripts may be executed (i.e. where they can be given the required 'execute' permissions). When things are set up properly, you should then be able to run it by typing its URL.

Jim

g1smd

9:00 pm on Aug 15, 2007 (gmt 0)

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



>> # Statements Not Supported By Host <<

You gotta be kidding.

That isn't a proper hosting company.

They are a bunch of jokers.

Run away. Very fast.

.

I pay about GBP 90 for two years hosting (that's about USD 180 at the moment) that allows me to do all of that, and more.

jshpik1

5:11 am on Aug 19, 2007 (gmt 0)

10+ Year Member



This is $100 / yr hosting, so it's not like one of the $2.99 ones that does it monthly. I plan to move once my time expires. But for now I'll make do with what I have rather than spend more money.

The PHP code does redirect properly, but I need to include it in the header file, because every page that's http:// should go to www. with a 301. Doing that with that code just gives infinite loop if it's in the header because it keeps redirecting once it gets to it's destination. I'm not sure how to write a script to do this in PHP without causing it to loop.

I'm almost positive the host has it setup to go to www. with a 302 automatically, but they are like you said... incompetent, the customer service doesn't even know what I'm talking about it seems.

jdMorgan

11:27 pm on Aug 19, 2007 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Try using the "Live HTTP Headers" extension for Firefox and Mozilla browsers -- It will show you every request and every server response, allowing you to 'follow' the redirect process and figure out why the PHP redirect is looping -- and where the 302 (if any) is coming from.

Jim

g1smd

12:26 am on Aug 20, 2007 (gmt 0)

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



After the millions of words written in the last few years about Google and their handling of 302 redirects, any host that sets up the www/non-www redirect as a 302 is beyond clueless.