这是使用 301 重定向来控制使用哪个域名的最佳位置吗?

发布于 2024-12-24 16:17:36 字数 1208 浏览 4 评论 0原文

我正在使用 ColdFusion 9.0.1

我有一个可通过几个域访问的新站点,例如:

mydomain.com
www.mydomain.com
foo.mydomain.com

出于 SEO 和跟踪目的,我想确保只有“mydomain.com”被索引和访问。因此,尝试通过其他域访问我的网站的每个请求都将被 301 定向到“mydomain.com”。

我想确保捕获并保留查询字符串,这样我就不会只是将人们发送到主页。

我还将确保我可以在 127.0.0.1 本地访问该站点,

我想知道代码中的哪个位置是执行这种特定类型的重定向的最佳位置。我猜它位于 application.cfc 顶部附近的 onRequestStart() 方法中。

这是放置代码的最佳位置吗?该代码是否完整?有更好的方法来编码吗?

<cfscript>
ThisHost = CGI.HTTP_HOST;
QString = CGI.QUERY_STRING;
GoToURL = "http://mydomain.com?" & QString;
if (ThisHost != "mydomain.com" && ThisHost != "127.0.0.1") {
    writeOutput("<cfheader statuscode='301' statustext='Moved permanently'>");
    writeOutput("<cfheader name='location' value='#GoToURL#'>");
    abort;
}
</cfscript>

更新

我知道这不是完成我需要的最佳方式,因为此任务更适合网络服务器的技能集。这是我的代码,直到我可以在网络服务器上实现它:

<cfscript
ThisHost = CGI.HTTP_HOST;
QString = CGI.QUERY_STRING;
GoToURL = "http://flyingpiston.com/?" & QString;
if (ThisHost != "flyingpiston.com" && ThisHost != "127.0.0.1:8500") {
    location(GoToURL, false, 301);  
}
<cfscript

I am using ColdFusion 9.0.1

I have a new site that is accessible through a few domains such as:

mydomain.com
www.mydomain.com
foo.mydomain.com

For SEO and tracking purposes, I want to make sure that only "mydomain.com" is indexed and accessed. So, every request that tries to access my site through other domains will be 301 directed to "mydomain.com".

I want to make sure that I capture and preserve the query string so that I don't just send people to the home page.

I will also make sure that I can access the site locally at 127.0.0.1

I wondering where in the code is the best place to do this SPECIFIC type of redirect. My guess it's in application.cfc near the top, in the onRequestStart() method.

Is this best place to put the code and does is this code complete? Is there a better way to code this?

<cfscript>
ThisHost = CGI.HTTP_HOST;
QString = CGI.QUERY_STRING;
GoToURL = "http://mydomain.com?" & QString;
if (ThisHost != "mydomain.com" && ThisHost != "127.0.0.1") {
    writeOutput("<cfheader statuscode='301' statustext='Moved permanently'>");
    writeOutput("<cfheader name='location' value='#GoToURL#'>");
    abort;
}
</cfscript>

UPDATE

I know this isn't the best way to accomplish what I need, because this task is much better suited to the web server's skill set. Here's my code till I can implement this on the web server:

<cfscript
ThisHost = CGI.HTTP_HOST;
QString = CGI.QUERY_STRING;
GoToURL = "http://flyingpiston.com/?" & QString;
if (ThisHost != "flyingpiston.com" && ThisHost != "127.0.0.1:8500") {
    location(GoToURL, false, 301);  
}
<cfscript

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

澜川若宁 2024-12-31 16:17:36

我同意其他评论和答案,即在网络服务器上执行此操作是更好的解决方案。我还要指出,如果您想使用脚本语法,这是完全错误的,只会向浏览器返回一个字符串:

writeOutput("<cfheader name='location' value='#GoToURL#'>");

在 ColdFusion 9 中,您可以使用 location() 函数:

location("url", addtoken, statusCode);

在您的情况下:

location(GoToURL, false, 301);

您的 GoToURL 变量也缺少页面名称,因此您需要在 ? 之前将 CGI.SCRIPT_NAME 添加到混合中code> 获取被调用的完整 URL。

使用标签语法(我相信从 ColdFusion 8 开始),无需使用 CFHEADER 标签进行 301 重定向。 CFLOCATION 标记现在支持 statuscode 属性,可以根据需要将其设置为 301。

