SitePoint Sponsor

User Tag List

Results 1 to 12 of 12

Thread: .htaccess - Rewrite IP to URL

  1. #1
    SitePoint Zealot mjkovis's Avatar
    Join Date
    May 2009
    Location
    St. Louis, MO
    Posts
    106
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    .htaccess - Rewrite IP to URL

    I just recently found an issue where Google has indexed 4-5 pages of a domain by the servers IP address.

    How do I enable mod_rewrite to be able to solve this issue from happening in the future?

    Current .htaccess
    Code:
    RewriteEngine on
    RewriteCond %{THE_REQUEST} /index.php
    RewriteRule ^([a-z]+/)?index\.php$ http://www.example.com/$1 [R=301,L]
    RewriteRule ^(example-directory-1/)?index\.php$ http://www.example.com/$1 [R=301,L]
    RewriteRule ^(example-directory-2/)?index\.php$ http://www.example.com/$1 [R=301,L]
    
    RewriteCond %{HTTP_HOST} ^example.com [NC]
    RewriteRule .? http://www.example.com%{REQUEST_URI} [R=301,L]

  2. #2
    Non-Member
    Join Date
    Apr 2004
    Location
    Miami, FL, USA
    Posts
    449
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Easy fix... change this one line from:
    Code:
    RewriteCond %{HTTP_HOST} ^example.com [NC]
    to:
    Code:
    RewriteCond %{HTTP_HOST} !^www\.example\.com [NC]

  3. #3
    SitePoint Zealot mjkovis's Avatar
    Join Date
    May 2009
    Location
    St. Louis, MO
    Posts
    106
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Out of curiosity, what does that do?

    That specific line is to redirect non-www to www. How does that fix redirecting the IP address (if a crawler gets there for some reason) to the www version of example domain?

  4. #4
    Non-Member
    Join Date
    Apr 2004
    Location
    Miami, FL, USA
    Posts
    449
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    The way you had it, it was only redirecting no-subdomain (example.com) to www.example.com.

    What I changed it to redirects anything except www.example.com to www.example.com (which includes straight ip-address hits).
    Last edited by ScallioXTX; May 2, 2011 at 11:19. Reason: delinkified example URLs

  5. #5
    SitePoint Zealot mjkovis's Avatar
    Join Date
    May 2009
    Location
    St. Louis, MO
    Posts
    106
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by transio View Post
    The way you had it, it was only redirecting no-subdomain (example.com) to www.example.com.

    What I changed it to redirects anything except www.example.com to www.example.com (which includes straight ip-address hits).
    Sorry for the delayed response!

    Thank you for clarifying this issue. Now would this also redirect parked domains that are creating duplicate content? For example, I have example.com as my main domain as well as example.net and example.org.

    Reason I ask, is because I parked those domains, they redirected the homepage, but subdirectories would go to the non .com domain without redirecting or being rewritten.

  6. #6
    Non-Member
    Join Date
    Apr 2004
    Location
    Miami, FL, USA
    Posts
    449
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Yeah. It'll redirect everything as it should to avoid content dilution in googles eyes. Add a [L,R=301] to the end of the rule to issue a 301 permanent redirect.

  7. #7
    Non-Member
    Join Date
    Apr 2004
    Location
    Miami, FL, USA
    Posts
    449
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Sorry, you had that correct in your original... was posting from my phone again lol.

  8. #8
    SitePoint Zealot mjkovis's Avatar
    Join Date
    May 2009
    Location
    St. Louis, MO
    Posts
    106
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by transio View Post
    Yeah. It'll redirect everything as it should to avoid content dilution in googles eyes. Add a [L,R=301] to the end of the rule to issue a 301 permanent redirect.
    Ok, so now this is my exact code for redirection...

    Code:
    RewriteCond %{HTTP_HOST} !^www\.equotemd\.com [NC]
    RewriteRule .? http://www.equotemd.com%{REQUEST_URI} [R=301,L]
    I tested it last night, it works for the IP address redirection. My next test will be checking out the parked domains. I will have to change the name servers to point to my server, park them and then await propagation.

    Help was appreciated!

  9. #9
    SitePoint Zealot mjkovis's Avatar
    Join Date
    May 2009
    Location
    St. Louis, MO
    Posts
    106
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    I just accidentally posted a response in the wrong thread regarding this issue...

    With these new changes to my .htaccess file, I cannot keep a subdomain from redirecting back to my root domain. How do I fix this issue?

  10. #10
    Do. Or do not. There is no try silver trophy
    SitePoint Award Recipient ScallioXTX's Avatar
    Join Date
    Aug 2008
    Location
    The Netherlands
    Posts
    8,346
    Mentioned
    87 Post(s)
    Tagged
    2 Thread(s)
    Just add a RewriteCond

    Code:
    RewriteCond %{HTTP_HOST} !^somesubdomain\.equotemd\.com [NC]
    RewriteCond %{HTTP_HOST} !^www\.equotemd\.com [NC]
    RewriteRule .? http://www.equotemd.com%{REQUEST_URI} [R=301,L]
    You need one RewriteCond for every subdomain you want to exclude, or put them together in one rule like so

    Code:
    RewriteCond %{HTTP_HOST} !^(www|somesubdomain)\.equotemd\.com [NC]
    RewriteRule .? http://www.equotemd.com%{REQUEST_URI} [R=301,L]
    Rémon - Hosting Advisor

    Minimal Bookmarks Tree
    My Google Chrome extension: browsing bookmarks made easy

  11. #11
    SitePoint Zealot mjkovis's Avatar
    Join Date
    May 2009
    Location
    St. Louis, MO
    Posts
    106
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Scallio... You are now officially my .htaccess hero of the day!

    Sad part is, I think I actually knew how to make that edit... Lol.

  12. #12
    Do. Or do not. There is no try silver trophy
    SitePoint Award Recipient ScallioXTX's Avatar
    Join Date
    Aug 2008
    Location
    The Netherlands
    Posts
    8,346
    Mentioned
    87 Post(s)
    Tagged
    2 Thread(s)
    Rémon - Hosting Advisor

    Minimal Bookmarks Tree
    My Google Chrome extension: browsing bookmarks made easy

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
  •