【问题标题】:How to redirect all php requests to a different subdomain如何将所有 php 请求重定向到不同的子域
【发布时间】:2012-07-11 13:26:57
【问题描述】:

我有一个用 Rails 重写的应用程序。旧应用程序在 php 中。

我需要将所有 php 请求重定向到子域 old.example.com(我已将旧应用程序放在那里运行),以便旧链接仍然可以工作。我相信所有链接中都有一个“.php”,而我的新网址都没有。所以可以作为重定向的条件。

我已经计划使用 apache RedirectMatch。所以像:

RedirectMatch \.php http://old.example.com

应该很接近。我相信。我只是不知道如何将请求路径传递给重定向规则。

或者如果有人有更好的想法如何做到这一点?

【问题讨论】:

  • 我发现Apache2中有一个REQUEST_URI变量。所以我需要将它添加到重定向 url。这样做的语法可能是什么?
  • 我试过RedirectMatch permanent ^.*\.php.*$ http://old.example.com%{REQUEST_URI},但没有成功。所以我按照 Alex W 的建议使用了重写引擎。

标签: redirect apache2 subdomain


【解决方案1】:

使用 Apache mod rewrite,您将使用以下内容:

RewriteEngine On
RewriteBase /
RewriteRule ^.*\.php.*$ http://old.example.com/index.php [R=301,NC]

如果您想在重定向 URL 中实际插入他们尝试访问的页面:

RewriteEngine On
RewriteBase /
RewriteRule ^/?(.*)\.php.*$ http://old.example.com/$1.php [R=301,NC]

$1 将在括号中插入任何与正则表达式匹配的内容。

对于 GET 参数:

RewriteEngine On
RewriteBase /
RewriteRule ^/?(.*)\.php(.*)$ http://old.example.com/$1.php$2 [R=301,NC]

【讨论】:

  • 如果url中还有一些GET参数呢?我认为它们不会包含在这些解决方案中。但可以包含在 .php 之后的第二个 (.*) 中?
  • @Mika 为 GET 参数更新了它。你是对的,它们可以包含在第二对括号中。
  • in webmasterworld.com/forum92/4307.htm 我读到 RewriteBase 应该只在 .htaccess 文件中定义。我将把它放在服务器配置文件中的<VirtualHost> 标记中。我应该排除 RewriteBase 吗?
  • @Mika 可以排除那个指令。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2014-04-24
  • 2011-01-07
  • 2015-12-29
  • 2015-03-21
  • 2010-10-10
  • 2017-06-29
  • 1970-01-01
相关资源
最近更新 更多