【问题标题】:.htaccess with subdomains and GET requests.htaccess 与子域和 GET 请求
【发布时间】:2014-01-23 00:48:03
【问题描述】:

当用户访问 test.domain.co.uk 时,我想向他们显示 channels/index.php?url=test 的页面,其中 测试可以是任何东西。

我使用以下代码进行此操作:

RewriteEngine On
RewriteCond %{HTTP_HOST} !^www\.domain\.co\.uk$
RewriteCond %{HTTP_HOST} ^.+\.domain\.co\.uk$
RewriteRule ^(.*) %{HTTP_HOST}$1 [C]
RewriteRule ^(.+)\.domain\.co\.uk/? channels/index.php?url=$1 [L]

但是,如果您致电 http://test.domain.co.uk/login/

,我也希望它能够正常工作

它会显示channels/index.php?url=test&page=login

我尝试了几种方法,它只是显示它好像是 ?url=test

【问题讨论】:

  • 你想只重写这 2 个特定的 URL 并保持所有其他的不变吗?很可能您还有其他资源,例如图像、css、js 文件...
  • 没关系,我可以在之后进行排序。只是它到目前为止还没有工作的问题。

标签: apache .htaccess mod-rewrite rewrite subdomain


【解决方案1】:

添加

RewriteRule ^(.+)\.domain\.co\.uk(.+)$ channels/index.php?url=$1&page=$2 [L]

到你的规则结束。并在最后一条规则的模式中添加$

RewriteRule ^(.+)\.domain\.co\.uk/?$ channels/index.php?url=$1 [L]

然后为了避免循环,将其添加到顶部:

RewriteCond %{ENV:REDIRECT_STATUS} 200
RewriteRule ^ - [L]

【讨论】:

  • 也试过了,但是添加 $ 会导致内部服务器错误?
  • 更正:这使得页面的GET值等于channels/index.php
  • @TomWhale 查看编辑,在规则顶部添加另一个规则以防止循环
  • 工作,几乎完美,只是在 PAGE 值上添加 2 个斜线?尝试使用 (.+^[^/]*$) 但它说 test.domain.co.uklogin 找不到
  • @TomWhale 我在测试时看不到任何斜线。也许尝试将最后一个正则表达式更改为:^(.+)\.domain\.co\.uk(.+?)/*$?
【解决方案2】:

你可以使用这条规则:

RewriteEngine On
RewriteCond %{HTTP_HOST} !^www\.
RewriteCond %{HTTP_HOST} ^([^.]+)\.domain\.co\.uk$
RewriteRule ^((?!channels/index\.php).*)$ /channels/index.php?url=%1&page=$1 [L,QSA]

【讨论】:

  • 试过类似的东西,只是给你一个内部服务器错误。错误日志“由于可能的配置错误,请求超出了 10 个内部重定向的限制”
  • 再次感谢,但是“RewriteCond: 无法编译正则表达式 '(?L^|&)url=[^&]+'”
  • 哦,对不起,这是一个错字,现在更正了。尝试更新规则。
  • 适用于如果URL后面有一个参数例如login/但是页面的GET请求变成index.php否则?
  • 在最后一行 $ 之前添加了一个斜杠,它可以工作,但是,如果你去 test.domain.co.uk 它去主 index.php 而不是 channels/index.php
猜你喜欢
  • 2015-01-17
  • 2011-06-30
  • 1970-01-01
  • 2013-11-02
  • 1970-01-01
  • 2011-12-11
  • 1970-01-01
  • 2012-03-22
  • 2014-02-07
相关资源
最近更新 更多