【问题标题】:Redirect to a different file from sub folder从子文件夹重定向到不同的文件
【发布时间】:2013-03-23 14:02:18
【问题描述】:

当用户请求http://www.hostname.com/first/api/getlink 时,我必须重定向到http://www.hostname.com/first/api.php?type=link

所以我尝试了以下

RewriteEngine on

RewriteCond %{HTTP_HOST} ^www\.hostname\.com\/first$

RewriteBase /

RewriteRule ^api/getlink$ \/api.php?type=link [L]

.htaccess 文件中并放在first/ 文件夹中。我无法获得正确的重定向。但我收到404 error。有人可以指导我吗?提前致谢。

【问题讨论】:

  • 动态字符串有哪些?
  • 更新了!你现在可以检查一下吗?
  • RewriteRule 必须在 RewriteCond 旁边。而且您的主机名正则表达式是错误的,因为它包含路径

标签: php .htaccess url-rewriting url-redirection


【解决方案1】:

您可以在/first 目录中的一个 .htaccess 文件中尝试此操作:

Options +FollowSymLinks -MultiViews
RewriteEngine On
RewriteBase /first
RewriteCond %{REQUEST_URI}   !api\.php               [NC]
RewriteRule  ^api/getlink  /first/api.php?type=link  [NC,L]

对于永久重定向,将 [NC,L] 替换为 [R=301,NC,L]

【讨论】:

  • 你的意思是根目录? hostname.com/ 或 hostname.com/first ?
  • 我们还有其他可能的方法来尝试在 /first 文件夹中的 .htaccess 吗?
  • 您希望 .htaccess 文件位于根目录 (hostname.com/) 还是位于第一个目录?
  • 好的。我会修改我的答案。
  • 基本上表示替换URL显示在浏览器的地址栏中并且是永久的。也许你想要那个。如果是这样,只需按照我的答案替换标志即可。
猜你喜欢
  • 2021-10-21
  • 2017-07-04
  • 1970-01-01
  • 1970-01-01
  • 2021-09-08
  • 2021-07-09
  • 2011-12-20
  • 1970-01-01
  • 2017-12-12
相关资源
最近更新 更多