how to fix htaccess error with multiple rewrite rule - ERR_TOO_MANY_REDIRECTS

走远了吗. 提交于 2019-12-13 04:28:11

问题


why this htaccess has error. it says "to many redirect" please help thanks

RewriteCond %{REQUEST_FILENAME} !-f

RewriteCond %{REQUEST_FILENAME} !-d

RewriteCond %{REQUEST_FILENAME} !-l

RewriteRule .* index.php [L]


RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC]
RewriteRule ^(.*)$ http://%1/$1 [R=301,L]

RewriteEngine on
Options +FollowSymLinks
RewriteCond %{THE_REQUEST} ^.*/index\.php
RewriteRule ^(.*)index.php$ http://us.domain.com/ae_en/$1 [R=301,L]


RewriteEngine on
RewriteBase /
RewriteCond %{HTTP_HOST} ^us\.domain\.com$
RewriteCond %{REQUEST_URI} !^/(ae_en|ae_ar) [NC]
RewriteRule ^(.*)/ae_en/%{REQUEST_URI} [R=301,L]

I cannot try the wrong code, it always appear error


回答1:


There are several issues with your .htaccess. I would suggest both re-ordering and editing the rules.

This would be a better approach, in my opinion. I'll explain the parts below.

RewriteEngine on
RewriteBase /
Options +FollowSymLinks

# External redirect: Remove www from domain
RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC]
RewriteRule ^(.*)$ http://%1/$1 [R=301,L]

# External redirect: Ensure language code is set
RewriteCond %{HTTP_HOST} ^us\.domain\.com$
RewriteCond %{REQUEST_URI} !^/(ae_en|ae_ar) [NC]
RewriteCond %{REQUEST_URI} !^/index.php$ [NC]
RewriteRule ^(.*)$ /ae_en/$1 [R=301,L]

# Internal redirect: Pass everything non-existent to index.php
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-l
RewriteRule .* index.php [L]

Structure

First, general options and settings should come at the top and only once, i.e.

RewriteEngine on
RewriteBase /
Options +FollowSymLinks

Second, I like to get the external URL to show up correctly, so I group all external redirects here - denoted with comments in the above code block.

Third, internal "redirects" follow, meaning mapping the (now correct and final) external URL to the correct scripts.

This structure ensures that the internal scripts are only matched on a URL that will not change when re-run against the .htaccess on the next RewriteRule match.

Omitting external redirect for index.php

I guess you added the index.php redirection handling after adding in the internal redirect, which probably has caused index.php to show up in the browser. So, with the above structure in place, I assume it will not show up again.

Error in last RewriteRule

As Mike Rockett pointed out in the comment above, there also was an error in the last RewriteRule.

Avoid redirect loop

To avoid a redirect loop when rewriting to index.php with the last rule, you have to explicitly exclude index.php from language code redirection, i.e.

RewriteCond %{REQUEST_URI} !^/index.php [NC]


来源:https://stackoverflow.com/questions/36266626/how-to-fix-htaccess-error-with-multiple-rewrite-rule-err-too-many-redirects

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!