Remove .html from URLs with a redirect

前端 未结 8 1572
一向
一向 2020-12-29 14:32

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, unfort

相关标签:
8条回答
  • 2020-12-29 14:37

    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).

    0 讨论(0)
  • 2020-12-29 14:43

    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

    0 讨论(0)
  • 2020-12-29 14:44

    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!

    0 讨论(0)
  • 2020-12-29 14:52

    Follow the steps, and you'll be able to remove .html from url without modifying .htaccess file.

    0 讨论(0)
  • 2020-12-29 14:56

    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]
    
    0 讨论(0)
  • 2020-12-29 14:57

    This should do the trick:

    RewriteEngine On
    RewriteRule ^(\w+)\.html$ /$1 [R=301,L]
    
    0 讨论(0)
提交回复
热议问题