2

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

山姆.xyz.com

重定向到

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

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

重写引擎开启

RewriteCond %{HTTP_HOST} !^www.xyz.com [NC]

RewriteCond %{HTTP_HOST} ^([^.]+).xyz.com

重写规则 ^(.*)$ /view.php?id=%1 [L,R]

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

山姆.xyz.com

重定向到

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

但是没有无限循环就不会移动到上面的url。请帮帮我。提前致谢,

4

2 回答 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]
于 2013-06-27T19:36:12.600 回答
1

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

确保 url 不包含:id=prefix。

此解决方案可防止有人调用 url: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 以不将访问者重定向到 url (/view.php?id=%1)

于 2013-06-27T19:57:17.680 回答