如何移动 ASP.NET MVC 站点的 URL

发布于 2024-12-10 10:36:54 字数 723 浏览 0 评论 0原文

我的 SEO 同事发出了一些涉及移动

www.mydomain.co.uk/news
的 URL 请求 www.mydomain.co.uk/news/{id}/{title}

www.mydomain.co.uk/uk-news
www.mydomain.co.uk/uk-news/{id}/{title}

执行此操作的最佳方法是什么?

我只是想更改 global.asax 文件中的路由

FROM:

    routes.MapRoute(
      "News", // Route name
      "News", // URL with parameters
      new { controller = "News", action = "Index" } // Parameter defaults
    );

TO:

routes.MapRoute(
  "News", // Route name
  "uk-news", // URL with parameters
  new { controller = "News", action = "Index" } // Parameter defaults
);

并在基本 www.mydomain.co.uk/news 上设置 301 重定向

有没有更好的方法来做到这一点?

My SEO colleague has fired in a few url requests that involve moving

www.mydomain.co.uk/news
www.mydomain.co.uk/news/{id}/{title}

to

www.mydomain.co.uk/uk-news
www.mydomain.co.uk/uk-news/{id}/{title}

What is the best way to carry this out.

I was just thinking of changing the route in the global.asax file

FROM:

    routes.MapRoute(
      "News", // Route name
      "News", // URL with parameters
      new { controller = "News", action = "Index" } // Parameter defaults
    );

TO:

routes.MapRoute(
  "News", // Route name
  "uk-news", // URL with parameters
  new { controller = "News", action = "Index" } // Parameter defaults
);

and setting up a 301 redirect on the basic www.mydomain.co.uk/news

Is there a better way to do this?

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

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

发布评论

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

评论(1

空心↖ 2024-12-17 10:36:54

我不这么认为。您的方向是正确的,更改路由上的 URL 并执行 301 永久重定向是正确的方法。对于 301,请尝试使用 URL 重写模块。

重写模块

<!-- Web.config file in subdirectory to be redirected -->
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
  <system.webServer>
    <rewrite>
      <rules>
        <rule name="RedirectRule" stopProcessing="true">
          <match url="(.*)" ignoreCase="true" />
          <action type="Redirect" url="http://www.newdomain.com/{R:1}" redirectType="Permanent" />
            <conditions>
                <add input="{HTTP_HOST}" pattern="www\.old-domain\.com" />
            </conditions>
        </rule>
      </rules>
    </rewrite>
  </system.webServer>
</configuration>

ISAPI_Rewrite

RewriteCond Host: ^mydomain\.co.uk
RewriteRule /news http://www.mydomain.co.uk/uk-news [I,O,RP,L] 

I(忽略大小写)

表示无论大小写,都匹配字符。该标志影响 RewriteHeader 指令和所有相应的 RewriteCond 指令。

O (nOrmalize)

在处理之前规范化字符串。规范化包括删除 URL 编码、非法字符等。此外,URI 的 IIS 规范化会完全删除查询字符串。因此,如果需要查询字符串,则不应使用规范化。此标志对于 URL 和 URL 编码标头很有用。

RP(永久重定向)

几乎与 [R] 标志相同,但发出 301(永久移动)HTTP 状态代码而不是 302(临时移动)。

L(最后一条规则)

在此停止重写过程,并且不再应用任何重写规则。

I don't think so. Your on the right track, changing the URL on the route and doing the 301 permanent redirect is the way to go. For the 301 try using the URL Rewrite Module.

Rewrite Module

<!-- Web.config file in subdirectory to be redirected -->
<?xml version="1.0" encoding="UTF-8"?>
<configuration>
  <system.webServer>
    <rewrite>
      <rules>
        <rule name="RedirectRule" stopProcessing="true">
          <match url="(.*)" ignoreCase="true" />
          <action type="Redirect" url="http://www.newdomain.com/{R:1}" redirectType="Permanent" />
            <conditions>
                <add input="{HTTP_HOST}" pattern="www\.old-domain\.com" />
            </conditions>
        </rule>
      </rules>
    </rewrite>
  </system.webServer>
</configuration>

ISAPI_Rewrite

RewriteCond Host: ^mydomain\.co.uk
RewriteRule /news http://www.mydomain.co.uk/uk-news [I,O,RP,L] 

I (ignore case)

Indicates that characters are matched regardless of a case. This flag affects RewriteHeader directive and all corresponding RewriteCond directives.

O (nOrmalize)

Normalizes string before processing. Normalization includes removing of an URL-encoding, illegal characters, etc. Also, IIS normalization of an URI completely removes query string. So, normalization should not be used if query string is needed. This flag is useful with URLs and URL-encoded headers.

RP (permanent redirect)

Almost the same as the [R] flag but issues 301 (moved permanently) HTTP status code instead of 302 (moved temporary).

L (last rule)

Stop the rewriting process here and don't apply any more rewriting rules.

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