4

I have recently changed my website url using htaccess so that my urls will not show file extensions. Now my problem is as I have created a new xml sitemap so that my url will be extensionless!!! the Google webmaster tool is telling me about duplicate content issue!! ie. page and page.html have same title.... so my question is how do i redirect the urls with file extension html to urls with out extension!!! this is an example of my website url with html extension

http://www.shenazhpeyk.co.uk/coding-machines.html

I want to redirect and change it to

http://www.shenazhpeyk.co.uk/coding-machines

so that will fix the issue with Google webmaster tools (Please provide me a code for use in htaccess file)

Many Thanks

Amir Oof
  • 49
  • 1
  • 2
  • See also http://stackoverflow.com/questions/9250356/how-to-redirect-file-php-to-file-but-make-file-point-to-file-php/9250387 – Gerben Feb 13 '12 at 15:01

3 Answers3

2

Found this code as well. Not sure if it will accomplish the same thing. Seems to work for me as does the one above (for PHP).

RewriteEngine On

# Unless directory, remove trailing slash
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)/$ /$1 [R=301,L]

# Redirect external .php requests to extensionless url
RewriteCond %{THE_REQUEST} ^(.+)\.php([#?][^\ ]*)?\ HTTP/
RewriteRule ^(.+)\.php$ /$1 [R=301,L]

# Resolve .php file for extensionless php urls
RewriteRule ^([^/.]+)$ $1.php [L]

I am wondering about the trailing slashes and whether those should be there or omitted?

Jordan
  • 1,650
  • 2
  • 18
  • 17
  • I know that I am a late to the party but can you please how you redirected it back to the page without the file extension? I really love your code and am using it right now. – Alex Lowe Oct 04 '16 at 02:27
0

Try this:

Options +FollowSymLinks -MultiViews
DirectorySlash Off

RewriteEngine On

RewriteCond %{SCRIPT_FILENAME}/ -d
RewriteCond %{SCRIPT_FILENAME}.html !-f
RewriteRule [^/]$ %{REQUEST_URI}/ [R=301,L]

RewriteCond %{ENV:REDIRECT_STATUS} ^$
RewriteRule ^(.+)\.html$ /$1 [R=301,L]

RewriteCond %{SCRIPT_FILENAME}.html -f
RewriteRule [^/]$ %{REQUEST_URI}.html [QSA,L]
Baz
  • 36,440
  • 11
  • 68
  • 94
Suhas
  • 61
  • 5
0

Try adding the following to the .htaccess file in the root directory of your site redirect URLs with .html extension and remove it.

RewriteEngine on
RewriteBase / 

#redirect to remove the .html extension
RewriteRule ^(.+)\.html$ $1 [L,NC,R=301] 
Ulrich Palha
  • 9,411
  • 3
  • 25
  • 31
  • thanks for reply But i am getting following error: The page isn't redirecting properly this is what i have from before and it works but it won't redirect html pages to without extension ones RewriteEngine On RewriteCond %{REQUEST_FILENAME} !-f RewriteRule ^([^\.]+)$ $1.html [NC,L] – Amir Oof Feb 13 '12 at 17:10
  • 2
    I found the solution, in case anyone else have same problem! lol! Options +FollowSymLinks -MultiViews DirectorySlash Off RewriteEngine On RewriteCond %{SCRIPT_FILENAME}/ -d RewriteCond %{SCRIPT_FILENAME}.html !-f RewriteRule [^/]$ %{REQUEST_URI}/ [R=301,L] RewriteCond %{ENV:REDIRECT_STATUS} ^$ RewriteRule ^(.+)\.html$ /$1 [R=301,L] RewriteCond %{SCRIPT_FILENAME}.html -f RewriteRule [^/]$ %{REQUEST_URI}.html [QSA,L] Note : Not sure if it is correct but it works! – Amir Oof Feb 13 '12 at 17:34
  • Amir: Congrats! If this solution works, drop it in as an official answer vs. a comment. That way folks will see it more readily! :) – Joe D'Andrea Jul 09 '12 at 15:38
  • What I'm curious about is how to make this sort of thing work, but still use Content Negotiation (+MultiViews) for things like images, etc. – Joe D'Andrea Jul 09 '12 at 15:41