Removing 'index.html' from url and adding 'www' with one single 301 redirect

前端 未结 3 1424
礼貌的吻别
礼貌的吻别 2021-01-06 16:13

In order to remove index.html or index.htm from urls I use the following in my .htaccess

RewriteCond %{REQUEST_URI} /i         


        
3条回答
  •  说谎
    说谎 (楼主)
    2021-01-06 16:41

    To avoid double redirection have another rule in .htaccess file that meets both conditions like this:

    Options +FollowSymlinks -MultiViews
    RewriteEngine on
    
    RewriteCond %{HTTP_HOST} !^www\. [NC]
    RewriteCond %{REQUEST_URI} ^(.*/)index\.html$ [NC]
    RewriteRule . http://www.%{HTTP_HOST}%1 [R=301,NE,L]
    
    RewriteCond %{HTTP_HOST} !^www\. [NC]
    RewriteRule . http://www.%{HTTP_HOST}%{REQUEST_URI} [NE,R=301,L]
    
    RewriteCond %{REQUEST_URI} ^(.*/)index\.html$ [NC]
    RewriteRule . %1 [R=301,NE,L]
    

    So if input URL is http://mydomain.com/path/index.html then both the conditions get satisfied in the first rule here and there will be 1 single redirect (301) to http://www.mydomain.com/path/.

    Also I believe QSA flag is not really needed above since you are NOT manipulating query string.

提交回复
热议问题