开发者

.htaccess - 301 redirect to a new url then silently redirect back to hide an ugly url

开发者 https://www.devze.com 2023-04-12 09:19 出处:网络
I just changed url schemes, so example.com/ugly is now example.com/nice example.com/ugly still handles the request - .htaccess has this r开发者_开发知识库ule:

I just changed url schemes, so example.com/ugly is now example.com/nice

example.com/ugly still handles the request - .htaccess has this r开发者_开发知识库ule:

RewriteRule ^nice$ example.com/ugly [PT,L,QSA]

This much works.

Now I'd like to redirect example.com/ugly to example.com/nice, but still have example.com/ugly handle the request. Something like this:

  1. Redirect example.com/ugly to example.com/nice (301)
  2. Internally redirect example.com/nice to example.com/ugly, without letting the user know, and without repeating step 1

Simple ways of doing this lead to looping of course. Is there a way to do this with .htaccess? If not, is there another way?


The best way I know of is using PHP in the target file. Here's approximately what you'd want to add:

if($_SERVER['REQUEST_URI'] == '/index.php') header("Location: http://".$_SERVER['HTTP_HOST']."/");

Obviously you need to add some other variables in there to handle whatever requests, but that's the basic idea.

0

精彩评论

暂无评论...
验证码 换一张
取 消