将 http://mydomain.example/ctrlr/act/val 转发到 http://WWW.mydomain.example/ctrlr/act/val

发布于 2024-08-06 00:07:06 字数 311 浏览 4 评论 0原文

我有一个在 ASP.NET MVC (V 1.0) 上编写的应用程序。该应用程序在 IIS7 上运行,DNS 由 GoDaddy 提供。

我想将来自 http://mydomain.example/ctrlr/act/value 的任何请求转发到以下形式的 URL:http://WWW.mydomain.example/ ctrlr/act/value

基本上,如果有人尝试访问 http://mydomain.example ,我想将 WWW 添加到主机名中,

那么最好的方法是什么?

I have an application written on ASP.NET MVC (V 1.0). The application runs on IIS7 and the DNS is provided by GoDaddy.

I would like to forward any request that comes from http://mydomain.example/ctrlr/act/value to a URL of this form: http://WWW.mydomain.example/ctrlr/act/value

Basically, I want to add WWW to the Host-name if someone tries to reach http://mydomain.example

What would be the best way to do this?

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

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

发布评论

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

评论(3

难得心□动 2024-08-13 00:07:06

我想你会找到适合这个的答案 问题

我同意你强制使用 www 的想法,就好像 Stack Overflow 决定 使用它 我确实相信他们在调整 cookie 性能并不得不使用 sstatic.net 而不是 images.stackoverflow.com 时感到遗憾。

在这里为您保存重定向是您需要做的要点。

以下是 IIS7 规则,用于将 WWW 前缀添加到所有传入 URL。将此 XML 片段剪切并粘贴到下面的 web.config 文件中

<system.webServer> / <rewrite> / <rules>

  <rule name="Add WWW prefix" >
    <match url="(.*)" ignoreCase="true" />
    <conditions>
      <add input="{HTTP_HOST}" pattern="^domain\.com" />
    </conditions>
    <action type="Redirect" url="http://www.domain.example/{R:1}"
            redirectType="Permanent" />
  </rule>

I think you will find an answer that suits from this question

I would agree with your idea of forcing the use off www, as though Stack Overflow decided to use it I do believe they regretted when tweaking performance for cookies and having to use sstatic.net instead of images.stackoverflow.com say.

To save you a redirect here is gist of what you need to do.

Here’s the IIS7 rule to add the WWW prefix to all incoming URLs. Cut and paste this XML fragment into your web.config file under

<system.webServer> / <rewrite> / <rules>

  <rule name="Add WWW prefix" >
    <match url="(.*)" ignoreCase="true" />
    <conditions>
      <add input="{HTTP_HOST}" pattern="^domain\.com" />
    </conditions>
    <action type="Redirect" url="http://www.domain.example/{R:1}"
            redirectType="Permanent" />
  </rule>
最近可好 2024-08-13 00:07:06

您可以使用 Code Plex 中的网址重写器。您可以通过执行以下操作将所有内容强制到 www.domain.example

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

或者,如果您想让它更适合您的域,

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

这也支持 mod_rewrite 提供的一大堆其他重写器功能。

You can use Url Rewriter from Code Plex. You can force everything to www.domain.example by doing the following:

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

Or if you want to make it more specific for your domain

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

This also supports a whole bunch of other rewriter functions provided by mod_rewrite.

爱给你人给你 2024-08-13 00:07:06

我选择在应用程序级别而不是 IIS 执行此操作。这是我为此编写的快速操作过滤器。只需将类添加到项目中的某个位置,然后就可以将 [RequiresWwww] 添加到单个操作或整个控制器。

public class RequiresWww : ActionFilterAttribute
    {
        public override void OnActionExecuting(ActionExecutingContext filterContext)
        {
            HttpRequestBase req = filterContext.HttpContext.Request;
            HttpResponseBase res = filterContext.HttpContext.Response;

            //IsLocal and IsLoopback i'm not too sure on the differences here, but I have both to eliminate local dev conditions.
            if (!req.IsLocal && !req.Url.Host.StartsWith("www") && !req.Url.IsLoopback)
            {
                var builder = new UriBuilder(req.Url)
                {
                    Host = "www." + req.Url.Host
                };

                res.Redirect(builder.Uri.ToString());

            }

            base.OnActionExecuting(filterContext);
        }
    }

然后

[RequiresWwww]
public ActionResult AGreatAction()
{
...
}

或者

[RequiresWwww]
public class HomeController : BaseAppController
{
..
..
}

I opted to do this at the app level, instead of IIS. Here's a quick action filter I wrote to do this. Simply add the class somewhere in your project, and then you can add [RequiresWwww] to a single action or an entire controller.

public class RequiresWww : ActionFilterAttribute
    {
        public override void OnActionExecuting(ActionExecutingContext filterContext)
        {
            HttpRequestBase req = filterContext.HttpContext.Request;
            HttpResponseBase res = filterContext.HttpContext.Response;

            //IsLocal and IsLoopback i'm not too sure on the differences here, but I have both to eliminate local dev conditions.
            if (!req.IsLocal && !req.Url.Host.StartsWith("www") && !req.Url.IsLoopback)
            {
                var builder = new UriBuilder(req.Url)
                {
                    Host = "www." + req.Url.Host
                };

                res.Redirect(builder.Uri.ToString());

            }

            base.OnActionExecuting(filterContext);
        }
    }

Then

[RequiresWwww]
public ActionResult AGreatAction()
{
...
}

or

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