首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >更改地址栏中的URL

更改地址栏中的URL
EN

Stack Overflow用户
提问于 2014-10-02 05:54:01
回答 3查看 2.5K关注 0票数 0

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

用户输入地址栏=> www.example.com/Q1

加载页面为=> www.example.com/dir1 1/cat.aspx?id=q1

但我想要这个:

用户输入地址栏=> www.example.com/dir1 1/cat.aspx?id=q1

浏览器在地址栏中显示=> www.example.com/othername/Q1

有什么办法吗?

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

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

EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2014-10-06 11:32:52

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

您可以用global.asax重写:

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

Stack Overflow用户

发布于 2014-10-02 06:15:49

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

代码语言:javascript
运行
复制
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文件,上面的内容应该如下所示:

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

参考资料

重写

将.htaccess内容转换为IIS web.config

票数 0
EN

Stack Overflow用户

发布于 2016-01-12 16:49:18

您可以使用IIS重写。

这是您需要安装的另一个模块。一旦您安装了它,您可以在仪表板上找到它。从这里开始,添加一个规则将传入的URL更改为所需的URL。

这里有一个很棒的指南:https://www.youtube.com/watch?v=hkEFPzixiVE

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

https://stackoverflow.com/questions/26155130

复制
相关文章

相似问题

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