页面没有正确重定向 - .htaccess文件问题

时间:2016-12-13 21:27:36

标签: html html5 apache .htaccess redirect

我的.htaccess文件存在问题。 一切都工作正常,直到我做了一些更改(添加了多语言处理):

在:

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]

我在文件末尾添加的内容:

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]

我想将所有国家/地区重定向到我们页面网址中仅更改(..)的网页:http://my-site.pl/index.(..).html,如上面的代码所示。

我无法找到问题所在。

Firefox浏览器出错:

  

页面未正确重定向。 Firefox已经检测到了   服务器正在以一种方式重定向该地址的请求   永远不会完成。

编辑: 更改后(仍然无效)完全.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>

1 个答案:

答案 0 :(得分:0)

第三条规则中的问题是重定向到index.html,第二条规则是将index.html重定向到/,从而导致重定向循环。

这样做:

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]

确保在测试时清除浏览器缓存。