1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

How do i get rid of file extensions using .htaccess?

Discussion in 'Black Hat SEO' started by rugbyjack2005, Aug 13, 2012.

  1. rugbyjack2005

    rugbyjack2005 Power Member

    Joined:
    Oct 2, 2011
    Messages:
    539
    Likes Received:
    50
    Location:
    United Kingdom
    On a particular site, i have some back links going to domain.com/page.html and some to domain.com/page. To amalgamate them together, i want to redirect all .html pages to their non .html pages. How do i do this using the .htaccess file?

    I have tried the following codes with no luck:
    RewriteRule ^(.+)\.html$ $1 [L,NC,R=301] and i have also tried RewriteRule ^([^/]+)\.html$ $1 [QSA,L]

    Help would be very much appreciated (will give thanks and rep)
     
  2. SEOWhizz

    SEOWhizz Power Member

    Joined:
    Oct 22, 2011
    Messages:
    606
    Likes Received:
    432
    Location:
    Lat: 38N 43' 11.298" Long: 27W 12' 7.733"
    1. Try:
    Code:
    RewriteCond %{REQUEST_FILENAME}\.html -f
    RewriteRule ^(.+)$ $1\.html [QSA]
    
    2. If 1) doesn't work, try:
    Code:
    RewriteRule ^index\.html$ / [L,R=301]
    RewriteRule (.+)\.html$ /$1 [L,R=301]
    
    3. Finally, if the above two don't work, try:
    Code:
    RewriteEngine on 
    RewriteCond %{REQUEST_FILENAME} !-d 
    RewriteCond %{REQUEST_FILENAME}\.html -f 
    RewriteRule ^(.*)$ $1.html
    

    * The above is assuming your server is Appache based and supports the rules.
     
    • Thanks Thanks x 1
    Last edited: Aug 13, 2012
  3. 1+1=2

    1+1=2 Junior Member

    Joined:
    Jul 14, 2009
    Messages:
    198
    Likes Received:
    29
    Here is what i added to .htaccess to hide php extension:
    Code:
    RewriteEngine On
    RewriteCond %{SCRIPT_FILENAME} !-d
    RewriteRule ^([^\.]+)$ $1.php [NC,L]
    it work perfectly for me :) you can replace .php by .html to get what you need.
     
    • Thanks Thanks x 1
  4. rugbyjack2005

    rugbyjack2005 Power Member

    Joined:
    Oct 2, 2011
    Messages:
    539
    Likes Received:
    50
    Location:
    United Kingdom
    Many thanks for your help. 1 and 3 didn't have any effect. The second one throws a 310 error saying that there are too many redirects. Any other ideas?

    Its a static site and the only code in the .htaccess file already is the non www. to www. as below:
    RewriteEngine On
    RewriteCond %{HTTP_HOST} ^site.co.uk
    RewriteRule (.*) http://www.site.co.uk/$1 [R=301,L]
     
  5. rugbyjack2005

    rugbyjack2005 Power Member

    Joined:
    Oct 2, 2011
    Messages:
    539
    Likes Received:
    50
    Location:
    United Kingdom
    Thanks for your help. This unfortunately doesn't have any effect. A wonder if the redirect code for non www. to www. is interfering?
     
  6. utuxia

    utuxia BANNED BANNED

    Joined:
    Feb 14, 2011
    Messages:
    673
    Likes Received:
    111
    you can't have both. you can't redirect foo.html to /foo and then expect /foo to redirect to foo.html

    that's the 310 never ending redirect you're seeing. Best thing to do is set all /foo to goto foo.html and then change all your links on your site to not point to .html directly.
     
  7. rugbyjack2005

    rugbyjack2005 Power Member

    Joined:
    Oct 2, 2011
    Messages:
    539
    Likes Received:
    50
    Location:
    United Kingdom
    I don't understand what you mean here
     
  8. utuxia

    utuxia BANNED BANNED

    Joined:
    Feb 14, 2011
    Messages:
    673
    Likes Received:
    111
    the extension-less domains are a painted door. They are actually pointing to the file with extension. ... so you can't have it both ways, you can't make files with extensions point back to the painted door....at the same time. It's a never ending loop. In other words you're taking two mirrors and facing them at each other and expecting something different.
     
    Last edited: Aug 14, 2012
  9. SnowWar

    SnowWar Power Member

    Joined:
    Mar 3, 2012
    Messages:
    595
    Likes Received:
    48
    Occupation:
    Pure student :p
    This is a complicated problem and can not be solved that easily. You should contact with some professionals to get rid of this. You might need to take some steps that is a bit complicated.
     
  10. rugbyjack2005

    rugbyjack2005 Power Member

    Joined:
    Oct 2, 2011
    Messages:
    539
    Likes Received:
    50
    Location:
    United Kingdom
    Ah i see. So is there anyway of consolidating the links to the two pages?
     
  11. utuxia

    utuxia BANNED BANNED

    Joined:
    Feb 14, 2011
    Messages:
    673
    Likes Received:
    111
    this might work, rename all your .html files to .htm. Then redirect /(.*) to /$1.htm and then you can redirect /(.*).html to /$1 (which will inturn redirect to .htm).. but be sure to change all your links on your site so they no longer point to .html.
     
  12. SEOWhizz

    SEOWhizz Power Member

    Joined:
    Oct 22, 2011
    Messages:
    606
    Likes Received:
    432
    Location:
    Lat: 38N 43' 11.298" Long: 27W 12' 7.733"
    There's some code examples here (esp. 'jdMorgan' posts):

    Code:
    [URL]http://www.webmasterworld.com/apache/3371997.htm[/URL]
    [URL]http://www.webmasterworld.com/apache/3955858.htm[/URL]
     
    Last edited: Aug 14, 2012