【问题标题】:Using HTTP_HOST to identify the identify the previous host name使用 HTTP_HOST 来识别识别之前的主机名
【发布时间】:2013-10-01 01:51:50
【问题描述】:

我已将我的站点 test.com 的域名更改为 test2.com

域名由test.com to test2.com更改

I want to know if all the pages like test.com/* will be redirected to test2.com/*

在 test2.com 中,我正在 mod_rewrite 中编写重写规则来识别智能移动设备。

For all smart phone I would like to redirect requests test.com/* to test2.com (home page)

For every non smart phone I would like to redirect test.com/* to test2.com/*

我为此研究了以下条件。我不确定这是否正确。

我已经尝试过这样的事情。

RewriteEngine On
RewriteCond %{HTTP_HOST} ^test\.com$ [AND]
RewriteCond %{HTTP_USER_AGENT} mobile
RewriteCond %{HTTP_USER_AGENT} !(android|blackberry|iphone|ipod|windows phone) [NC]
RewriteRule ^(.*)$ http://test2.com/mobil/#1 [L]

【问题讨论】:

  • “对于所有智能手机,我想将请求 test.com/* 重定向到 test2.com(主页)” - 为什么? .当网站将我从深层链接(从论坛链接或搜索引擎中找到)重定向​​到他们的移动主页时,我非常讨厌它。我直接离开这些网站,再也没有回来。

标签: regex apache .htaccess http mod-rewrite


【解决方案1】:

通过httpd.conf 启用mod_rewrite.htaccess,然后将此代码放入您的DOCUMENT_ROOT/.htaccess 文件中:

Options +FollowSymLinks -MultiViews
# Turn mod_rewrite on
RewriteEngine On
RewriteBase /

# For mobile devices:
RewriteCond %{HTTP_HOST} ^(www\.)?test\.com$ [NC]
RewriteCond %{HTTP_USER_AGENT} (android|blackberry|iphone|ipod|windows phone) [NC]
RewriteRule ^ http://test2.com/ [L,R=301]

# For non-mobile devices:
RewriteCond %{HTTP_HOST} ^(www\.)?test\.com$ [NC]
RewriteRule ^(.*)$ http://test2.com/$1 [L,R=301]

【讨论】:

  • 绝妙的答案。谢谢。
  • 我只会在 test2.com 服务器上写这个。在这种情况下,我是否需要在重写规则中提及完整的网站名称。
  • 你将在test.comDOCUMENT_ROOT/.htaccess写这篇文章
  • 即使您关闭了test.com 主机,您仍然拥有该域。只需将其 A 记录指向与指向 test2.com 的主机相同的主机。一旦完成,将上面的代码放入test2.com DOCUMENT_ROOT/.htaccess
  • 我不得不使用完整地址,因为我们正在处理 2 个域以及从一个域到另一个域的外部重定向。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多