I agree with other comments and answers that doing this at the web server is a better solution. I would also point out that if you want to use the script syntax, this is entirely wrong and will simply return a string to the browser:

writeOutput("<cfheader name='location' value='#GoToURL#'>");

In ColdFusion 9, you would instead use the location() function:

location("url", addtoken, statusCode);

In your case:

location(GoToURL, false, 301);

Your GoToURL variable is also missing the page name, so you'd need to add CGI.SCRIPT_NAME into the mix just before the ? to get the full URL being called.

With the tag syntax (as of ColdFusion 8 I believe), there is no need to use the CFHEADER tag for a 301 redirect. The CFLOCATION tag now supports a statuscode attribute which can be set to 301 as needed.

时光倒影 2024-12-31 16:17:36

如果您使用的是 IIS 7.0,则可以为规范重定向配置 web.config 文件,如下所示:

<configuration>
  <system.webServer>
    <rewrite>
      <rules>
        <rule name="Redirect to WWW" stopProcessing="true">
          <match url=".*" />
          <conditions>
            <add input="{HTTP_HOST}" pattern="^domain.com$" />
          </conditions>
          <action type="Redirect" url="http://www.domain.com/{R:0}"
               redirectType="Permanent" />
        </rule>
      </rules>
    </rewrite>
  </system.webServer>
</configuration>

查看此 链接 了解更多选项。

If you are on IIS 7.0 the you may be able configure your web.config file for a canonical redirect like so:

<configuration>
  <system.webServer>
    <rewrite>
      <rules>
        <rule name="Redirect to WWW" stopProcessing="true">
          <match url=".*" />
          <conditions>
            <add input="{HTTP_HOST}" pattern="^domain.com$" />
          </conditions>
          <action type="Redirect" url="http://www.domain.com/{R:0}"
               redirectType="Permanent" />
        </rule>
      </rules>
    </rewrite>
  </system.webServer>
</configuration>

Check out this link for additional options.

毁梦 2024-12-31 16:17:36

前面的答案显示了如何将domain.com重定向到www.domain.com。如果要将 www.domain.com 重定向到“domain.com”,则需要一个如下所示的 web.config 文件:

<?xml version="1.0" encoding="UTF-8"?>
<configuration>
    <location path="" overrideMode="Inherit">
        <system.webServer>
            <rewrite>
                <rules>
                    <rule name="remove www" patternSyntax="Wildcard" stopProcessing="true">
                        <match url="*" />
                        <conditions logicalGrouping="MatchAny">
                            <add input="{HTTP_HOST}" pattern="www.*" />
                            <add input="{HTTP_HOST}" pattern="foo.*" />
                        </conditions>
                        <serverVariables />
                        <action type="Redirect" url="http://{C:1}" />
                    </rule>
                </rules>
            </rewrite>
        </system.webServer>
    </location>
</configuration>

上述 web.config 文件是在 IIS 7.5 (Windows Server 2008 R2) 上创建的。
您的主机需要安装上面提到的 URL 重写模块才能使其正常工作。
web.config 文件存储在站点的根文件夹中。
上面的示例将“www”和“foo”子域重定向到该域。

10 个 URL 重写技巧和 Tricks 文章对我来说是一个很好的参考。

The previous answer shows how to redirect domain.com to www.domain.com. If you want to redirect www.domain.com to 'domain.com', you will need a web.config file that looks like this:

<?xml version="1.0" encoding="UTF-8"?>
<configuration>
    <location path="" overrideMode="Inherit">
        <system.webServer>
            <rewrite>
                <rules>
                    <rule name="remove www" patternSyntax="Wildcard" stopProcessing="true">
                        <match url="*" />
                        <conditions logicalGrouping="MatchAny">
                            <add input="{HTTP_HOST}" pattern="www.*" />
                            <add input="{HTTP_HOST}" pattern="foo.*" />
                        </conditions>
                        <serverVariables />
                        <action type="Redirect" url="http://{C:1}" />
                    </rule>
                </rules>
            </rewrite>
        </system.webServer>
    </location>
</configuration>

The above web.config file was created on IIS 7.5 (Windows Server 2008 R2).
Your host will need to install the URL Rewrite Module as mentioned above in order for this to work.
The web.config file is stored in the root folder of your site.
The above example will redirect 'www' and 'foo' sub-domains to the domain.

This 10 URL Rewriting Tips and Tricks article has been a good reference for me.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文