【问题标题】:Mojolicious base URL when running Mojolicious Lite app on Apache under subdirectory在子目录下的 Apache 上运行 Mojolicious Lite 应用程序时的 Mojolicious 基本 URL
【发布时间】:2017-07-23 09:06:05
【问题描述】:

我正在尝试在 Apache(共享主机)上的子目录中运行一个小型 Mojolicious Lite 应用程序:http://www.example.com/mymojoapp/。而且我主要尝试关注this guide

mymojoapp是服务器上的实际目录,app结构如下:

mymojoapp
|- .htaccess
|- app.pl
|- public
|  +- images
|  |  +- ... (image files) ...
|  +- css
|  |  +- ... (css files) ...
|  +- js
|     +- ... (js files) ...
+- templates
   |- layouts
   |  |- index-layout.html.ep
   |  +- other-page-layout.html.ep
   |- index.html.ep
   +- other-page.html.ep

.htaccess内容:

AddHandler cgi-script .pl
Options +ExecCGI

IndexIgnore *

RewriteEngine on

RewriteCond %{DOCUMENT_ROOT}/public/%{REQUEST_URI} -f
RewriteRule ^(.*)$ public/$1 [L]

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-l
RewriteRule ^(.*)$ app.pl/$1 [L]

app.pl 内容(perlmlperl + 用户安装模块的路径,包括 Mojolicious):

#!/usr/bin/perlml

use Mojolicious::Lite;

hook before_dispatch => sub {
    my $c = shift;
    $c->req->url->base(Mojo::URL->new(q{http://www.example.com/mymojoapp/}));
};

get '/' => 'index';

get '/u/:username' => sub {
    my $self = shift;
    $self->stash(profile => $self->stash('username'));
} => 'user-profile-page';

app->secrets(['app','secrets']);
app->start;

index-layout.html.epother-page-layout.html.ep 中,我将css(和图像)称为<link rel="stylesheet" href="<%= url_for %>/css/styles.css">。当我访问基本 URL (http://www.example.com/mymojoapp/) 时,页面以正确的样式显示,因为 url_for 给了我 /mymojoapp/ 使样式表的路径 = /mymojoapp/css/normalize.css。但是当我访问http://www.example.com/mymojoapp/u/someuser时,CSS路径是/mymojoapp/u/someuser/css/normalize.css并且没有应用样式。

我觉得我缺少一些重写规则,或者它可能可以在 before_dispatch 钩子中修复,但到目前为止我无法弄清楚。

如何确保在我的应用中生成的所有页面都获得正确的基本 URL?

【问题讨论】:

  • 您应该自己发布答案并命名来源。不要将答案编辑到您的问题中。这不是论坛。

标签: perl mojolicious mojolicious-lite


【解决方案1】:

在 IRC 的 #mojo 上收到了来自 @jabberwok 的答案:

我需要使用<%= url_for('/css/styles.css') %>,而不是<%= url_for %>/css/styles.css

【讨论】:

    猜你喜欢
    • 2015-10-06
    • 2017-08-27
    • 2012-10-06
    • 1970-01-01
    • 2018-05-14
    • 1970-01-01
    • 1970-01-01
    • 2014-05-21
    • 1970-01-01
    相关资源
    最近更新 更多