Welcome to WebmasterWorld Guest from 54.227.125.200

Forum Moderators: Ocean10000 & incrediBILL & phranque

Message Too Old, No Replies

Canonicalization: best code to redirect no-www to www & index to /

Canonicalization: best code to redirect no-www to www & index to /

     

ewwatson

8:00 pm on Oct 15, 2012 (gmt 0)

5+ Year Member



Canonicalization: is this the best code to redirect no-www to www & index to root? I've read around the web for a couple days now and this is the best code I can find. Should I include Options +FollowSymLinks? And is this the leanest this can be to accomplish all it does? Thanks!

[size=2]Options +FollowSymLinks
RewriteEngine On
# redirect index.htm and index.html to /
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /.*index\.html?\ HTTP/
RewriteRule ^(.*)index\.html?$ http://www.mydomain.com/$1 [R=301,L]

# redirect no-www to www.
RewriteCond %{HTTP_HOST} ^mydomain\.com$ [NC]
RewriteRule ^(.*)$ http://www.mydomain.com/$1 [R=301,L][/size]

helenp

6:09 pm on Jan 15, 2013 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



There must be some other rule elsewhere that conflicts.

Do we need this line?
RewriteRule !index\.html? - [S=2]


I have tried both yours and themadscientist, both with and without that line, and sometimes it worked then suddenly I got a https file when I wrote index.htm in an http adress.
So it looks like there is some strange cache issue when swithch form https to http
however it works, thanks

ewwatson

6:23 pm on Jan 15, 2013 (gmt 0)

5+ Year Member



Now that you guys are here I will reference my original question. I'm still a bit confused on how to proceed?

I'm back! New issue with same code. If I don't have a index file in one of the addon domains it redirects to the main domain. Is this normal behavior for the below htacces code? Or is something amiss? Thanks

# 301 permanent redirect index.html(htm) to folder with exclusion for addon domains
RewriteCond %{HTTP_HOST} !(addondomain\.com|addondomain\.com|addondomain\.com|addondomain\.com|addondomain\.com)
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /([^/]+/)*index\.html?\ HTTP/
RewriteRule ^(([^/]+/)*)index\.html?$ ht tp://w ww.maindomain.com/$1 [R=301,L]

# 301 permanent redirect non-www (non-canonical) to www with exclusion for addon domains
RewriteCond %{HTTP_HOST} !(addondomain\.com|addondomain\.com|addondomain\.com|addondomain\.com|websitecodetutorials\.com)
RewriteCond %{HTTP_HOST} !^(www\.maindomain\.com)?$
RewriteRule (.*) ht tp://w ww.maindomain.com/$1 [R=301,L]

Note I put spaces in the main domain - I forgot the domain to use to bypass that.

lucy24

10:58 pm on Jan 15, 2013 (gmt 0)

WebmasterWorld Senior Member lucy24 is a WebmasterWorld Top Contributor of All Time Top Contributors Of The Month



How did we end up with three different discussions in the same thread?

Do we need this line?
RewriteRule !index\.html? - [S=2]

Heh. I just commented on the [S=2] flag in That Other Thread. Helen, be absolutely sure you understand what it does. Of course this caution applies to everything in mod_rewrite-- and everything else in htaccess. But it goes double for [S=some-number]. Sometimes there's no choice. But here there are simple alternatives, like setting an extra Condition on those two following Rules.

Now that you guys are here

