【问题标题】:URL Rewrite Default Document Web Page to SEO URLURL 将默认文档网页重写为 SEO URL
【发布时间】:2012-07-12 12:21:35
【问题描述】:

我已经开始为我的站点使用 ASP.NET 路由。我一直在通过 Global.asax 文件中的 Application_Start() 注册路由。

routes.MapPageRoute("ROUTE-ABOUT", "about", "~/About.aspx");
routes.MapPageRoute("ROUTE-CONTACT", "contact", "~/Contact.aspx");
//etc...

这非常适合“关于”和“联系”页面。

我想要什么:

我的主页是 Home.aspx,我想做的是重写任何访问过的人

http://localhost/mysite.com/Home.aspx 

http://localhost/mysite.com/Home 

我的尝试

  • 我的站点在我的机器上的本地 IIS v7.5 中运行(完整 管理员权限)。
  • 我已将以下内容添加到我的 Web.config

Web.config

<rewrite>
    <rules>
        <rule name="HOMETOSEO" stopProcessing="true">
            <match url="Home\.aspx" />
            <action type="Redirect" url="home" appendQueryString="false" />
        </rule>
    </rules>
</rewrite>

提前致谢

【问题讨论】:

  • 您使用的是哪个版本的 IIS?

标签: asp.net url-rewriting routing webforms


【解决方案1】:

经过数小时的尝试,我最终设法使用 Global.asax 文件的 Web.configApplication_Start() 中的以下条目使其工作:

Web.config

<rewrite>
    <rules>
        <rule name="default" enabled="true" patternSyntax="ECMAScript" stopProcessing="false">
            <match url="(.*)Home\.aspx" ignoreCase="true" />
            <action type="Redirect" url="home" appendQueryString="false" />
        </rule>
        <rule name="lower" enabled="true" patternSyntax="ECMAScript" stopProcessing="true">
            <match url="[A-Z]" ignoreCase="false" />
            <action type="Redirect" url="{ToLower:{URL}}" /> 
        </rule>
    </rules>
</rewrite>

Global.asax

protected void Application_Start(object sender, EventArgs e)
{
    //...
    BuildStaticRoutes(RouteTable.Routes);
    //...
}

public void BuildStaticRoutes(RouteCollection routes)
{
    //...
    routes.MapPageRoute("ROUTE-HOME", "home", "~/Home.aspx");
    //...
}

【讨论】:

    【解决方案2】:

    如果您使用的是 IIS v7.5,您可以在 web.config 中添加它

    <system.webServer>  
    <rewrite>
                <rules>                
                    <rule name="HOMETOSEO" stopProcessing="true">
                        <match url="^Home" />
                        <conditions>
                            <add input="{REQUEST_FILENAME}" matchType="IsFile" negate="true" />
                            <add input="{REQUEST_FILENAME}" matchType="IsDirectory" negate="true" />
                        </conditions>
                        <action type="Rewrite" url="Home.aspx" />
                    </rule>
                </rules>
    </rewrite>
    </system.webServer>  
    

    当您输入http://mysite.com/Home 时,它将显示http://mysite.com/Home.aspx。这是你所追求的还是相反的?

    【讨论】:

    • 我用 尝试过它并没有为我重写它。还有什么我可能会丢失的吗?当我在机器上的 IIS 管理器中查看我的站点时,可以看到 Url Rewrite 组件。
    【解决方案3】:

    您可以通过从 RouteBase 继承来使用自定义路由来做到这一点 - 所以在您的情况下,它看起来像这样

    public class HomeRoute : RouteBase
    {
        public override RouteData GetRouteData(HttpContextBase httpContext)
        {
            if (httpContext.Request.Url.ToString().ToLower().Contains("home.aspx"))
            {
                httpContext.Response.Status = "301 Moved Permanently"; //Optional 301 redirect
                httpContext.Response.RedirectLocation = "Home";
                httpContext.Response.End();
            }
    
            return null;
        }
    
        public override VirtualPathData GetVirtualPath(RequestContext requestContext, RouteValueDictionary values)
        {
            return null;
        }
    }
    

    然后在您注册路线时,您将拥有

    routes.Add("HomeUrl", new HomeRoute());
    

    因此,对 /Home.aspx 的任何请求都会自动重定向到 /Home - 显然,通过一些额外的工作,您可以让任何 .aspx 请求更通用。

    【讨论】:

    • 感谢您的回复。我已经测试了您的代码,并且代码似乎从未进入 if 语句。我肯定已经在 Application_Start() 中添加了针对 RouteCollection 注册路由的调用。
    • 有趣..您在所有其他路线之前注册了吗?如果没有,请尝试一下 - 我在生产代码中有类似的工作。
    • 是的,我什至评论了所有其他路线注册,但仍然无法正常工作。该死的。
    猜你喜欢
    • 1970-01-01
    • 2013-09-04
    • 2013-04-18
    • 2012-07-17
    • 2012-09-06
    • 2015-07-07
    • 2011-06-17
    • 1970-01-01
    • 2011-03-03
    相关资源
    最近更新 更多