Remove .html from URLs with a redirect

2019-02-03 17:40发布

We have a website, unfortunately all the URLs have the .html suffix, its a Magento installation, Magento allows you to change this on the CMS, but again, unfortunately all this URLs with .html suffix have a good ranking in Google. We need to redirect to non .html.

So, consider the following scenario, we are rebuilding this site from scratch, so we have the same urls on the new site but without the .html suffix.

  • Now is: www.example.de/cool-shoes.html
  • Will be: www.example.de/cool-shoes

So www.example.de/cool-shoes.html will not exist anymore, and I've been trying a redirect with the .htaccess with no luck.

I've tried so far:

RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /.*\.html\ HTTP/
RewriteRule (.*)index\.html$ /$1 [R=301,L] 

and:

RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.html -f
RewriteRule ^(.*)$ $1.html

but it doesn't seem to work...any ideas?

8条回答
孤傲高冷的网名
2楼-- · 2019-02-03 18:08

This is for URLs ending with .html /product/raspberrypi.html ---> /product/raspberrypi/ (/product/raspberrypi/index.php) the index.php is hidden. Took me awhile to figure this out. LOL...

RewriteEngine On
RewriteBase /

RewriteCond %{REQUEST_URI} \.html$
RewriteRule ^(.*)\.html$ $1 [R=301,L]

RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]

You have to use 'REQUEST_URI' and add it before index redirect rules since it could be overridden by the application. Its important to know that its URI not a filename or directory we are trying to redirect, since the file names all have index.php in the root folders(Wordpress).

查看更多
Luminary・发光体
3楼-- · 2019-02-03 18:09

Try this to putting in your .htaccess file Redirect permanent www.mysite.de/cool-shoes.html www.mysite.de/cool-shoes this may be helpful to you

查看更多
萌系小妹纸
4楼-- · 2019-02-03 18:13

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

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]
查看更多
Fickle 薄情
5楼-- · 2019-02-03 18:20

This will rewrite the url like so http://example.com/page.html -> http://example.com/page

# Remove .html from url
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.html -f
RewriteRule ^(.*)$ $1.html
查看更多
放荡不羁爱自由
6楼-- · 2019-02-03 18:21

This should do the trick:

RewriteEngine On
RewriteRule ^(\w+)\.html$ /$1 [R=301,L]
查看更多
叛逆
7楼-- · 2019-02-03 18:23

Ok so, after some research, and failing to achieve this with a rewrite rule, the following line of code worked:

redirectMatch 301 ^(.*)\.html $1

This is quite usefull to remove any url extension and avoid broken links, hopefully helps someone in the future...

cheers!

查看更多
登录 后发表回答