首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >使用rewriteModule从页面中删除.aspx?

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

Stack Overflow用户
提问于 2011-05-23 21:05:53
回答 3查看 17.1K关注 0票数 17

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

代码语言:javascript
复制
<section name="rewriteModule" type="RewriteModule.RewriteModuleSectionHandler, RewriteModule"/>

然后我把这个放在<system.webServer>里面。

代码语言:javascript
复制
    <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上的共享主机上。

EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2011-06-16 04:11:07

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

代码语言:javascript
复制
<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 (仅此案例编号)并将其添加到查询字符串的页面上,我在前面添加了一个类似的规则:

代码语言:javascript
复制
<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:

代码语言:javascript
复制
<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更改为小写。看看下面你为什么想要这样做。

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

代码语言:javascript
复制
 if (!String.IsNullOrEmpty(Request.RawUrl))
        form1.Action = Request.RawUrl;

或者对于IIS 7:

代码语言:javascript
复制
 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会将它们作为两个单独的页面进行索引。

票数 22
EN

Stack Overflow用户

发布于 2013-03-06 14:42:06

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

代码语言:javascript
复制
<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>
票数 5
EN

Stack Overflow用户

发布于 2011-05-23 21:15:55

代码语言:javascript
复制
<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>

这样就可以了-我已经在本地机器上生成了这个vis将myserver.com更改为您自己的URL。您可以更改正则表达式以实际处理url的x.aspx部分,然后它应该可以在所有页面上工作

票数 3
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/6097592

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档