使用rewriteModule从页面中删除.aspx?

Dan*_*npe 17 .net web-config url-rewriting

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

<section name="rewriteModule" type="RewriteModule.RewriteModuleSectionHandler, RewriteModule"/>
Run Code Online (Sandbox Code Playgroud)

然后我把它放在里面<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>
Run Code Online (Sandbox Code Playgroud)

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

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

将变成:

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

我怎样才能做到这一点?

我正在使用IIS7在GoDaddy上进行共享虚拟主机托管.

Ser*_*rge 22

这些是我开始每个项目的标准重写规则.我对所有页面仅使用干净的URL(示例第一条规则适用于www.example.com/about,第二条规则适用于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>
Run Code Online (Sandbox Code Playgroud)

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

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

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

编辑以回答您的第二个问题加上奖金

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

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

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

同样最好更新Form操作,以便post backs不会返回到丑陋的URL.使用IIS 7.5或更新版本应该可行:

 if (!String.IsNullOrEmpty(Request.RawUrl))
        form1.Action = Request.RawUrl;
Run Code Online (Sandbox Code Playgroud)

或者对于IIS 7:

 if (!String.IsNullOrEmpty(Context.Request.ServerVariables["HTTP_X_ORIGINAL_URL"]))
        form1.Action = Context.Request.ServerVariables["HTTP_X_ORIGINAL_URL"];
Run Code Online (Sandbox Code Playgroud)

还有一件事需要记住......将所有网址保持在小写状态是个好主意.混合URL中的大写/小写字符会为SEO/Google创建重复的内容问题.例如,网站http://About和website.com/about将加载相同的页面,但Google会将它们编入两个单独的页面.


cwi*_*hva 5

首先,您需要删除.aspx(default.aspx)并重定向到默认值以更改浏览器地址,然后使用IIS添加.aspx并重新连接到页面

<rewrite>
    <rules>
        <clear />
        <rule name="Redirect to clean URL" enabled="true" stopProcessing="true">
            <match url="^([a-z0-9/]+).aspx$" ignoreCase="true" />
            <conditions logicalGrouping="MatchAll" trackAllCaptures="false" />
            <action type="Redirect" url="{R:1}" />
        </rule>
        <rule name="RewriteASPX" enabled="true">
            <match url="(.*)" />
            <conditions logicalGrouping="MatchAll" trackAllCaptures="false">
                <add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
                <add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" />
            </conditions>
            <action type="Rewrite" url="{R:1}.aspx" />
        </rule>
    </rules>
</rewrite>
Run Code Online (Sandbox Code Playgroud)