2

I've seen this asked before and people provide answers, but none of them seem to work. I'm trying to rewrite the URL so any request to /whatever show as /. I don't want the location of the files to change, just the URL. For example, if someone types in:

Http://www.mysite.com/whatever ( the files are located at /docroot/whatever )
I want the URL to show http://www.mysite.com/

In my htaccess ( placed in /docroot , not /docroot/whatever ) I'm currently using:

RewriteEngine on
RewriteRule   ^whatever/(.+)$   /$1 [L]

What's happening is the URL is staying , but the server is looking for the files /docroot .

That's the opposite of what I need, I need the URL to be , with the server looking for the files in /docroot/whatever .

4

2 回答 2

1

我认为你有它倒退。

  • 如果您希望将根 URI 映射到 DOCROOT/whatever,那么您需要在 Apache 中进行内部重定向。
  • 如果您收到外部请求http://www.mysite.com/whatever并希望客户端使用根 URI,那么您需要执行外部 (301) 重定向。
  • 您还需要确保不会陷入无限循环。

以下几行将为您执行此操作:

RewriteEngine On
RewriteBase   /

RewriteCond   %{ENV:REDIRECT_END}  !^1 
RewriteRule   ^whatever/(.+)$  $1               [R=301,L]

RewriteCond   %{REQUEST_URI}   !^/whatever
RewriteRule   ^(.+)$           whatever/$1      [E=END:1,L]

在第二条规则中,将E=END:1环境变量 END 设置为 1,并且在内部重定向之后,它变成了 REDIRECT_END。第一个条件停止,例如 /fred -> /whatever/fred -> (301) /fred - 这将导致浏览器出错。

也从 302s 开始,当你有规则工作时切换到 301s - 因为客户端浏览器将缓存 301s,这使得调试这些规则成为一个错误。

于 2012-08-15T17:12:00.970 回答
0

尝试将其放入文档根目录的 htaccess 文件中:

RewriteEngine On

# change the browser's URL address bar
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /whatever($|\ )
RewriteRule ^whatever / [L,R=301]

# internally rewrite / back to whatever
RewriteRule ^$ /whatever [L]
于 2012-08-15T17:34:35.130 回答