首页 > 解决方案 > 在 DocumentRoot 的子文件夹中安装 Symfony 4

问题描述

我希望这个问题不是重复的,但我只为旧版本的 Symfony 找到了有关此的文章或问题。我是 Symfony 4 的新手,我想用它创建一个新应用程序并将其安装为现有网站的一部分(domain如下)。Sf 4 应用程序进入admin/下面层次结构中显示的子目录。domain使用 TYPO3 CMS,但我不确定这对这个问题是否重要。

首先,这是目录结构:

/home/webuser/domain/
    public/ (this is the document root for "domain.localhost")
        admin/ (the Symfony app goes here)
            public/
            …
        …
        typo3/
        .htaccess
        index.php
        …
        composer.json

我目前正在本地计算机上的网站上工作,我想从domain.localhost/admin. 我正在使用 Apache 虚拟主机,该domain/public文件夹是以下文件的根目录domain

<VirtualHost *:80>
    ServerName domain.localhost
    DocumentRoot "/home/webuser/www/domain/public"
</VirtualHost>

我尝试将以下重写规则添加到我的 VirtualHost :

<VirtualHost *:80>
    ServerName domain.localhost
    DocumentRoot "/home/webuser/www/domain/public"
    RewriteEngine On
    RewriteRule "^/admin/(.*)$" "/admin/public/$1"
    LogLevel trace1
</VirtualHost>

但这似乎只有在我访问时才有效http://domain.localhost/admin/:然后我看到“欢迎使用 Symfony 4.2.7”页面。一旦我尝试访问子页面,就会收到 404 错误。我尝试按照此处lucky/number文档中的说明创建我的第一页,并查看 Apache 错误日志,我有如下几行:

[Mon Apr 29 18:52:09.472043 2019] [rewrite:trace1] [pid 22250:tid 140404453660416] mod_rewrite.c(483): [client ::1:43084] ::1 - - [domain.localhost/sid#55a33c704bf8][rid#7fb260002bd0/initial] [perdir /home/webuser/www/domain/public/] pass through /home/webuser/www/domain/public/admin/public/lucky

[Mon Apr 29 18:52:09.472087 2019] [core:info] [pid 22250:tid 140404453660416] [client ::1:43084] AH00128: File does not exist: /home/webuser/www/domain/public/admin/public/lucky/number

现在,TYPO3 有一个.htaccess包含 URL 重写的文件,但我认为它们不会干扰。下面,我只是admin/在规则中以“停止重写处理”开头的注释后添加了自己的文件夹,这样我们就不会在domain找不到文件时重定向到网站的主页:

<IfModule mod_rewrite.c>

    # Enable URL rewriting
    RewriteEngine On

    # Store the current location in an environment variable CWD to use
    # mod_rewrite in .htaccess files without knowing the RewriteBase
    RewriteCond $0#%{REQUEST_URI} ([^#]*)#(.*)\1$
    RewriteRule ^.*$ - [E=CWD:%2]

    # Rule for versioned static files, configured through:
    # - $GLOBALS['TYPO3_CONF_VARS']['BE']['versionNumberInFilename']
    # - $GLOBALS['TYPO3_CONF_VARS']['FE']['versionNumberInFilename']
    # IMPORTANT: This rule has to be the very first RewriteCond in order to work!
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule ^(.+)\.(\d+)\.(php|js|css|png|jpg|gif|gzip)$ %{ENV:CWD}$1.$3 [L]

    # Access block for folders
    RewriteRule _(?:recycler|temp)_/ - [F]
    RewriteRule fileadmin/templates/.*\.(?:txt|ts)$ - [F]
    RewriteRule ^(?:vendor|typo3_src|typo3temp/var) - [F]
    RewriteRule (?:typo3conf/ext|typo3/sysext|typo3/ext)/[^/]+/(?:Configuration|Resources/Private|Tests?|Documentation|docs?)/ - [F]

    # Block access to all hidden files and directories with the exception of
    # the visible content from within the `/.well-known/` hidden directory (RFC 5785).
    RewriteCond %{REQUEST_URI} "!(^|/)\.well-known/([^./]+./?)+$" [NC]
    RewriteCond %{SCRIPT_FILENAME} -d [OR]
    RewriteCond %{SCRIPT_FILENAME} -f
    RewriteRule (?:^|/)\. - [F]

    # Stop rewrite processing, if we are in the typo3/ directory or any other known directory
    # NOTE: Add your additional local storages here
    RewriteRule ^(?:typo3/|fileadmin/|typo3conf/|typo3temp/|uploads/|favicon\.ico|admin/) - [L]

    # If the file/symlink/directory does not exist => Redirect to index.php.
    # For httpd.conf, you need to prefix each '%{REQUEST_FILENAME}' with '%{DOCUMENT_ROOT}'.
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_FILENAME} !-l
    RewriteRule ^.*$ %{ENV:CWD}index.php [QSA,L]

</IfModule>

生产服务器将是一个专用系统,所以我可以修改任何我想要的东西。

标签: apacheurl-rewritingurl-routingsymfony4

解决方案


事实证明,我只需要安装 Apache 包,就像在这个答案中一样


推荐阅读