2014-10-02 155 views
0

我想更改我的网站中的我的网址。我读过一些文章,现在,我知道如何重写URL是这样的:更改地址栏中的网址

用户输入地址栏=> 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>

+0

http://httpd.apache.org/docs/2.2/mod/mod_rewrite.html 您可以使用.htaccess文件重定向指令或类似实现这一目标。 – Cyclonecode 2014-10-02 05:59:23

+0

我的房东是窗户。它工作吗? – mahdi 2014-10-02 06:10:49

+0

你使用哪个网络服务器?如果你正在运行IIS,那么我认为你应该启用'ISAPIRewrite' – Cyclonecode 2014-10-02 06:13:19

回答

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; 
      } 
     } 
    } 
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

+0

重写不是我的问题。我想在自动输入地址栏后更改地址栏中的网址。例如:我写:www.example.com/presentation/default.aspx然后在地址栏网址中加载页面www.example.com – mahdi 2014-10-02 07:19:05

+0

@mahdi - 是的,“R”标志将为您做到这一点。 – Cyclonecode 2014-10-02 07:45:24

+0

它不工作,我想要的。 – mahdi 2014-10-02 09:05:12

0

您可以使用IIS - URL重写。

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

这是一个梦幻般的指导: https://www.youtube.com/watch?v=hkEFPzixiVE