Forum Moderators: phranque

Message Too Old, No Replies

redirection of http:// to http://www using .htaccess

         

joeduck

4:27 pm on May 19, 2005 (gmt 0)

10+ Year Member



I keep failing to figure out the correct line
in my .htaccess file to do a global redirection of http:// to [www...] for all the files in the site - any help would be appreciated.

jdMorgan

10:21 pm on May 19, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Please post the code you've tried, so we can discuss it.

Or try a search [google.com].

Jim

joeduck

12:28 am on May 20, 2005 (gmt 0)

10+ Year Member


This is the line I have now that is NOT working:

Redirect http://quickaid.com/*.* http://www.quickaid.com/*.*

jdMorgan

12:48 am on May 20, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



If both domains are hosted on the same server, and you do not have httpd.conf privileges, you'll have to use mod_rewrite in .htaccess; Redirect [httpd.apache.org] accepts only a local URL-path as its first argument, not a canonical URL (http://...). Therefore Redirect cannot be used to 'test' the requested domain name.

Also, Apache does not accept Windows-type wild-cards such as "*.*" -- Apache uses regular expressions (see our Forum Charter [webmasterworld.com] for a reference).

The search I posted above will lead you to working examples of the mod_rewrite method.

Jim

joeduck

1:06 am on May 20, 2005 (gmt 0)

10+ Year Member


Frustrating! I tried this but no luck either:

# Force www canonical hostname
RewriteCond %{HTTP_HOST} ^example\.com
rewriterule (.*) http:/[smilestopper]/www.example.com [R=301,L]
#
# Rewrite all requests to www subdirectory, but prevent rewrite looping
RewriteCond $1!^www/
rewriterule (.*) /www/$1 [L]
#
# Forbid direct-request access to /www subfolder
RewriteCond %{THE_REQUEST} ^[A-Z]+\ /www/
rewriterule .* - [F]

jdMorgan

3:02 am on May 20, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Can't help much without a detailed definition of "no luck."

Jim

joeduck

5:29 am on May 20, 2005 (gmt 0)

10+ Year Member


Hi Jim -

I'm trying to avoid a "duplicate content" problem by using htaccess to force any page request to give the http://www.mysite.com/page.htm version rather than http://mysite.com/page.htm. Right now I show both www and non www versions of all my pages.

joeduck

7:37 am on May 24, 2005 (gmt 0)

10+ Year Member


OK, this one is working fine:

RewriteEngine on
RewriteCond %{http_HOST} ^example\.com
RewriteRule ^(.*) http:/[smilestopper]/www.example.com/$1 [L,R=301]

jdMorgan

1:21 pm on May 24, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Glad you got it working!

We have a major problem here, in that it's impossible for members to look over each other's shoulders to examine the test results on the monitor and make suggestions while testing. We needed to know how your testing went, and what the results were... What error messages did you get, how did the results differ from your expectations, etc. So, that's what I mean when I say, "What did you mean by 'no luck'?"

Anyway, good show, and I hope that code helps you achieve your purpose.

Jim

joeduck

7:02 pm on May 24, 2005 (gmt 0)

10+ Year Member



Thx Jim!

christopherwoods

8:27 pm on May 24, 2005 (gmt 0)

10+ Year Member


Ideally you shouldn't be forcing the www. , it's unnecessary and a hangover from the old days of protocol. As put forward by no-www.org (and others), your redirect should work the other way round, i.e.

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

...But I guess it is your site at the end of the day.

jdMorgan

12:35 am on May 25, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Unless you might eventually want to do DNS-based load-sharing, in which case, not having a machine name would be rather fatal. You'd have to redirect your whole site, wait for months to get re-listed (hopefully without destroying your business), and then set up the load-sharing...

W3C is a great resource, but they sometimes ignore some realities of the Web and its infrastructure.

Jim

christopherwoods

1:14 am on May 25, 2005 (gmt 0)

10+ Year Member



I understand that BIND needs a CNAME entry to add multiple subsequent entries to, as the SRV-type entry is still not fully-supported... But couldn't lbnamed handle a class-2 domain-with-load-balancing fairly easily? (considering its very existence is to facilitate load-balancing)...

... I ask as a query, as I've never had the opportunity to use lbnamed personally. 8)

jdMorgan

2:05 am on May 25, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Me neither, I only watched someone do this on an old set-up, and the machine name was required. It's one of those things where you'd better stick to the lowest common denominator for now, until the newer features are ubiquitous.

Jim

MrSpeed

12:08 pm on May 25, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member


If you have other directives in your .htaccess file
does the order matter that they are in the file? It seems like you could get some wacky loops going on.

RewriteEngine on
RewriteCond %{http_HOST} ^example\.com
RewriteRule ^(.*) http:/[smilestopper]/www.example.com/$1 [L,R=301]

RewriteRule widgets/(.*)/(.*)/ products.php?category=$1&widgets=$2&type=widgets [L]
RewriteRule widgets/(.*)/ products.php?category=$1&type=widgets [L]

--OR--
RewriteEngine on
RewriteRule widgets/(.*)/(.*)/ products.php?category=$1&widgets=$2&type=widgets [L]
RewriteRule widgets/(.*)/ products.php?category=$1&type=widgets [L]

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

jdMorgan

12:41 pm on May 25, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



There is no substitute for a full analysis, but a few rules of thumb are helpful to prevent precedence errors.

General guidelines:

  • Use the most specific regular-expressions patterns possible.
  • Use fully-anchored patterns whenever possible.
  • Place the longest, most-specific-pattern Rules first.
  • Always use the [L] flag, unless you know you have a reason not to.

    In the code you posted, the first point applies, but from an efficiency standpoint: The pattern ".*" is the most ambiguous, least-efficient pattern possible when used for multiple subpatterns as in "(.*)/(.*)". In this case, "([^/]+)/([^/]+)/" or "([^/]+)/([^/]*)/?" would be much faster to process (although I can't be completely sure either will work for you without knowing your entire URL-scheme.)

    Negative-match patterns like "([^/]+)/([^/]+)/" can be processed from left to right in one single pass, whereas evaluating patterns like "(.*)/(.*)" requires multiple trial matches, with the number of required trials proportional to the number of characters in the requested URI.

    Jim

  • MrSpeed

    3:35 pm on May 25, 2005 (gmt 0)

    WebmasterWorld Senior Member 10+ Year Member



    Thanks for that tip.

    But is it better to rewrite the urls to handle
    non-www --> www before or after rewriting the urls to make the urls SE friendly?

    jdMorgan

    4:11 pm on May 25, 2005 (gmt 0)

    WebmasterWorld Senior Member 10+ Year Member



    Take a top-down approach and dispense with the 'incorrect' domain first.

    Jim