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

To remove index.htmlor index.htmfrom URLs, I use the following in.htaccess

RewriteCond %{REQUEST_URI} /index\.html?$ [NC]
RewriteRule ^(.*)index\.html?$ "/$1" [NC,R=301,NE,L]

It works! (More on the checkboxes at the end of this question *)

Then, to add wwwin the urls, I use the following in my.htaccess

RewriteCond %{HTTP_HOST} !^www\.mydomain\.com$ [NC]
RewriteRule ^(.*)$ "http://www.mydomain.com/$1" [R=301,NE,L]

It works too!

The question here is how to avoid the double redirection created by the rules above in cases like the following:

  • browsers request http://mydomain.com/path/index.html
  • the server sends a header301 to redirect the browser tohttp://mydomain.com/path/
  • then browser requests http://mydomain.com/path/
  • the server sends a header301 to redirect the browser tohttp://www.mydomain.com/path/

, , , , http://mydomain.com/path/index.html, , , . , Googlebot ( , , .)

!


* :

  • NC , .. index.html/ index.htm
  • NE url http://.../index.html?hello=ba%20be http://.../index.html?hello=ba%2520be
  • QSA , .. http://.../index.html?hello=babe http://.../?hello=babe ( anubhava)
+3
3

, .htaccess, :

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]

, URL- http://mydomain.com/path/index.html, , 1 (301) http://www.mydomain.com/path/.

, , QSA , .

+6

index.html www index.html ADD www URL- . , - http://domain.com/index.html http://www.domain.com/ FIRST. (www) , www, .

+1

L ? L ( ) , , URL .

Rules are applied sequentially from top to bottom, each time rewriting the URL if it matches the rules and pattern.

RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule ^ http://www.%{HTTP_HOST}%{REQUEST_URI} [R=301]

RewriteRule ^(.*/)index\.html?$ $1 [NC,QSA,R=301,NE,L]

Therefore, above, first add wwwand then delete index.html?before submitting a new URL; The only redirection for all rules.

-1
source

Source: https://habr.com/ru/post/1725257/


All Articles