homepage Welcome to WebmasterWorld Guest from 54.167.182.201
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Become a Pro Member

Home / Forums Index / Code, Content, and Presentation / Apache Web Server
Forum Library, Charter, Moderators: Ocean10000 & incrediBILL & phranque

Apache Web Server Forum

This 77 message thread spans 3 pages: < < 77 ( 1 2 [3]     
Canonicalization: best code to redirect no-www to www & index to /
Canonicalization: best code to redirect no-www to www & index to /
ewwatson

5+ Year Member



 
Msg#: 4508251 posted 8:00 pm on Oct 15, 2012 (gmt 0)

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

WebmasterWorld Senior Member 10+ Year Member



 
Msg#: 4508251 posted 6:09 pm on Jan 15, 2013 (gmt 0)

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

5+ Year Member



 
Msg#: 4508251 posted 6:23 pm on Jan 15, 2013 (gmt 0)

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

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



 
Msg#: 4508251 posted 10:58 pm on Jan 15, 2013 (gmt 0)

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

WebmasterWorld Senior Member 10+ Year Member



 
Msg#: 4508251 posted 7:04 am on Jan 16, 2013 (gmt 0)

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

5+ Year Member



 
Msg#: 4508251 posted 6:42 pm on Jan 16, 2013 (gmt 0)

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

ewwatson

5+ Year Member



 
Msg#: 4508251 posted 2:28 am on Mar 26, 2013 (gmt 0)

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

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



 
Msg#: 4508251 posted 5:42 am on Mar 26, 2013 (gmt 0)

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

5+ Year Member



 
Msg#: 4508251 posted 11:17 am on Mar 26, 2013 (gmt 0)

It's Hostgator though. Totally logic co

ewwatson

5+ Year Member



 
Msg#: 4508251 posted 11:19 am on Mar 26, 2013 (gmt 0)

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

5+ Year Member



 
Msg#: 4508251 posted 6:08 pm on Mar 26, 2013 (gmt 0)

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

WebmasterWorld Senior Member g1smd us a WebmasterWorld Top Contributor of All Time 10+ Year Member



 
Msg#: 4508251 posted 6:13 pm on Mar 26, 2013 (gmt 0)

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

5+ Year Member



 
Msg#: 4508251 posted 9:27 pm on Mar 26, 2013 (gmt 0)

@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

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



 
Msg#: 4508251 posted 11:52 pm on Mar 26, 2013 (gmt 0)

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

5+ Year Member



 
Msg#: 4508251 posted 11:28 am on Mar 27, 2013 (gmt 0)

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

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



 
Msg#: 4508251 posted 1:21 pm on Mar 27, 2013 (gmt 0)

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

ewwatson

5+ Year Member



 
Msg#: 4508251 posted 3:43 pm on Mar 29, 2013 (gmt 0)

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

ewwatson

5+ Year Member



 
Msg#: 4508251 posted 9:54 am on Apr 1, 2013 (gmt 0)

Bump. Still waiting on some help

This 77 message thread spans 3 pages: < < 77 ( 1 2 [3]
Global Options:
 top home search open messages active posts  
 

Home / Forums Index / Code, Content, and Presentation / Apache Web Server
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Home ¦ Free Tools ¦ Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About ¦ Library ¦ Newsletter
WebmasterWorld is a Developer Shed Community owned by Jim Boykin.
© Webmaster World 1996-2014 all rights reserved