Need help tweaking this htacces code please?

Hello. A while back with lots of help I put together 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} !(addondomain\.com|addondomain\.com|addondomain\.com|addondomain\.com|addondomain\.com|addondomain\.com)
RewriteCond %{THE_REQUEST} [1]{3,9}\ /([^/]+/)index\.html?\ HTTP/
RewriteRule ^(([^/]+/)
)index\.html?$ http://www.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|addondomain\.com|addondomain\.com)
RewriteCond %{HTTP_HOST} !^(www\.example\.com)?$
RewriteRule (.*) http://www.maindomain.com/$1 [R=301,L]

Thanks!


  1. A-Z ↩︎

Hi Eric,

This is simply a logic problem (converting not’s to is’s) - I recall the term “contrapositive” from too many years ago to remember exactly what that is but …

PLEASE wrap your code in [noparse]

...

[/noparse] tags as that preserves the code when quoting for a reply.

# 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|addondomain\\.com) 
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\\ /([^/]+/)*index\\.html?\\ HTTP/
RewriteRule ^(([^/]+/)*)index\\.html?$ http://www.maindomain.com/$1 [R=301,L]

It looks like you simply want to redirect maindomain.com to www.maindomain.com so

RewriteCond %{HTTP_HOST} ^maindomain\\.com [NC]
RewriteRule .? http://www.maindomain.com%{REQUEST_URI}

Here, I simply targeted the ONE domain you were targeting and checking whether there was a www. subdomain before redirecting. I’m not sure what you were doing with ^(([^/]+/)/) index\.html?$ except to match every index.html file … but why not all files (your attempt to only change index.html file subdomains to www doesn’t make sense to me). Therefore, in my redirection, I simply redirected to the original URI. If you, indeed, only want to redirect index.html files, then ^(./)index\.html?$ and /$1index.html would be easier to understand.

# 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|addondomain\\.com|addondomain\\.com)
RewriteCond %{HTTP_HOST} !^(www\\.example\\.com)?$
RewriteRule (.*) http://www.maindomain.com/$1 [R=301,L]

Okay, that’s what I was whining about above. If it works for every file, why separate out the index.htm(l) files and treat them differently? If you’re simply trying to remove the DirectoryIndex file(s), do it that way (and force the server to find the file and serve it after it’s been stripped from the URI?!?).

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

Regards,

DK

Thank you so much! I am a dunce when it comes to htacces stuff. Can you pretty please just show the whole bit I should add. From your examples I dont know which order or which to include?

I’ll be honest… I’m a little confused about what you’re trying to accomplish.

In your first rule, it looks like you want to strip “index.html” from the end of a URL… unless you’re accessing the site from an addon domain… then you skip this rule.

In the second rule, the comments say it’s to redirect to a non-www URL. Yet you then redirect to a www URL. What did you actually want to accomplish with this rule?

No prob I will explain. Maybe you can simplify it. Guys over at webmasterworld with like 30,000 posts and most in the server forum put that together. Based on that and the way they spoke I assumed they new what they were doing. But who knows?

What I’m doing is Canonicalization. Canonicalization is when you can get to a domain in like 10 different ways your site juice can be spread between them. So the first rule is stripping the index if present so all directs to folders. That’s the one that probably redirects the addon domains. The second rule is making sure all links without www are redirected to the www version of the site.

The main site sits in the root obviously at /. And the addon domains at root/folder - so /addon. The host is Hostgator. Apparently that’s not a common set up because most are confused by it.

If you know of cleaner way to accomplish this that doesnt redirect the addon domains to the root domain and does it automatically without me having to add each new addon to the list I’m all eager ears!

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.

Thanks Jeff. Here is what the other guy that provided the original code had to say about your code here. Unfortunately he offered no alternative.

"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). "

Any validity to any of that? Or is he just talking out his ass? Since this has very direct influence on my seo. And since this is for my site that puts food on my table, I have to take this very seriously. And not just use the first thing given. I need to know its the best for what it’s intended.

