【问题标题】:URL rewrite for Subdomain子域的 URL 重写
【发布时间】:2014-11-14 08:03:11
【问题描述】:

情况很简单。我在我的 cpanel 中创建了一个通配符子域,如下所示:*.mysite.com。

我可以加载页面,但我想将通配符的值作为 GET 的参数,以便页面可以根据该 GET 值实际显示相关内容。

所以我想知道的是是否有可能有一个允许我执行以下操作的重写规则。

从以下网址获取:$_GET['store']=='andystore':http://andystore.mysite.com

【问题讨论】:

  • 好的。您在谈论重写规则,但您提供的唯一代码是 PHP 的 $_GET 方法。是哪个?

标签: php .htaccess url mod-rewrite url-rewriting


【解决方案1】:

您可以在 DOCUMENT_ROOT/.htaccess 文件中使用此代码:

RewriteEngine On
RewriteBase /

# capture first part of host name
RewriteCond %{HTTP_HOST} ^([^.]+)\.mysite\.com$ [NC]
# make sure store= query parameter isn't there
RewriteCond %{QUERY_STRING} !(^|&)store= [NC]
# rewrite to current URI?store=backererence #1 from host name
RewriteRule ^ %{REQUEST_URI}?store=%1 [L,QSA]

参考:Apache mod_rewrite Introduction

Apache mod_rewrite Technical Details

【讨论】:

  • 谢谢。工作!甜蜜而轻松。
  • 能不能简单解释一下,让我和其他可能有类似问题的菜鸟,看代码能看懂一点?
  • 我有同样的问题,但是使用 cakephp,我已经尝试过您的代码,但它重定向到了其他页面。并显示错误消息“抱歉。您要查找的网站不可用”stackoverflow.com/questions/26212025/…
【解决方案2】:
RewriteCond %{HTTP_HOST} ^mysite.com$ [NC]
RewriteRule ^(.+)$ http://$1.mysite.com [L,R=301]

【讨论】:

  • 您可能想解释一下这是做什么的。 SO 的存在是为了教育,而不仅仅是回答问题。
猜你喜欢
  • 2012-09-17
  • 2014-05-15
  • 1970-01-01
  • 1970-01-01
  • 2012-10-02
  • 2018-10-22
  • 2011-05-25
  • 1970-01-01
相关资源
最近更新 更多