简体   繁体   English

.htaccess重定向问题-www重定向按预期工作,但index.html不重定向到根目录

[英].htaccess redirect issue - www redirect working as expected, but index.html not redirecting to root

I see there have been many questions in this vein all over the web, but none I can find to resolve my specific .htaccess woes at the moment! 我发现整个网络上都存在许多问题,但是目前没有任何一个可以解决我的.htaccess难题! In my .htaccess file I have rules both to redirect a) non-www to www and b) index.html to the root. 在我的.htaccess文件中,我有规则都将a)非www重定向到www和b)index.html重定向到根。 The non-www to www condition is being met and redirecting successfully. 满足非www到www的条件并成功重定向。 However, the index.html file is not redirecting to the root. 但是,index.html文件没有重定向到根目录。

I have also tested with http://htaccess.madewithlove.be . 我还用http://htaccess.madewithlove.be进行了测试。 The debugging logs there indicate "This variable is not supported: %{THE_REQUEST}" and subsequently, the rewrite condition for the index.html redirect is not met, but I can't surmise why or whether that variable is the real culprit here. 那里的调试日志指示“不支持此变量:%{THE_REQUEST}”,随后,不满足index.html重定向的重写条件,但是我无法推测为什么或该变量是真正的罪魁祸首。 I will also mention that this particular domain is a sub-domain/add-on domain to another domain. 我还将提到该特定域是另一个域的子域/附加域。 The .htaccess file depicted below is located in the root of the subdomain. 下面描述的.htaccess文件位于子域的根目录中。 The .htaccess file at the primary domain level is blank at present. 目前,主域级别的.htaccess文件为空。

Here is my existing .htaccess content: 这是我现有的.htaccess内容:

Options +FollowSymLinks
RewriteEngine On
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /.*index\.html?\ HTTP/
RewriteRule ^(.*)index\.html?$ http://www.example.com/$1 [R=301,L]
RewriteCond %{HTTP_HOST} ^example\.com$ [NC]
RewriteRule ^(.*)$ http://www.example.com/$1 [R=301,L]

Appreciate your help! 感谢您的帮助!

Keep your rules like this in root .htaccess: 将您的规则保留在root .htaccess中:

DirectoryIndex index.html
Options +FollowSymLinks
RewriteEngine On

RewriteCond %{HTTP_HOST} ^example\.com$ [NC]
RewriteRule ^ http://www.%{HTTP_HOST}%{REQUEST_URI} [R=302,L,NE]

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

Test this in a new browser. 在新的浏览器中对此进行测试。

Drop the first RewriteCond. 删除第一个RewriteCond。 It's not needed for your needs. 您不需要它。 The first RewriteRule alone will solve your issue. 仅第一个RewriteRule即可解决您的问题。

Maybe you also want to update your first RewriteRule . 也许您还想更新您的第一个RewriteRule In the current form it will redirect (regardless of the domain used) to http://www.example.com/ . 在当前形式下,它将重定向(无论使用哪个域)到http://www.example.com/ If you want to "keep" the used subdomain make the redirect relative: 如果要“保留”使用的子域,请使重定向相对:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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