htaccess redirect然后重写一个url使其漂亮

时间:2013-12-03 15:27:30

标签: regex apache .htaccess mod-rewrite redirect

我目前有网址,因为这是实际找到文件的地方:

www.example.com/bla/annoying/long.php

我使用301将一个更好的网址重定向到上面的网址。

redirect 301 /niceee http://www.example.com/bla/annoying/long.php

所以现在当人们转到www.example.com/niceee时,它会重定向到正确的页面...但问题是,一旦重定向发生,它最终会再次成为烦人的长网址。

所以他们转到www.example.com/niceee并最终到达http://www.example.com/bla/annoying/long.php

如何将其重定向并将网址重写为 / niceee

  1. 所以他们转到www.example.com/niceee
  2. 显示http://www.example.com/bla/annoying/long.php的内容
  3. 但网址仍为www.example.com/niceee

2 个答案:

答案 0 :(得分:1)

将此代码放在DOCUMENT_ROOT/.htaccess文件中:

RewriteEngine On

RewriteRule ^niceee/? /bla/annoying/long.php [L,NC]

遵守这条规则:

  1. 用户将访问www.example.com/niceee
  2. 将显示/bla/annoying/long.php的内容
  3. 浏览器中的
  4. 网址仍为www.example.com/niceee

答案 1 :(得分:0)

您必须使用相对URL才能重定向, 像这样:

RewriteEngine on
RewriteRule /niceee /bla/annoying/long

这种方式/ niceee留在地址栏中,但内容显示在/ bla / annoying / long