为什么htaccess中的重写规则会导致重定向错误?

时间:2016-01-01 17:39:03

标签: apache .htaccess mod-rewrite

我刚刚更改了我网站上的主机,出于某种原因,当我尝试查看它时,我得到了太多的重定向"错误。我已将其缩小到我的.htaccess文件中的特定规则,但我不明白为什么此规则在我的新服务器上无效?

这是规则

RewriteRule ^games/([A-Za-z0-9_-\s\.]+)/?$ /index.php?page=games&console=$1 [NC,L]

完整的htaccess文件

Options +FollowSymlinks
RewriteEngine on

# compress text, html, javascript, css, xml:
AddOutputFilterByType DEFLATE text/plain
AddOutputFilterByType DEFLATE text/html
AddOutputFilterByType DEFLATE text/xml
AddOutputFilterByType DEFLATE text/css
AddOutputFilterByType DEFLATE application/xml
AddOutputFilterByType DEFLATE application/xhtml+xml
AddOutputFilterByType DEFLATE application/rss+xml
AddOutputFilterByType DEFLATE application/javascript
AddOutputFilterByType DEFLATE application/x-javascript

<IfModule mod_expires.c>
    ExpiresActive on

    ExpiresByType image/jpg "access plus 1 month"
    ExpiresByType image/jpeg "access plus 1 month"
    ExpiresByType image/gif "access plus 1 month"
    ExpiresByType image/png "access plus 1 month"
</IfModule>


ErrorDocument 404 /not-found/
RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC]
RewriteRule ^(.*)$ http://%1%{REQUEST_URI} [R=301,QSA,NC,L]


RewriteRule ^([^/\.]+)/?$ index.php?page=$1 [NC,L]
RewriteRule ^play/(.+)/?$ /index.php?page=play&console=$1 [NC,L]
#RewriteRule ^games/([A-Za-z0-9_-\s\.]+)/?$ /index.php?page=games&console=$1 [NC,L]

1 个答案:

答案 0 :(得分:0)

这样做:

ErrorDocument 404 /not-found/
RewriteEngine On

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

# skip all files and directories from rewrite rules below
RewriteCond %{REQUEST_FILENAME} -d [OR]
RewriteCond %{REQUEST_FILENAME} -f
RewriteRule ^ - [L]

RewriteRule ^play/(.+)/?$ /index.php?page=play&console=$1 [NC,L,QSA]
#RewriteRule ^games/([\w\s.-]+)/?$ /index.php?page=games&console=$1 [NC,L,QSA]

RewriteRule ^([^/.]+)/?$ index.php?page=$1 [NC,L,QSA]

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