#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2011
    Posts
    16
    Rep Power
    0

    Htaccess redirects unnecessary?


    I'm not sure if this is the best section to post this in, but I'll give it a shot.

    I have the following code in my htaccess:
    Code:
    #if the domain is not www.mydomain.com
    RewriteCond %{HTTP_HOST} !^www\.mydomain\.com$ [NC]
    #redirect to www.mydomain.com
    RewriteRule ^(.*)$ http://www.mydomain.com/$1 [L,R=301]
    I believe the purpose of this is for redirecting the non-www domain to the www domain (I'll call this the 'non-www code').

    I was wondering if I even need this code because I have www.mydomain.com set as my hard canonical name, which I think is supposed to redirect aliases such as mydomain.com automatically (without having to code anything in htacess). So could I just do away with this code completely?

    I also have the following code:
    Code:
    #leave this rule in place, but after the one above to handle the home page
    RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /index\.html\ HTTP/ 
    RewriteRule ^(.*)index\.html$ http://www.mydomain.com/$1 [R=301,L]
    I believe this is in there to redirect http://www.mydomain.com/index.html to http://www.mydomain.com/ (I'll call this the 'index.html' code).

    I'm not sure if I could also also remove the 'index.html code' using the same logic that I gave above for getting ride of the 'non-www code'? Can I remove only the 'non-www code' and just leave the 'index.html code' by itself?
  2. #2
  3. Did you steal it?
    Devshed Supreme Being (6500+ posts)

    Join Date
    Mar 2007
    Location
    Washington, USA
    Posts
    14,007
    Rep Power
    9398
    You're in the right place.

    Originally Posted by fine0023
    I have the following code in my htaccess:
    Code:
    #if the domain is not www.mydomain.com
    RewriteCond %{HTTP_HOST} !^www\.mydomain\.com$ [NC]
    #redirect to www.mydomain.com
    RewriteRule ^(.*)$ http://www.mydomain.com/$1 [L,R=301]
    I believe the purpose of this is for redirecting the non-www domain to the www domain (I'll call this the 'non-www code').

    I was wondering if I even need this code because I have www.mydomain.com set as my hard canonical name, which I think is supposed to redirect aliases such as mydomain.com automatically (without having to code anything in htacess). So could I just do away with this code completely?
    If you're talking about DNS, no. DNS only makes sure that people can get to your server with the right name(s). What happens beyond that has to be set up independently.
    If you're talking about canonical links, still no. Your site is still accessible under whatever domain name gets to there and there will not be any redirections. For SEO you won't be penalized for duplicate content but you still shouldn't allow it.

    Originally Posted by fine0023
    I also have the following code:
    Code:
    #leave this rule in place, but after the one above to handle the home page
    RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /index\.html\ HTTP/ 
    RewriteRule ^(.*)index\.html$ http://www.mydomain.com/$1 [R=301,L]
    I believe this is in there to redirect http://www.mydomain.com/index.html to http://www.mydomain.com/ (I'll call this the 'index.html' code).

    I'm not sure if I could also also remove the 'index.html code' using the same logic that I gave above for getting ride of the 'non-www code'? Can I remove only the 'non-www code' and just leave the 'index.html code' by itself?
    Also no, and even moreso because the only thing (besides that) which says / is equivalent to /index.html is the DirectoryIndex directive to Apache, but that does not imply any kind of redirection at all.

    If you want to redirect all /*/index.html (not just /index.html) to its directory then
    Code:
    RewriteRule (.*)(?<=^|/)index.html$ /$1 [L,R=301]
    ("redirect /index.html and /.../index.html to the parent directory")
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Oct 2011
    Posts
    16
    Rep Power
    0
    Thanks for your response. I was surprised to see that it allowed me to have a page such as http://www.mydomain.com/whatever/index.html without redirecting it to http://www.mydomain.com/whatever/. I think I have that fixed now. The 'index.html' code now looks like this:
    Code:
    #leave this rule in place, but after the one above to handle the home page
    RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /index\.html\ HTTP/ 
    RewriteRule ^(.*)index\.html$ http://www.mydomain.com/$1 [R=301,L]
    RewriteRule (.*)(?<=^|/)index.html$ /$1 [L,R=301]
    Originally, I replaced this:
    Code:
    RewriteRule ^(.*)index\.html$ http://www.mydomain.com/$1 [R=301,L]
    with this:
    Code:
    RewriteRule (.*)(?<=^|/)index.html$ /$1 [L,R=301]
    but when that didn't seem to work, I realized that I was probably just supposed to add that extra line of code, which seems to have done the trick.

    From your responses, it sounds like you think I definitely should not go without any of this code. I don't totally understand your reasoning because a lot of these concepts are new to me. The reason I asked this question in the first place was because I had gotten this response on a forum for my website host (when I was asking about something unrelated):
    Simply having an alias just means your site will respond to requests for that host. It doesn't necessarily mean they'll be redirected to www.domain.com. That depends on your canonical name setting. You probably want (and may already have) a "hard" canonical name, which means that people who reach your site via any name other than www.domain.com will be automatically redirected to the same page on www.domain.com (without you having to code anything up in .htaccess). There are other ways of accomplishing that, but a hard canonical name is the easiest.
    Like I said, I'll go with your advice, but do you know why this person is saying I don't really need it?
    For SEO you won't be penalized for duplicate content but you still shouldn't allow it.
    Also, I just wanted to make sure I understood what you were saying here. So is the purpose of all this code not just to redirect, but also to not have duplicate content? And what does that mean exactly?
  6. #4
  7. Did you steal it?
    Devshed Supreme Being (6500+ posts)

    Join Date
    Mar 2007
    Location
    Washington, USA
    Posts
    14,007
    Rep Power
    9398
    That response is correct: simply having the name set up does not mean a user will be redirected to it. You also do not have to have it - the problem is that Google will see two sites with the same content and can hurt you. That's what I was getting at with the reference to SEO.

    The redirection is not the number one reason you're doing this. You're doing it because you don't want two (or more) URLs to go to the same place. The fact that search engines rank according to that same idea isn't a coincidence: it's how people think and so a system modelling that should get the most relevants for a search. Kinda.
    I'm not an SEO buff but I know enough for being a web developer. Besides, this isn't quite the right venue (ie, forum) for this kind of subject. If you want to know more for real then search around - it's a really, really popular topic nowadays so you'll find a lot.

IMN logo majestic logo threadwatch logo seochat tools logo