SitePoint Sponsor

User Tag List

Page 1 of 2 12 LastLast
Results 1 to 25 of 48
  1. #1
    SitePoint Wizard bronze trophy PicnicTutorials's Avatar
    Join Date
    Dec 2007
    Location
    Carlsbad, California, United States
    Posts
    3,658
    Mentioned
    15 Post(s)
    Tagged
    0 Thread(s)

    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} ^[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|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!

  2. #2
    Certified Ethical Hacker silver trophybronze trophy dklynn's Avatar
    Join Date
    Feb 2002
    Location
    Auckland
    Posts
    14,680
    Mentioned
    20 Post(s)
    Tagged
    3 Thread(s)
    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 [code]...[/code] tags as that preserves the code when quoting for a reply.

    Code:
    # 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

    Code:
    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.

    Code:
    # 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?!?).

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

    DK
    David K. Lynn - Data Koncepts is a long-time WebHostingBuzz (US/UK)
    Client and (unpaid) WHB Ambassador
    mod_rewrite Tutorial Article (setup, config, test & write
    mod_rewrite regex w/sample code) and Code Generator

  3. #3
    SitePoint Wizard bronze trophy PicnicTutorials's Avatar
    Join Date
    Dec 2007
    Location
    Carlsbad, California, United States
    Posts
    3,658
    Mentioned
    15 Post(s)
    Tagged
    0 Thread(s)
    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?

  4. #4
    SitePoint Wizard bronze trophy Jeff Mott's Avatar
    Join Date
    Jul 2009
    Posts
    1,314
    Mentioned
    19 Post(s)
    Tagged
    1 Thread(s)
    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?
    "First make it work. Then make it better."

  5. #5
    SitePoint Wizard bronze trophy PicnicTutorials's Avatar
    Join Date
    Dec 2007
    Location
    Carlsbad, California, United States
    Posts
    3,658
    Mentioned
    15 Post(s)
    Tagged
    0 Thread(s)
    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!

  6. #6
    SitePoint Wizard bronze trophy Jeff Mott's Avatar
    Join Date
    Jul 2009
    Posts
    1,314
    Mentioned
    19 Post(s)
    Tagged
    1 Thread(s)
    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.
    "First make it work. Then make it better."

  7. #7
    SitePoint Wizard bronze trophy PicnicTutorials's Avatar
    Join Date
    Dec 2007
    Location
    Carlsbad, California, United States
    Posts
    3,658
    Mentioned
    15 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Jeff Mott View Post
    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.

  8. #8
    SitePoint Wizard bronze trophy Jeff Mott's Avatar
    Join Date
    Jul 2009
    Posts
    1,314
    Mentioned
    19 Post(s)
    Tagged
    1 Thread(s)
    Quote Originally Posted by EricWatson View Post
    "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.
    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.

    Quote Originally Posted by EricWatson View Post
    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). "
    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.
    "First make it work. Then make it better."

  9. #9
    SitePoint Wizard bronze trophy PicnicTutorials's Avatar
    Join Date
    Dec 2007
    Location
    Carlsbad, California, United States
    Posts
    3,658
    Mentioned
    15 Post(s)
    Tagged
    0 Thread(s)
    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...

    Code:
    # 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...

    Code:
    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!

  10. #10
    SitePoint Wizard bronze trophy Jeff Mott's Avatar
    Join Date
    Jul 2009
    Posts
    1,314
    Mentioned
    19 Post(s)
    Tagged
    1 Thread(s)
    Quote Originally Posted by EricWatson View Post
    How can I confirm port number?
    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.

    Quote Originally Posted by EricWatson View Post
    So just to confirm... I can replace this...

    Code:
    # 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...

    Code:
    RewriteRule ^(.*/)?index\.html?$ /$1 [R=301,L]
    RewriteCond %{HTTP_HOST} !^www\. [NC]
    RewriteRule (.*) http://www.%{HTTP_HOST}/$1 [R=301,L]
    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.
    "First make it work. Then make it better."

  11. #11
    SitePoint Wizard bronze trophy PicnicTutorials's Avatar
    Join Date
    Dec 2007
    Location
    Carlsbad, California, United States
    Posts
    3,658
    Mentioned
    15 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Jeff Mott View Post
    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, where

    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.

  12. #12
    SitePoint Wizard bronze trophy Jeff Mott's Avatar
    Join Date
    Jul 2009
    Posts
    1,314
    Mentioned
    19 Post(s)
    Tagged
    1 Thread(s)
    Quote Originally Posted by EricWatson View Post
    And based on your description, then I need NOT to add this code to my addon domains htaccess as well then? Correct? Thanks.
    Correct.

    Quote Originally Posted by EricWatson View Post
    Will this also add a slash at the end of the .com/ if none is present?
    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.
    "First make it work. Then make it better."

  13. #13
    SitePoint Wizard bronze trophy PicnicTutorials's Avatar
    Join Date
    Dec 2007
    Location
    Carlsbad, California, United States
    Posts
    3,658
    Mentioned
    15 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Jeff Mott View Post
    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.

  14. #14
    SitePoint Wizard bronze trophy Jeff Mott's Avatar
    Join Date
    Jul 2009
    Posts
    1,314
    Mentioned
    19 Post(s)
    Tagged
    1 Thread(s)
    Quote Originally Posted by EricWatson View Post
    See you can test with this link http://www.websitecodetutorials.com/. If you leave it off its rewritten in.
    Not in Chrome it isn't. Nor in Firefox. Nor is there a network request to redirect to a different URL.
    "First make it work. Then make it better."

  15. #15
    SitePoint Wizard bronze trophy PicnicTutorials's Avatar
    Join Date
    Dec 2007
    Location
    Carlsbad, California, United States
    Posts
    3,658
    Mentioned
    15 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Jeff Mott View Post
    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.

  16. #16
    SitePoint Wizard bronze trophy PicnicTutorials's Avatar
    Join Date
    Dec 2007
    Location
    Carlsbad, California, United States
    Posts
    3,658
    Mentioned
    15 Post(s)
    Tagged
    0 Thread(s)
    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.

  17. #17
    SitePoint Wizard bronze trophy Jeff Mott's Avatar
    Join Date
    Jul 2009
    Posts
    1,314
    Mentioned
    19 Post(s)
    Tagged
    1 Thread(s)
    Quote Originally Posted by EricWatson View Post
    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.
    "First make it work. Then make it better."

  18. #18
    SitePoint Wizard bronze trophy PicnicTutorials's Avatar
    Join Date
    Dec 2007
    Location
    Carlsbad, California, United States
    Posts
    3,658
    Mentioned
    15 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Jeff Mott View Post
    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-re...ps-and-tricks/

  19. #19
    SitePoint Wizard bronze trophy PicnicTutorials's Avatar
    Join Date
    Dec 2007
    Location
    Carlsbad, California, United States
    Posts
    3,658
    Mentioned
    15 Post(s)
    Tagged
    0 Thread(s)
    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-ad...onicalization/

  20. #20
    SitePoint Wizard bronze trophy Jeff Mott's Avatar
    Join Date
    Jul 2009
    Posts
    1,314
    Mentioned
    19 Post(s)
    Tagged
    1 Thread(s)
    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 "/".
    "First make it work. Then make it better."

  21. #21
    SitePoint Wizard bronze trophy PicnicTutorials's Avatar
    Join Date
    Dec 2007
    Location
    Carlsbad, California, United States
    Posts
    3,658
    Mentioned
    15 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Jeff Mott View Post
    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 "/".
    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.

  22. #22
    SitePoint Wizard bronze trophy Jeff Mott's Avatar
    Join Date
    Jul 2009
    Posts
    1,314
    Mentioned
    19 Post(s)
    Tagged
    1 Thread(s)
    Quote Originally Posted by EricWatson View Post
    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-ad...onicalization/
    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.
    "First make it work. Then make it better."

  23. #23
    SitePoint Wizard bronze trophy Jeff Mott's Avatar
    Join Date
    Jul 2009
    Posts
    1,314
    Mentioned
    19 Post(s)
    Tagged
    1 Thread(s)
    Quote Originally Posted by EricWatson View Post
    Do you know of any pages (or you) that can give supporting evidence that its the same?
    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.
    "First make it work. Then make it better."

  24. #24
    SitePoint Wizard bronze trophy PicnicTutorials's Avatar
    Join Date
    Dec 2007
    Location
    Carlsbad, California, United States
    Posts
    3,658
    Mentioned
    15 Post(s)
    Tagged
    0 Thread(s)
    Is it advisable to force a trailing slash via htaccess? http://enarion.net/web/htaccess/trailing-slash/

  25. #25
    SitePoint Wizard bronze trophy Jeff Mott's Avatar
    Join Date
    Jul 2009
    Posts
    1,314
    Mentioned
    19 Post(s)
    Tagged
    1 Thread(s)
    Quote Originally Posted by EricWatson View Post
    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.
    "First make it work. Then make it better."


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •