首页 文章

.htaccess重写规则导致无限循环

提问于
浏览
2

如果任何通配符 as a subdomain 条目命中浏览器,我希望我的.htaccess文件重定向到某个页面 . 即我想要

sam.xyz.com

要重定向到

sam.xyz.com/view.php?id=sam

我正在使用以下重写规则进行重定向 .

RewriteEngine On RewriteCond%!^ www.xyz.com [NC] RewriteCond% ^([^.]) . xyz.com RewriteRule ^( . *)$ / view.php?id =%1 [L,R]

我面临的问题是它不会转移到新域保持查询字符串,而是生成无限循环

sam.xyz.com

重定向到

http://sam.xyz.com/view.php?id=sam

但是没有无限循环就没有移动到url上面 . 请帮助我 . 提前致谢,

2 回答

  • 3

    您应该添加重定向条件以防止重定向循环:

    RewriteCond %{REQUEST_URI} !^/view\.php
    

    整个代码将是:

    RewriteEngine On
    RewriteCond %{HTTP_HOST} !^www.xyz.com [NC]
    RewriteCond %{HTTP_HOST} ^([^.]+).xyz.com
    RewriteCond %{REQUEST_URI} !^/view\.php
    RewriteRule ^(.*)$ /view.php?id=%1 [L,R]
    
  • 1

    您正在重定向到:prefix.domain.tld / view.php?id = prefix

    确保url不包含:id = prefix .

    这个解决方案可以防止有人打电话给网址:aaa.example.com/view.php?id = bbb

    RewriteEngine On
    RewriteCond %{HTTP_HOST} !^www.xyz.com [NC]
    RewriteCond %{HTTP_HOST} ^([^.]+).xyz.com
    RewriteCond %1::%{QUERY_STRING} !^([^:]+)::.*id=\1
    RewriteRule ^ /view.php?id=%1 [L,R]
    

    注意:重写规则中的( . *)已过时 .

    离开R不要将访问者重定向到网址(/view.php?id=%1)

相关问题