更改地址栏中的URL

时间:2014-10-02 05:54:01

标签: rewrite url-mapping

我想在我的网站上更改我的网址。我读了一些文章,现在,我知道如何重写这样的网址:

用户输入地址栏=> www.example.com/Q1
并且加载的页面是=> www.example.com/dir1/cat.aspx?id=Q1

但我想要这个:

用户输入地址栏=> www.example.com/dir1/cat.aspx?id=Q1
和浏览器显示在地址栏=> www.example.com/othername/Q1

有什么办法吗?


这是我关于重写的webconfig的一部分:

<system.webServer>
    <rewrite>
      <rules>
        <rule name="Rewrite page to aspx" stopProcessing="true">
          <match url="^([a-z0-9/]+)$" ignoreCase="false" />
          <action type="Rewrite" url="{R:1}.aspx" />
        </rule>
      </rules>
      <rule name="Rewrite item ID" stopProcessing="true">
        <match url="^items/([0-9]+)$" ignoreCase="false"/>
        <action type="Rewrite" url="items.aspx?id={R:1}"/>
      </rule>
      <rule name="Redirect to clean URL" stopProcessing="true">
        <match url="^([a-z0-9/]+).aspx$" ignoreCase="true"/>
        <action type="Redirect" url="{R:1}"/>
      </rule>
    </rewrite>

3 个答案:

答案 0 :(得分:1)

我找不到真正的答案。但我找到了一种简单的方法来重写我的网址,而没有任何设置和模块部分解决了我的问题:

您可以在 global.asax

中重写
    void Application_BeginRequest(Object sender, EventArgs e)
    {
        String strCurrentPath;
        String strCustomPath;
        strCurrentPath = Request.Path;
        if ( strCurrentPath.EndsWith("/home/"))
        {
            strCustomPath = strCurrentPath.Replace("/home/", "/presentation/default.aspx");
            Context.RewritePath(strCustomPath);
            return;
        }
        else
        {
            if (strCurrentPath.Contains("/dir1/"))
            {
                strCustomPath = strCurrentPath.Replace("/dir1/", "/othername/cat.aspx?cid=");
                Context.RewritePath(strCustomPath);
                return;
            }
        }
     }

答案 1 :(得分:0)

如果您正在使用apache,可以通过将以下内容添加到.htaccess文件中来完成:

RewriteEngine on
# check if the query string contains an `id` equal to `Q1`
RewriteCond %{QUERY_STRING} id=Q1
# rewrite `dir1/cat.aspx` to `othername/Q1`
RewriteRule dir1/cat.aspx http://www.example.com/othername/Q1? [R=301,L]

对于web.config文件,上面应该类似于:

<rule name="rule 1k" stopProcessing="true">
  <match url="dir1/cat.aspx"  />
  <conditions trackAllCaptures="true">
    <add input="{QUERY_STRING}" pattern="^id=Q1" />
  </conditions>
  <action type="Redirect" appendQueryString="false" url="http://www.example.com/othername/Q1"  />
</rule>

<强>参考

Apache Module mod_rewrite

Translate .htaccess Content to IIS web.config

答案 2 :(得分:0)

您可以使用IIS - URL重写。

这是您需要安装的附加模块。 安装完成后,您可以在仪表板上找到它。 在此处,添加规则以将传入的URL更改为所需的URL。

这是一本很棒的指南: https://www.youtube.com/watch?v=hkEFPzixiVE