使用rewriteModule网页中删除的.aspx?网页、rewriteModule、aspx

2023-09-02 01:35:27 作者:花季,莫浅忆

我使用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"/>

后来才知道有这里面&LT; system.webServer&GT;

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

我怎样才能做到这一点?

How can i do that?

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

推荐答案

这是我开始每一个项目和标准重写规则。我只用干净的网址为所有页面(例如第一条规则适用于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 =真正的

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

修改回答你的第二个问题,加上奖金

这条规则将重定向aspx页面的干净网址:

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:

or for IIS 7:

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

还有一件事要记住......这是保持所有URL小写一个好主意。在URL中混合下/大写字符创建的搜索引擎优化/谷歌重复内容的问题。例如website.com/About和website.com/about将加载相同的页面,但谷歌将索引它们作为两个单独的页面。

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.