的.htaccess似乎忽略OpenShifthtaccess、OpenShift

2023-09-02 00:52:46 作者:月亮是我磨弯滴ㄨ

我工作的一个网站上OpenShift(Django的,Python的2.7,Apache的),我想它总是通过HTTPS提供。我想这将是使用一个简单的任务的.htaccess 和OpenShift的知识基础的似乎表明它应该被中。不过,我已经折腾在这几天,我无法弄清楚如何使它发挥作用。基本上,我不能让我的的.htaccess 文件到任何东西,但静态文件进行操作。我根本无法获得非静态资源的请求重定向到的什么的。

I'm working on a site on OpenShift (Django, Python 2.7, Apache), and I'd like it to always be served over HTTPS. I figured this would be a simple task using .htaccess, and OpenShift's knowledge base seems to show that it should be. However, I've been agonizing over this for days, and I can't figure out how to make it work. Basically, I can't get my .htaccess files to operate on anything but static files. I simply can't get requests for non-static resources to redirect to anything.

下面是我的目录结构,内侧〜/应用程序根/回购。它使用了新的(2014年3月左右?)OpenShift的目录结构。

Here's my directory structure, inside ~/app-root/repo. It's using the "new" (March 2014 or so?) OpenShift directory structure.

/MyProject
    settings.py
    urls.py
    [etc.]
/[app directories]
/static
/wsgi
    wsgi.py
    static -> ../static
manage.py
requirements.txt

好了,这么多的挫折之后,我一直在测试以最小的可行的.htaccess 来只是看看我是否能得到任何工作。它看起来是这样的:

Ok, so after much frustration, I've been testing with a minimum viable .htaccess to just see if I can get anything to work. It looks like this:

RewriteEngine on
RewriteRule .* http://www.google.com

一切都应该去谷歌。如果我把这个在 WSGI 目录中,所有的静态文件重定向到google.com,但没有人这样做。如果我把它放在〜/应用程序根/回购,什么都没有发生。这就像该文件不存在。

Everything should go to Google. If I put this in the wsgi directory, all my static files get redirected to google.com, but nothing else does. If I put it in ~/app-root/repo, nothing at all happens. It's like the file doesn't exist.

同样,如果我使用由OpenShift提出的命令,并在文件中 WSGI ,我的静态文件重定向到使用HTTPS,但没有人这样做。

Similarly, if I use the commands suggested by OpenShift and put the file in wsgi, my static files get redirected to use HTTPS, but nothing else does.

我也试着像加 os.environ ['HTTPS'] =关于 wsgi.py 所建议的网站像这样的。这一直是无效的,我真的不能说我有见地有足够的了解这些各种各样的设置知道我需要添加,但最好我想通过的.htaccess 。该请求应该使用SSL它击中的WSGI之前。

I've also tried WSGI and Django things like adding os.environ['HTTPS'] = "on" to wsgi.py as suggested by sites like this one. That's been ineffective, and I really can't say I'm knowledgable enough about those sorts of settings to know what I'd need to add, but ideally I'd like to do this through .htaccess. The request should be using SSL before it hits WSGI.

我需要做什么怎么做才能让我的的.htaccess 文件上的所有的请求的工作?为什么是OpenShift忽略的.htaccess 文件不在 WSGI

What do I need to do to make my .htaccess files work on all requests? And why is OpenShift ignoring .htaccess files not in wsgi?

推荐答案

我打这个很短的时间,并迅速决定用Django的SSL重定向中间件组件。

I fought with this for a short time and quickly decided to use the django-ssl-redirect middleware package.

的全部原因,我在OpenShift是为了避免与这些类型的配置问题的战斗,让我有点失望,我不得不求助于此。

The whole reason I'm on OpenShift is to avoid fighting with these kinds of configuration issue, so I'm a little disappointed I had to resort to this.

不过,这里有云:

只需添加到您的MIDDLEWARE_CLASSES

Just add this to your MIDDLEWARE_CLASSES

'ssl_redirect.middleware.SSLRedirectMiddleware',

添加这requirements.txt

Add this to requirements.txt

django-ssl-redirect==1.0

添加像这样的设置(我把这个在我的基础设置,我同意在多种环境中的文件,因此​​如果)。

Add something like this in settings (I put this in my base settings file that I share in multiple environments, hence the "if").

if os.environ.has_key('OPENSHIFT_REPO_DIR'):
    SSL_ON = True
    SSL_ALWAYS = True

这里更多信息: https://bitbucket.org/nextscreenlabs/django-ssl-redirect