Need help tweaking this htacces code please?

Ok I see how every site redirects to the slash. So with and without is considered the same link to google? Not duplicate? I believe you Jeff. Do you know of any pages (or you) that can give supporting evidence that its the same? And why it’s the same? Thanks for endulging me. :slight_smile:

It would be interesting if Matt could confirm whether Google treats them as different URLs, since in this case they would point to the same resource (unlike example.com and www.example.com, where they might point to different resources).

Whatever the case may be, there’s still nothing you can do with rewrite rules or from anything else on the server. The server won’t even know the difference, because the HTTP request will always be for “/”. The only thing you can control is how you format your URLs in your own HTML links.

The HTTP spec says “Note that the absolute path cannot be empty; if none is present in the original URI, it MUST be given as “/” (the server root).”

However, whether google also treats an empty path the same as a root path is a different question. I haven’t been able to find any statements from google that explicitly says one way or the other. Though, I expect that they would treat them the same, since the HTTP spec treats them the same.

And just to reiterate, whether google treats them the same or not doesn’t change that a rewrite rule can’t enforce it. All you can do is be consistent with the way you write your HTML links.

Is it advisable to force a trailing slash via htaccess? http://enarion.net/web/htaccess/trailing-slash/

That’s again talking about slashes at the end of paths, not at the end of hostnames.

If you or anyone else can devise a rewrite rule that forces slashes at the end of hostnames, that would be pretty exciting. But everything I know says this isn’t possible.

Here Matt says how google “probably” treats them. http://www.youtube.com/watch?v=CTrdP7lJ2HU using this search “matt cutts trailing slash or not”

But regardless if the slash is left off the browser redirects to the slash. So it would be better to force the trailing slash before the browser gets it yeah??

Would this did it?

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

http://stackoverflow.com/questions/7149013/how-to-write-in-htaccess-file-to-force-trailing-slash-and-www-at-the-same-time

Ish. “Redirect” is probably the wrong word in this case. If you watch the network console, you’ll see that there isn’t actually a redirect happening. (On the other hand, if you leave the trailing slash off a path directory, then it is redirected, and you can see that redirect in the network console.)

In theory, it’s better to be consistent, and to pick the slash-after-hostname format when writing links. But in practice it makes no difference. There’s no performance cost, because browsers will always send the slash anyway, and there’s no page rank cost, because google can easily deduce that they’re the same resource.

And getting back to your original requirement… wanting rewrite rules to enforce the slash is moot, because it’s still the case that rewrite rules can’t enforce this. And since browsers will send the slash either way, the server can’t even detect if it’s there or not.

You probably could have gotten a faster answer if you tried it yourself. But no, that won’t do it.

Ok thanks Jeff. I’ll throw the code in tomorrow morning and test it all out. I’ll post back with the results. Take care

Morning :slight_smile:

ok seems to work perfect on the main domain. But doesnt seem to perform as mentioned on the addon domains. Without the code added to htaccess in addon nothing is rewritten as it should be - no www stays no www and domain/index.php remains with the index.php. If I add the code to addon htaccess then the no www is redirect to www but the index is not striped off. What do you think?

EDIT: forgot to change it to php extensions. Ok so without the code added to the addons httaccess nothing works. With it added it works just like the main. So you said no need to add code to addon htaccess. Just checking how its working. Should I obviuosly add it to the addon htaccesss too then? Or should we edit the code some to include? Thanks!

I’m a little confused now how your site is configured. Originally it seemed like your maindomain and addondomains all read from the same htaccess, and that’s why you had to exclude the addondomains from your original rewrite rules. But now it sounds like you’re saying they each read from their own htaccess…?

Don’t ask me lol. Best I can guess is that there is some bleed through. Currently yes each domain has their own htaccess. But as shown my previous rules bled through. Maybe Hostgator has a config file set up that does some magic. As said before my set up is…

public_html/.htaccess (root main domain)
public_html/addondomain/.htaccess

If you have rewrite rules in your addondomaon htaccess, then that may be overriding the rewrite rules in the root maindomain htaccess.

No I have no rewrite rules in there. Here is what Hostgator says about addon domains.

http://support.hostgator.com/articles/cpanel/please-read-before-creating-an-addon-domain

It sounds like an addon domain is treated the same as a subdomain, which means different vhost, different document root, and it also means they don’t share htaccess with the main domain. But the weird part is that you said your original rewrite rules were redirecting the addon domains to the main domain. But if the addon domains weren’t reading the main domain htaccess… were you just copy-pasting the same code to all the htaccess files?

I suppose the simple option is to keep on copy-pasting if that’s what you were doing before. The clever option would be to use <Directory> directives with regular expressions (but you need access to the main server config). And the dirty option would be to create the addon domain htaccess via symbolic link to the main domain htaccess.

Yeah for the most part I was just repeating the htaccess. Aside from the exclusion line for the addons. Sounds good. Just checking to see if you had any in sites as to the whys. Thanks a lot.

Hey Jeff. Looking around the web every other example of this code to strip the index adds another line above your example like so. Is there a reason you removed it? And do you think it’s necessary? And what does it do? Thanks!

 
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\\ /.*index\\.html\\ HTTP/
RewriteRule ^(.*)index\\.html$ /$1 [R=301,L]

I suspect most examples were copy-pasted from other websites, which were themselves copy-pasted from other websites, and so on. And the people who copy it onto their own website may not even themselves understand the reason for each line. :wink:

Based on things your other guy has said, I suspect that he believes that testing THE_REQUEST is necessary to avoid an infinite loop. Though, it turns out it isn’t actually necessary. The rewrite rule I posted has no infinite loop. I had tested it when I first posted, and I tested it again just now.

The rewrite rule will match only if index.html is at the end of the URL. After it performs the redirect, then index.html will no longer be at the end, and the rewrite rule won’t match the second time around. Thus, no infinite loop.

Thanks Jeff. Yeah I know thats why I’m actually trying to understand. I just pasted your one liner in google and the whole page showed that code i just posted. Aside from checking the request, is it also making sure its letter a-z and numbers 1-9 or something also?