Actually the use of (.*) is both standard practice and frequently used even in the Apache documentation itself. In fact, the authors of Apache use it in exactly the same way that this other guy says is “an affront to decent coding.” I don’t see any validity to his argument (there is no infinite loop), and I trust the Apache documentation a lot more than some random person.

The goal of the second rewrite rule, so far as I know, was to add “www.” to URLs that don’t have it. His example URL does have it. So why should it be redirected?

Perhaps the “issue” he meant to highlight is the port? So, for example, maindomain.com:8080 would redirect to www.maindomain.com (without the port). And that’s true. However, if you know that your site is running on port 80 – as virtually every site is – then you’re safe to leave off the port. And once again, the Apache documentation backs me up on this. They show a port example, and they say it’s only “for sites running on a port other than 80.” I don’t think that applies to you. In fact, that applies to almost no one.

Thanks Jeff. I will take your word for it. I didnt intend to challenge you. Thanks for the explanation!

How can I confirm port number? So just to confirm… I can replace this…

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

With simply this…

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

So this will rewrite all index’s to folder? And rewrite all no www to include www? And not include addon domains? So if I want the same on the addons then I am to use the same set of rules in its htaccess? Is that all correct??? Oh and will it also add a " / " to the end of .com? So .com/

Thank you for your time!

You can search your Apache config for “Listen”. That’s where you can check what ports your server is configured to work on. Also, if your site domain when typed in the address bar doesn’t use a port number at all, then you’re on port 80.

By and large, yes. I say “by and large” because there is a difference in behavior between the two. Though, based on what you wanted to achieve, these may be good differences. For example, in your original rewrite rule, index.html would be stripped from your maindomain URLs, but not from your addondomain URLs. Whereas the new rewrite rule will strip index.html from the URL regardless of the domain. Ditto for the www. Your original rule would not add www to your addon domains, whereas the new rule will.

All sounds good. Just two remaining questions for ya then!? Will this also add a slash at the end of the .com/ if none is present? And based on your description, then I need NOT to add this code to my addon domains htaccess as well then? Correct? Thanks.

Correct.

No. I’m not even sure if it’s possible to control this. The HTTP request will always send the slash. But whether that slash is shown in the address bar seems to depend on the browser. The latest versions of Firefox and Chrome appear to not show the slash, even if I explicitly type it in, while IE9 seems to always show the slash, even if I explicitly leave it off.

The original code dos this. This is a must! See you can test with this link http://www.websitecodetutorials.com/. If you leave it off its rewritten in. Yes some browsers don’t show it but if you copy the URL they will paste it as you have it written. Because with it off and on can be two dif URLs. A Canonicalization issue.

Not in Chrome it isn’t. Nor in Firefox. Nor is there a network request to redirect to a different URL.

Huh. I type it in without the slash on my ipad and it refreshes with the slash. Is been a while but if memory serves me that’s part of the the Canonicalization issues.

Just tested it on Firefox. No it’s there! Copy it. And paste it. When pasted it is there. Firefox just doesn’t show it. But it’s there.

That’s correct. It’s always there. Some browsers show it, some don’t. But your rewrite rules have no power to control whether it’s shown or not. Nor does it represent a difference resource (and thus not a canonical issue). site.com and site.com/ represent the same resource, just like site.com and site.com:80 represent the same resource.

Hey Jeff. Like here this page discusses enforcing trailing slash in #1 and says its a canonical issue http://ruslany.net/2009/04/10-url-rewriting-tips-and-tricks/

And here Matt Cuts says put the slash

"Matt January 4, 2006 at 10:36 am
Tony Hill, great question about http://www.example.com vs. http://www.example.com/ . This is one of those cases where I’d pick a preferred format and stick with it uniformly. I would lean toward having the trailing slash, because that’s what most people expect. Maybe I’ll just duck in and update that in the post… " here http://www.mattcutts.com/blog/seo-advice-url-canonicalization/

Yes, but they’re not talking about slashes after the hostname. They’re talking about slashes at the end of a path. So, for example, these URLs are different:

site.com/path
site.com/path/

That’s what the article was talking about, and they’re absolutely right about that. But these URLs are the same:

site.com
site.com/

In the former, the path is implicitly “/”.