如何使用正确的mod_rewrite在子目录下部署Django?

时间:2018-12-14 06:03:29

标签: django mod-rewrite cpanel namecheap

在根文件夹下安装Django应用要容易得多,但是我有一个项目,我必须在子文件夹下设置应用,尽管我知道大多数有经验的开发人员都不会这样做。

我有一个托管有NameCheap的网站,http://www.sctongye.com,在根文件夹下安装了一个WordPress博客,现在我正在尝试通过cPanel http://sctongye.com/thoughts/的子文件夹在设置下设置Django应用.py显示如下

STATIC_URL = 'thoughts/static/' # on server
STATICFILES_DIRS = (
    os.path.join(BASE_DIR, "static"),
)

一切似乎都正常,但是当我尝试通过admin登录

http://sctongye.com/thoughts/admin/http://sctongye.com/thoughts/xadmin/(我安装了另一个管理员)

所有静态文件的路径错误,如下所示:

GET http://sctongye.com/thoughts/xadmin/thoughts/static/thoughts/static/xadmin/vendor/bootstrap/css/bootstrap.css 404 (Not Found)
GET http://sctongye.com/thoughts/xadmin/thoughts/static/xadmin/css/themes/bootstrap-xadmin.css 404 (Not Found)
GET http://sctongye.com/thoughts/xadmin/thoughts/static/thoughts/static/xadmin/css/xadmin.main.css 404 (Not Found)

如您所见,路径链接中有大量重复的静态名称。

为了使子文件夹Django应用能够正常工作,我必须在子文件夹下设置一个.htaccess文件

# DO NOT REMOVE. CLOUDLINUX PASSENGER CONFIGURATION BEGIN
PassengerAppRoot "/home/username/thoughts"
PassengerBaseURI "/thoughts"
PassengerPython "/home/username/virtualenv/thoughts/3.6/bin/python3.6"
RewriteCond %{REQUEST_URI} !^/thoughts  (<== I had to add this line to make it work, but I think this should be where the potential problem come from)
# DO NOT REMOVE. CLOUDLINUX PASSENGER CONFIGURATION END

由于我是通过cPanel进行此操作的,因此无法访问所有其他服务器设置文件(例如conf),有人可以帮我弄清楚为什么管理页面的静态文件看起来如此奇怪以及如何纠正它吗?

0 个答案:

没有答案