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

发布于 2024-11-09 08:18:00 字数 1217 浏览 0 评论 0原文

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

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

然后我把它放在里面。

    <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。示例:

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

将变成:

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

我该怎么做?

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

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>

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

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

Will turn into:

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

How can i do that?

I'm on Shared Web Hosting on GoDaddy using IIS7.

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

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

发布评论

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

评论(3

滿滿的愛 2024-11-16 08:18:00

这些是我开始每个项目时所遵循的标准重写规则。我仅对所有页面使用干净的 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>

我需要解析 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>

如果您想在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>

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

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

 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 会将它们索引为两个单独的页面。

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>

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>

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

Edit to answer your second question plus a bonus

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>

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

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;

or for IIS 7:

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

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.

撩发小公举 2024-11-16 08:18:00

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

<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>

First you need to remove the .aspx (default.aspx) and redirect to default to change the browser address then add the .aspx and rewire to page using IIS

<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>
孤芳又自赏 2024-11-16 08:18:00
<rewrite>
  <rules>
            <remove name="RewriteUserFriendlyURL1" />
            <remove name="RedirectUserFriendlyURL1" />
            <rule name="RedirectUserFriendlyURL2" stopProcessing="true">
                <match url="^www\.myserver\.com/(.*)\.aspx$" />
                <conditions>
                    <add input="{REQUEST_METHOD}" pattern="^POST$" negate="true" />
                </conditions>
                <action type="Redirect" url="www.myserver.com/{R:1}" appendQueryString="false" />
            </rule>
            <rule name="RewriteUserFriendlyURL2" stopProcessing="true">
                <match url="^www\.myserver\.com/(.*)$" />
                <conditions>
                    <add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
                    <add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" />
                </conditions>
                <action type="Rewrite" url="www.myserver.com/{R:1}.aspx" />
            </rule>
        </rules>
        <outboundRules>
            <remove name="OutboundRewriteUserFriendlyURL1" />
            <rule name="OutboundRewriteUserFriendlyURL2" preCondition="ResponseIsHtml1">
                <match filterByTags="A, Form, Img" pattern="^(.*)www\.myserver\.com/(.*)\.aspx$" />
                <action type="Rewrite" value="www.myserver.com/{R:1}" />
            </rule>
        </outboundRules>
</rewrite>

这样就可以了 - 我已经在我的本地计算机上通过 IIS 生成了这个 - 将 myserver.com 更改为您自己的 URL。您可以更改正则表达式以实际处理网址的 x.aspx 部分,然后它应该适用于所有页面

<rewrite>
  <rules>
            <remove name="RewriteUserFriendlyURL1" />
            <remove name="RedirectUserFriendlyURL1" />
            <rule name="RedirectUserFriendlyURL2" stopProcessing="true">
                <match url="^www\.myserver\.com/(.*)\.aspx$" />
                <conditions>
                    <add input="{REQUEST_METHOD}" pattern="^POST$" negate="true" />
                </conditions>
                <action type="Redirect" url="www.myserver.com/{R:1}" appendQueryString="false" />
            </rule>
            <rule name="RewriteUserFriendlyURL2" stopProcessing="true">
                <match url="^www\.myserver\.com/(.*)$" />
                <conditions>
                    <add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
                    <add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" />
                </conditions>
                <action type="Rewrite" url="www.myserver.com/{R:1}.aspx" />
            </rule>
        </rules>
        <outboundRules>
            <remove name="OutboundRewriteUserFriendlyURL1" />
            <rule name="OutboundRewriteUserFriendlyURL2" preCondition="ResponseIsHtml1">
                <match filterByTags="A, Form, Img" pattern="^(.*)www\.myserver\.com/(.*)\.aspx$" />
                <action type="Rewrite" value="www.myserver.com/{R:1}" />
            </rule>
        </outboundRules>
</rewrite>

this will do it - I have generated this vis IIS on my local machine - change myserver.com to your own URL. you can change the regex to actually take care of the x.aspx part of the url then it should work across all pages

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