简体   繁体   中英

.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! In my .htaccess file I have rules both to redirect a) non-www to www and b) index.html to the root. The non-www to www condition is being met and redirecting successfully. However, the index.html file is not redirecting to the root.

I have also tested with 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. 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. The .htaccess file at the primary domain level is blank at present.

Here is my existing .htaccess content:

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:

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. It's not needed for your needs. The first RewriteRule alone will solve your issue.

Maybe you also want to update your first RewriteRule . In the current form it will redirect (regardless of the domain used) to http://www.example.com/ . If you want to "keep" the used subdomain make the redirect relative:

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

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