使用 rewriteModule 从页面中删除 .aspx? [英] Removing .aspx from pages using rewriteModule?

查看:21
本文介绍了使用 rewriteModule 从页面中删除 .aspx?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用 ASP .NET rewriteModule 将 http://example.com 重写为 http://www.example.com.

I'm using ASP .NET rewriteModule to rewrite http://example.com to http://www.example.com.

<section name="rewriteModule" type="RewriteModule.RewriteModuleSectionHandler, RewriteModule"/>

然后我在 中有这个.

Then i have this inside <system.webServer>.

    <rewrite>
        <rules>
            <rule name="Canonical" stopProcessing="true">
                <match url=".*"/>
                <conditions>
                    <add input="{HTTP_HOST}" pattern="^([a-z]+[.]com)$"/>
                </conditions>
                <action type="Redirect" url="http://www.{C:0}/{R:0}" redirectType="Permanent"/>
            </rule>
        </rules>
    </rewrite>

现在我想删除页面末尾的所有 .aspx.示例:

Now i want to remove all the .aspx in the end of my pages. Example:

http://www.example.com/Register.aspx

会变成:

http://www.example.com/Register/

我该怎么做?

我使用 IIS7 在 GoDaddy 上共享虚拟主机.

推荐答案

这些是我开始每个项目的标准重写规则.我只对所有页面使用干净的 URL(示例第一条规则适用于 www.example.com/about,第二条规则适用于 www.example.com/product/123)

These are the standard rewrite rules I start every project with. I use only clean URLs for all the pages (example first rule works for www.example.com/about and second rule www.example.com/product/123)

<rewrite>
<rules>
  <rule name="Rewrite default to aspx" stopProcessing="true">
    <match url="^$" ignoreCase="false" />
    <action type="Rewrite" url="default.aspx" />
  </rule>
  <rule name="Rewrite page to aspx" stopProcessing="true">
    <match url="^([a-z0-9/]+)$" ignoreCase="false" />
    <action type="Rewrite" url="{R:1}.aspx" />
  </rule> 
</rules>
</rewrite>

我需要解析出ID(仅此案例编号)并将其添加到查询字符串的页面我在前面添加了类似的规则:

Pages where I need to parse out the ID (this case number only) and add it to the query string I add a similar rule to the front:

<rule name="Rewrite Product ID" stopProcessing="true">
  <match url="^product/([0-9]+)$" ignoreCase="false"/>
  <action type="Rewrite" url="product.aspx?id={R:1}"/>
</rule>

如果要在 URL 中使用大小写字母,请设置 ignoreCase="true"

If you want to use lower and upper case letters in the URL, set ignoreCase="true"

编辑回答你的第二个问题外加一个奖励

此规则会将 aspx 页面重定向到干净的 URL:

This rule will redirect aspx page to the clean URL:

<rule name="Redirect to clean URL" stopProcessing="true">
  <match url="^([a-z0-9/]+).aspx$" ignoreCase="true"/>
  <action type="Redirect" url="{R:1}"/>
</rule>

将 url="{R:1}" 替换为 url="{ToLower:{R:1}}" 以将 URL 更改为小写.请参阅下文,了解您为什么要这样做.

Replace url="{R:1}" with url="{ToLower:{R:1}}" to change URL to lowercase. See below why you would want to do this.

更新表单操作也是一个好主意,以便回发不会返回到丑陋的 URL.使用 IIS 7.5 或更新版本应该可以:

Also a good idea to update the Form action so that post backs don't return back to the ugly URL. Using IIS 7.5 or newer this should work:

 if (!String.IsNullOrEmpty(Request.RawUrl))
        form1.Action = Request.RawUrl;

或对于 IIS 7:

 if (!String.IsNullOrEmpty(Context.Request.ServerVariables["HTTP_X_ORIGINAL_URL"]))
        form1.Action = Context.Request.ServerVariables["HTTP_X_ORIGINAL_URL"];

还有一点要记住……最好将所有 URL 保持小写.在 URL 中混合小写/大写字符会给 SEO/Google 带来重复的内容问题.例如 website.com/About 和 website.com/about 会加载同一个页面,但 Google 会将它们编入两个单独的页面.

One more thing to keep in mind... it's a good idea to keep all URLs lower case. Mixing lower/upper case characters in the URL creates duplicate content issues for SEO/Google. For example website.com/About and website.com/about will load the same page, but Google will index them as two separate pages.

这篇关于使用 rewriteModule 从页面中删除 .aspx?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