Somewhere in the middle of the previous page I suggested that your problem may have nothing to do with htaccess at all and is simply what your host does when you have a domain with no content. And, ahem, you forgot to read the forum charter and the Sticky. (Er, it is called a Sticky isn't it? php/bb vocabulary is everywhere...)

helenp

7:04 am on Jan 16, 2013 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Do we need this line?
RewriteRule !index\.html? - [S=2]

Heh. I just commented on the [S=2] flag in That Other Thread. Helen, be absolutely sure you understand what it does.

As I understand it says if the following is not index.htm skip the next two rules, ie that make it not read the rules if not index.htm. Sorry, did not understand you very well in the other thread.
Thanks

ewwatson

6:42 pm on Jan 16, 2013 (gmt 0)

5+ Year Member



Ok I'll test. I'm going to remove the htaccess and the content in the addon domain and see what happens...

ewwatson

2:28 am on Mar 26, 2013 (gmt 0)

5+ Year Member



Hello. A while back you guys gave me this code and added exclusion for my addon domains. These are not sub domains. But separate domains with their own folders sitting in the public_html (same as www prob). Without the exclusions all urls would redirect to the main domain. So all was/is good. But now I'm getting into the domain biz and need to host many domains. Each time I add a domain I have to go into the main sites htaccess and add it to the code for exclusion. It's a pain. Is there anyway to rewrite this code so it automatically does it so I don't have to enter each new domain?

So root ( / ) is were the maindomain is. And /addondomain is where the addon domains are.

# 301 permanent redirect index.html(htm) to folder with exclusion for addon domains
RewriteCond %{HTTP_HOST} !(example\.com|example\.com|example\.com|example\.com|example\.com|example\.com)
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /([^/]+/)*index\.html?\ HTTP/
RewriteRule ^(([^/]+/)*)index\.html?$ http://www.example.com/$1 [R=301,L]

# 301 permanent redirect non-www (non-canonical) to www with exclusion for addon domains
RewriteCond %{HTTP_HOST} !(example\.com|example\.com|example\.com|example\.com|example\.com|example\.com)
RewriteCond %{HTTP_HOST} !^(www\.example\.com)?$
RewriteRule (.*) http://www.example.com/$1 [R=301,L]


Thank you so much,
Eric W

lucy24

5:42 am on Mar 26, 2013 (gmt 0)

WebmasterWorld Senior Member lucy24 is a WebmasterWorld Top Contributor of All Time Top Contributors Of The Month



But now I'm getting into the domain biz and need to host many domains. Each time I add a domain

Frankly I think you need to change hosts. Sure, you can have lots of domains living on the same server. But they need to be parallel: not a single "primary" domain and a bunch of addons. Subdirectories are for subdomains. Even something as simple as multiple domains within a single userspace will fix you up. Put the canonicalization redirects into each domain's own directory.

Your exact setup will depend on your hosting. Mine goes in two tiers:
shared htaccess in my userspace. This one is for mod_setenvif and mod_authzzzz only. If an IP or UA gets blocked, they're blocked everywhere.
separate htaccess for each domain. This one is for mod_rewrite only. There's a tiny bit of overlap, because some domains do have the same RewriteRules, and some things like Options -Indexes won't work in the shared htaccess. But essentially everything at this level is domain-specific.

ewwatson

11:17 am on Mar 26, 2013 (gmt 0)

5+ Year Member



It's Hostgator though. Totally logic co

ewwatson

11:19 am on Mar 26, 2013 (gmt 0)

5+ Year Member



I don't want to take the time to switch at the moment. There has to be a way to write it so it auto does it? Please tell/show me as if I was a toddler.

ewwatson

6:08 pm on Mar 26, 2013 (gmt 0)

5+ Year Member



Here is a answer I reseved at Sitepoint. Is this up to snuff?


To stip off index.html, this seems to do the trick.

RewriteRule ^(.*/)?index\.html?$ /$1 [R=301,L]

And to add "www", this should work.

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

Neither of these hardcode any domain name, so it shouldn't matter whether you access your site through an addon domain.

g1smd

6:13 pm on Mar 26, 2013 (gmt 0)

WebmasterWorld Senior Member g1smd is a WebmasterWorld Top Contributor of All Time 10+ Year Member Top Contributors Of The Month



The index redirect is an affront to decent coding. NEVER use (.*) at the beginning or in the middle of a RegEx pattern. Without a RewriteCond testing THE_REQUEST, the rule will generate an infinite loop.

The www redirect is below optimimum. Specifically, it fails to redirect a number of non-canonical hostname requests (e.g. www.example.com:80 and others).

Use example.com in this forum to suppress URL auto-linking.

ewwatson

9:27 pm on Mar 26, 2013 (gmt 0)

5+ Year Member



@g1smd

I would much rather use your code as I trust your experience here. You worked up that code for me in my msg:4558494. Is there anyway you can rework that so I don't have to add each new addon domain to the list with each new site I pick up? Thanks!

lucy24

11:52 pm on Mar 26, 2013 (gmt 0)

WebmasterWorld Senior Member lucy24 is a WebmasterWorld Top Contributor of All Time Top Contributors Of The Month



The problem with generic code is that the optimal canonicalization rule is expressed as a negative. So you'd have to say

!^(?:www\.(\w+)\.com)?$

... and then you're in trouble, because the target has to involve %1 but there is no %1. You can't capture from a negative.

:: racking brains ::

%{HTTP_HOST} !^www\. [OR]
%{HTTP_HOST} !\.com$
%{HTTP_HOST} (\w+)\.com

I don't care for the looks of that :( And there's the HTTP/1.0 problem.

ewwatson

11:28 am on Mar 27, 2013 (gmt 0)

5+ Year Member



You guys say you bore of the same old question asked over and over again. Isn't this a new question? I would of assumed you'd enjoy flexing your brain for this one?

phranque

1:21 pm on Mar 27, 2013 (gmt 0)

WebmasterWorld Administrator phranque is a WebmasterWorld Top Contributor of All Time 10+ Year Member Top Contributors Of The Month



have you considered using an External Rewriting Program?
http://httpd.apache.org/docs/current/rewrite/rewritemap.html#prg

ewwatson

3:43 pm on Mar 29, 2013 (gmt 0)

5+ Year Member



Well if you guys won't help then I guess I have no choice but to use the flawed code above. :(

ewwatson

9:54 am on Apr 1, 2013 (gmt 0)

5+ Year Member



Bump. Still waiting on some help
This 77 message thread spans 3 pages: 77
 

Featured Threads

Hot Threads This Week

Hot Threads This Month