简体   繁体   中英

The page isn't redirecting properly - .htaccess file issue

I have a problem with my .htaccess file. Everything was working fine till I made some changes (added multi language handling):

Before:

RewriteEngine On

RewriteBase /
RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC]
RewriteRule ^(.*)$ http://%1/$1 [R=301,L]
RewriteRule ^(index.html)$ http://my-site.pl/ [R=301,L]

What I have added at the end of the file:

RewriteCond %{HTTP:Accept-Language} (pl) [NC]
RewriteRule .* index.html [R,L]
RewriteCond %{HTTP:Accept-Language} (en) [NC]
RewriteRule .* index.en.html [R,L]
RewriteCond %{HTTP:Accept-Language} (jp) [NC]
RewriteRule .* index.jp.html [R,L]

I want to redirect all countries to theirs pages changing only (..) in my page url: http://my-site.pl/index.(..).html as in the above code.

I can't find where is the problem.

Error from Firefox browser:

The page isn't redirecting properly. Firefox has detected that the server is redirecting the request for this address in a way that will never complete.

EDIT: After changes (still not work) full .htaccess:

RewriteEngine On
RewriteBase /

RewriteCond %{HTTP_HOST} ^www\.(.+)$ [NC]
RewriteRule ^ http://%1%{REQUEST_URI} [R=301,L,NE]

RewriteCond %{THE_REQUEST} /index\.html [NC]
RewriteRule ^index\.html$ / [R=301,L,NC]

RewriteCond %{HTTP:Accept-Language} pl [NC]
RewriteRule ^(?!index\.html$).+$ / [NC,R,L]

RewriteCond %{HTTP:Accept-Language} en [NC]
RewriteRule ^/?$ index.en.html [R,L]

RewriteCond %{HTTP:Accept-Language} jp [NC]
RewriteRule ^/?$ index.jp.html [R,L]

ErrorDocument 404 /404.html

## EXPIRES CACHING ##
<IfModule mod_expires.c>
ExpiresActive On
ExpiresByType image/jpg "access 1 year"
ExpiresByType image/jpeg "access 1 year"
ExpiresByType image/gif "access 1 year"
ExpiresByType image/png "access 1 year"
ExpiresByType text/css "access 1 month"
ExpiresByType text/html "access 1 month"
ExpiresByType application/pdf "access 1 month"
ExpiresByType text/x-javascript "access 1 month"
ExpiresByType application/x-shockwave-flash "access 1 month"
ExpiresByType image/x-icon "access 1 year"
ExpiresDefault "access 1 month"
</IfModule>
## EXPIRES CACHING ##
<IfModule mod_deflate.c>
  # Compress HTML, CSS, JavaScript, Text, XML and fonts
  AddOutputFilterByType DEFLATE application/javascript
  AddOutputFilterByType DEFLATE application/rss+xml
  AddOutputFilterByType DEFLATE application/vnd.ms-fontobject
  AddOutputFilterByType DEFLATE application/x-font
  AddOutputFilterByType DEFLATE application/x-font-opentype
  AddOutputFilterByType DEFLATE application/x-font-otf
  AddOutputFilterByType DEFLATE application/x-font-truetype
  AddOutputFilterByType DEFLATE application/x-font-ttf
  AddOutputFilterByType DEFLATE application/x-javascript
  AddOutputFilterByType DEFLATE application/xhtml+xml
  AddOutputFilterByType DEFLATE application/xml
  AddOutputFilterByType DEFLATE font/opentype
  AddOutputFilterByType DEFLATE font/otf
  AddOutputFilterByType DEFLATE font/ttf
  AddOutputFilterByType DEFLATE image/svg+xml
  AddOutputFilterByType DEFLATE image/x-icon
  AddOutputFilterByType DEFLATE text/css
  AddOutputFilterByType DEFLATE text/html
  AddOutputFilterByType DEFLATE text/javascript
  AddOutputFilterByType DEFLATE text/plain
  AddOutputFilterByType DEFLATE text/xml

  # Remove browser bugs (only needed for really old browsers)
  BrowserMatch ^Mozilla/4 gzip-only-text/html
  BrowserMatch ^Mozilla/4\.0[678] no-gzip
  BrowserMatch \bMSIE !no-gzip !gzip-only-text/html
  Header append Vary User-Agent
</IfModule>

Problem is in 3rd rule that is redirecting to index.html and 2nd rule is redirecting index.html to / thus causing a redirect loop.

Have it this way:

RewriteEngine On
RewriteBase /

RewriteCond %{HTTP_HOST} ^www\.(.+)$ [NC]
RewriteRule ^ http://%1%{REQUEST_URI} [R=301,L,NE]

RewriteCond %{THE_REQUEST} /index\.html [NC]
RewriteRule ^index\.html$ / [R=301,L,NC]

RewriteCond %{HTTP:Accept-Language} pl [NC]
RewriteRule ^(?!index\.html$).+$ / [NC,R,L]

RewriteCond %{HTTP:Accept-Language} en [NC]
RewriteRule ^/?$ index.en.html [R,L]

RewriteCond %{HTTP:Accept-Language} jp [NC]
RewriteRule ^/?$ index.jp.html [R,L]

Make sure to clear your browser cache while testing.

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