OpenShift .htaccess RewriteRule doesn't work

2019-08-10 13:20发布

I have a multiuser wordpress install which is currently using subdomains. I'd like to migrate it to use subfolder multiuser. I've set up subfolder multi-user and it actually works. Now, I'd like to redirect from the old address to the new address. For instance:

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

Both domains above are properly added as aliases in OpenShift. To cause the redirect, I created a rewrite rule in the .htaccess file. It looks like this:

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

This results in a redirect loop. Inspecting this, a simple request on peter.example.com produces:

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>

The return value is correct, as is the URL in the body; so I know my RewriteRule is being evaluated. However, the URL in the Location header is incorrect. Why? It seems to me as if OpenShift is mangling this header causing the redirect to fail.

Is this a bug in OpenShift? In my RewriteRule? Does anyone have a suggested workaround?

2条回答
倾城 Initia
2楼-- · 2019-08-10 13:47

I've encountered the same issue, and were able to prove that OpenShift rewrites the location header. Here's what I did and found:

  1. Set up an app at test-app.rhcloud.com
  2. Assigned a custom domain test.com
  3. Created a very simple PHP page that issued the following HTTP header

    Location: http://www.example.com/
    
  4. When I open that page, it sends the following header:

    Location: http://test-app.rhcloud.com/
    
  5. If I change the redirect to any other domain (e.g. www.example2.com), it works (doesn't rewrite redirect).

I haven't yet found an explanation, but others seem to be having problems with this too. I don't know of a proper workaround either, but you could try using HTML redirects (with meta tags). Google has limited support for this kind of redirection.

UPDATE: I've found a solution. Simply append the default port number (80) after the URL, and OpenShift won't rewrite your URL. Because, the port number is the default, the browsers will remove it, and your users won't even see anything.

This is how your updated rewrite rule would look like:

RewriteCond %{HTTP_HOST} ^((?!www)[^.]+)\.example\.com$ [NC]
RewriteRule ^(.*)$ http://example.com:80/%1/$1 [R=301,L]
查看更多
老娘就宠你
3楼-- · 2019-08-10 13:47

The port:80 addition fixed the issue. It was impossible for me to redirect correctly, and OpenShift was of no help. And as others said, it is not documented anywhere.

查看更多
登录 后发表回答