OpenShift .htaccess RewriteRule不起作用

1 .htaccess mod-rewrite openshift

我有一个多用户wordpress安装,目前正在使用子域.我想将其迁移到使用子文件夹多用户.我已经设置了子文件夹多用户,它实际上工作.现在,我想从旧地址重定向到新地址.例如:

peter.example.com - > example.com/peter

上述两个域都在OpenShift中作为别名正确添加.为了引起重定向,我在.htaccess文件中创建了一个重写规则.它看起来像这样:

RewriteCond %{HTTP_HOST} ^((?!www)[^.]+)\.example\.com$ [NC]
RewriteRule ^(.*)$ http://example.com/%1/$1 [R=301,L]
Run Code Online (Sandbox Code Playgroud)

这导致重定向循环.检查这一点,peter.example.com上的一个简单请求产生:

HTTP/1.1 301 Moved Permanently
Date: Mon, 05 Jan 2015 03:30:58 GMT
Server: Apache/2.2.15 (Red Hat)
Location: http://peter.example.com/peter/
Content-Length: 335
Content-Type: text/html; charset=iso-8859-1
Accept-Ranges: none

<!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 2.0//EN">
<html><head>
<title>301 Moved Permanently</title>
</head><body>
<h1>Moved Permanently</h1>
<p>The document has moved <a href="http://example.com/peter/">here</a>.</p>
<hr>
<address>Apache/2.2.15 (Red Hat) Server at peter.example.com Port 80</address>
</body></html>
Run Code Online (Sandbox Code Playgroud)

返回值是正确的,正文中的URL也是如此; 所以我知道我的RewriteRule正在评估中.但是,Location标头中的URL不正确.为什么?在我看来,好像OpenShift正在破坏此标头导致重定向失败.

这是OpenShift中的错误吗?在我的RewriteRule中?有没有人有建议的解决方法?

小智 7

我遇到了同样的问题,并且能够证明OpenShift重写了位置标题.这是我做的和发现的:

  1. test-app.rhcloud.com上设置应用程序
  2. 分配了自定义域test.com
  3. 创建了一个非常简单的PHP页面,它发出了以下HTTP标头

    Location: http://www.example.com/
    
    Run Code Online (Sandbox Code Playgroud)
  4. 当我打开该页面时,它会发送以下标题:

    Location: http://test-app.rhcloud.com/
    
    Run Code Online (Sandbox Code Playgroud)
  5. 如果我将重定向更改为任何其他域(例如www.example2.com),它可以工作(不重写重定向).

我还没有找到解释,但其他人似乎也遇到了问题.我也不知道正确的解决方法,但您可以尝试使用HTML重定向(使用元标记).谷歌已经对这种重定向的支持有限.

更新:我找到了解决方案.只需在URL后附加默认端口号(80),OpenShift就不会重写您的URL.因为端口号是默认值,浏览器会将其删除,而您的用户甚至都看不到任何内容.

这是您更新的重写规则的样子:

RewriteCond %{HTTP_HOST} ^((?!www)[^.]+)\.example\.com$ [NC]
RewriteRule ^(.*)$ http://example.com:80/%1/$1 [R=301,L]
Run Code Online (Sandbox Code Playgroud)