【问题标题】:subdomain redirection in htaccess [closed]htaccess中的子域重定向[关闭]
【发布时间】:2012-01-13 01:45:56
【问题描述】:

我想知道使用 htaccess 规则是否可以实现以下场景。 我希望将一个子域重定向到另一个 url。 我已联系服务器管理员将“test”子域添加到“example.com”域。 主域没有其他子域。

我必须在 htaccess 中加入什么规则才能实现: http://test.example.com 被重定向到http://www.something-else.com

注意:www.something-else.com 是一个复杂的网址(长度为 200 个字符)

编辑 我的完整 htaccess 文件现在看起来像:

Options +FollowSymLinks
RewriteEngine on
RewriteOptions inherit
RewriteBase /

RewriteCond %{HTTP_HOST} ^test.example.com$ [NC]
RewriteRule ^(.*)$ http://www.example.com/redir.php [L]

RewriteCond %{HTTP_HOST} ^example.com [NC]
RewriteRule ^(.*)$ http://www.example.com/$1 [L,R=301]

htaccess 文件位于 example.com 根目录。 我没有对 test 子目录的物理访问权限,尽管我知道它存在 - ping 到该 url 会显示 example.com 的 IP

在浏览器地址栏输入 test.example.com 无效。白屏,没有重定向,什么都没有。如果那里存在某个页面,我不知道。

【问题讨论】:

  • @宙斯之书:somdomain.com/a/folder1/…
  • stackoverflow.com/a/1460652/567986 但您应该删除末尾的 ,P 并删除 %{REQUEST_URI}。
  • 我尝试了这个规则,它对我有用(当然是使用我的网站)现在你能提供更多关于has no effect. White screen, no redirection, no nothing 的信息吗,比如你能分享一个 httpfox 吗?可能是子域设置不正确之类的
  • @Book Of Zeus 当我在浏览器中输入子域地址时,它会显示一个白屏。似乎有一个页面,但没有内容。如果我尝试访问 test.example.com/index.htm,我会收到未找到的错误。网址保持不变(test.example.com)
  • 日志说什么? test.example.com 文件夹中有什么(任何 .htaccess?)?

标签: .htaccess mod-rewrite subdomain redirect


【解决方案1】:

对于静态网址:

RewriteEngine On
RewriteCond %{HTTP_HOST} ^test.example.com$ [NC]
RewriteRule ^(.*)$ https://www.somdomain.com/a/folder1/somepage?var=xxx&var2=xxx&var3=xxx&var4=http://another-domain.com/folder1/xxxxx/?&var5=xxx&var6=xxxx [R=301,NC,L]

对于动态网址(如果原始域有需要移动到其他域的文件夹):

RewriteEngine On
RewriteCond %{HTTP_HOST} ^test.example.com$ [NC]
RewriteRule ^(.*)$ https://www.somdomain.com%{REQUEST_URI} [R=301,NC,L,QSA]

来自您的编辑:

RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} ^test.example.com$ [NC]
RewriteRule ^(.*)$ redir.php [R=301,NC,L]

【讨论】:

  • 不,它确实有效。乍一看,我认为重定向 url 破坏了 htaccess。我把它放在一个 php 文件中并更改我的 htaccess 以将子域重定向到 php 文件。还是行不通。 RewriteEngine On RewriteCond %{HTTP_HOST} ^test.example.com$ [NC] RewriteRule ^(.*)$ example.com/redir.php [R=301,NC,L]
  • 还是什么都没有。它应该工作,我知道,但它没有。我已经编辑了我的原始帖子以显示我的完整 htaccess。我很迷惑。一开始看起来很简单
猜你喜欢
  • 2019-10-13
  • 2012-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-01-15
  • 1970-01-01
相关资源
最近更新 更多