我有两个独立的网站,我想在这两个网站上修改 .htaccess 文件,这样如果有人访问 mydomain.com/index.htm 文件,他们就会被重定向到 mydomain.com。
我在网上发现,这个代码可以通过这一行来完成:
RewriteRule ^index.htm$ / [NC,R,L]
(位于)https://moz.com/community/q/topic/35464/redirecting-index-html-to-the-root
但是,在查看我的两个不同站点的 .htaccess 文件时,它们的初始 rewritecond 和 rewriterule 行略有不同(我在下面用 --- 符号标记):
代码集 1
RewriteEngine On
RewriteCond %{HTTP_HOST} ^EXAMPLE.com$ ---
RewriteRule ^$ http://EXAMPLE.com/Index.htm [L,R=301] ---
RewriteRule ^index.htm$ / [NC,R,L]
Redirect 301 /index.php http://www.EXAMPLE.com/Index.htm
Redirect 301 /index.htm http://www.EXAMPLE.com/Index.htm
Redirect 301 /index.html http://www.EXAMPLE.com/Index.htm
代码集 2
RewriteEngine on
RewriteCond %{HTTP_HOST} !^www\.EXAMPLE\.com ---
RewriteRule (.*) http://www.EXAMPLE.com/$1 [R=301,L] ---
RewriteRule ^index.htm$ / [NC,R,L]
Redirect 301 /index.php http://www.EXAMPLE.com/Index.htm
Redirect 301 /index.htm http://www.EXAMPLE.com/Index.htm
Redirect 301 /index.html http://www.EXAMPLE.com/Index.htm
在功能方面,当我测试 index.htm 等各种 URL 时,这些不同的行似乎在做同样的事情——但文字代码当然是不同的。我已经尝试阅读 RewriteCond 和 RewriteRule 行在概念上正在做什么,但我无法理解在线的股票解释。
有人愿意告诉我每条标记为“---”的行都在做什么,就像我是一个 5 岁的孩子一样,哪种方式可能是最好的方式,代码集 1 或代码集2?在每个代码块中,标记的 RewriteCond 行是完全影响标记的 RewriteRule 行还是它们不相互影响?它们是否会影响RewriteRule ^index.htm$ / [NC,R,L]行的行为,反之亦然?
我也知道 R=301 参数是 301 重定向,但是如果没有指定 R 会发生什么,比如在RewriteRule ^index.htm$ / [NC,R,L]行中?