【问题标题】:htaccess redirect domain root, but not /#AnchorNamehtaccess 重定向域根目录,但不重定向 /#AnchorName
【发布时间】:2014-01-24 06:10:57
【问题描述】:



我拼命想弄清楚如何仅重定向根目录,而不在斜杠 / (包括锚点#)之后重定向(字面意思)任何内容。

我有一个登陆页面,我也想重定向所有 domainname.com 流量,但还需要让人们通过使用 domainname.com/store、domainname.com/#about 或 domainname.com 等深层链接进入主站点/#up(主页和导航使用锚点)。

我正在使用的代码如下,结果是:
- 重定向 domainname.com > loginpage.domainname.com (GOOD)
- 不重定向 domainname.com/store (GOOD)
- 重定向 domainname.com/#about > loginpage.domainname.com/#about(不好 - 这就是问题)

RewriteEngine On
RewriteCond %{HTTP_HOST} ^(www\.)?domainname\.com [NC]
RewriteRule ^$ http://landingpage.domainname.com [L,NC,R=301]

谢谢,

詹姆斯

【问题讨论】:

  • 你是否意识到web服务器只得到http://landingpage.domainname.com/而留下#about部分。

标签: php apache .htaccess mod-rewrite redirect


【解决方案1】:

您不能使用 htaccess 来执行此操作,因为 URL 的 #about 部分永远不会发送到服务器,如果它永远不会发送到服务器,所有 apache 和 mod_rewrite 都会看到是/,没有#about#about 是一个 URL 片段,严格地保留在浏览器端。没有办法使用 mod_rewrite 来阻止这种重定向。

因此,您必须编写 javascript 来执行重定向,并在您的 htaccess 文件中摆脱重定向。

<script language=”javascript” type=”text/javascript”>
if(window.location.hostname != "landingpage.domainname.com" &&  // check that hostname isn't landingpage
   !window.location.hash &&  // check that there is no fragment
   window.location.pathname == "/") {   
  window.location = "http://landingpage.domainname.com/";
}
</script>

【讨论】:

  • 非常感谢您的解释!
【解决方案2】:

詹姆斯

您是否可以选择将资源从 domain.com/store 删除到 sub.domain.com/store?看起来您将进行大量重定向,随着时间的推移会变得笨拙。

无论如何,我只会重写 domain.com 请求,而别管其他所有请求。

重写引擎开启

RewriteCond %{HTTP_HOST} ^http://www.example.com [NC]

重写规则 ^(.*)$ http://sub.example.com/$1 [R=301,L]

【讨论】:

    猜你喜欢
    • 2013-09-12
    • 2017-06-26
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-05-04
    • 1970-01-01
    • 1970-01-01
    • 2017-01-12
    相关资源
    最近更新 更多