简体   繁体   中英

Redirect all http to https, all www to non-www, and remove .html from all urls

  • .html should be removed from the end of every URL
  • All http links should go to https
  • All www links should go to non-www

How do I achieve this with .htaccess, and will this be sufficient for Google to crawl my site correctly and not produce duplicate links?

RewriteEngine on

# remove .html from all files
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.html -f
RewriteRule ^(.*)$ $1.html

# redirect all http to https and all www to non-www
RewriteCond %{HTTPS} off [OR]
RewriteCond %{HTTP_HOST} ^www\. [NC]
RewriteCond %{HTTP_HOST} ^(?:www\.)?(.+)$ [NC]
RewriteRule ^ https://%1%{REQUEST_URI} [L,NE,R=301]

This seems to have done the job. Several answers I found online failed - http://www.example.com was often incorrectly directed to https://www.example.com .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM