简体   繁体   English

如何配置Apache以使Symfony4路由在WordPress域的子目录中工作

[英]How to config Apache to get Symfony4 routes working in subdirectory on a WordPress domain

On my Apache server I have a WordPress installation working on www.domain.com and I want to add a Symfony4 installation on www.domain.com/symfony. 在我的Apache服务器上,我在www.domain.com上有一个WordPress安装,我想在www.domain.com/symfony上添加一个Symfony4安装。 Symfony routes are taken to the WordPress 404 page (but still are the correct URL ie www.domain.com/symfony/about). Symfony路由被带到WordPress 404页面(但仍然是正确的URL,即www.domain.com/symfony/about)。 However, if there are NO routes defined in my Symfony app, then the base www.domain.com/symfony/ URL will correctly display the Symfony4 getting started page. 但是,如果我的Symfony应用程序中未定义任何路由,则基本www.domain.com/symfony/ URL将正确显示Symfony4入门页面。 What is wrong in my Apache conf files? 我的Apache conf文件有什么问题?

Web server is Apache 2.4, I have tried using Alias and AliasMatch directives in the /etc/apache2 conf files. Web服务器是Apache 2.4,我尝试在/ etc / apache2 conf文件中使用Alias和AliasMatch指令。 Alias displays WordPress 404 for all Symfony routes and AliasMatch appends many /index.php's onto the URL and displays the same 404. I tried to copy what phpmyadmin does with its Alias /phpmyadmin /usr/share/phpmyadmin Alias为所有Symfony路由显示WordPress 404,AliasMatch将许多/index.php附加到URL上并显示相同的404。我试图复制phpmyadmin对其Alias /phpmyadmin /usr/share/phpmyadmin所做的操作

# /etc/apache2/sites-available/wordpress.conf
<Directory /var/www/wordpress>
    Options Indexes FollowSymLinks
    AllowOverride All
    Require all granted
</Directory>
<VirtualHost *:80>
    ServerName domain.com
    ServerAlias www.domain.com
    ServerAdmin me@myemail.com
    DocumentRoot /var/www/wordpress
</VirtualHost>


# /var/www/wordpress/.htaccess
<IfModule mod_rewrite.c>
    RewriteEngine On
    RewriteBase /
    RewriteRule ^index\.php$ - [L]
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule . /index.php [L]
</IfModule>


# /etc/apache2/sites-available/symfony.conf
Alias /symfony /var/www/symfony/app/public
<Directory /var/www/symfony/app/public>
    Options FollowSymLinks
    AllowOverride All
    Require all granted
    Allow from All
    FallbackResource /index.php
</Directory>


# /var/www/symfony/app/config/routes.yaml
about_index:
  path: /about
  controller: App\Controller\AboutController::index

I expected www.domain.com/symfony/about to go to /var/www/symfony/app/public/index.php and route to App\\Controller\\AboutController::index , but it looks like WordPress is trying to find the page /symfony/about and 404's. 我期望www.domain.com/symfony/about可以转到/var/www/symfony/app/public/index.php并路由到App\\Controller\\AboutController::index ,但是看起来WordPress正在尝试查找页/symfony/about和404。

When I change my Alias /symfony /var/www/symfony/app/public to AliasMatch ^/symfony.* /var/www/symfony/app/public it changes the Symfony URL to www.domain.com/symfony/index.php/index.php/index.php/index.php/index.php/index.php but has the same result (WordPress 404 page). 当我将Alias /symfony /var/www/symfony/app/public更改为AliasMatch ^/symfony.* /var/www/symfony/app/public它将Symfony URL更改为www.domain.com/symfony/index.php/index.php/index.php/index.php/index.php/index.php但结果相同(WordPress 404页面)。 None of my changes have impacted the WordPress part at all - it still functions perfectly. 我所做的任何更改都没有对WordPress部分造成任何影响-它仍然可以完美运行。

EDIT 1 编辑1

Tried a new conf to test if it's my symfony config - it works as expected so I just think it must be something in my Apache confs. 尝试了一个新的conf来测试它是否是我的symfony配置-它可以按预期工作,所以我只是认为它一定是我的Apache conf中的内容。 This config treats the symfony app as the main site and wordpress is totally disabled: 此配置将symfony应用视为主要站点,并且wordpress完全禁用:

# /etc/apache2/sites-available/symfony_only.conf
<Directory /var/www/symfony/app/public>
    Options FollowSymLinks
    AllowOverride All
    Require all granted
    Allow from All

    FallbackResource /index.php
</Directory>

<Directory /var/www/symfony/app/public/bundles>
    FallbackResource disabled
</Directory>

<VirtualHost *:80>
    ServerName domain.com
    ServerAlias www.domain.com
    ServerAdmin me@myemail.com
    DocumentRoot /var/www/symfony/app/public
</VirtualHost>

Looks like I got a working conf now. 看来我现在有了一个有效的会议。 Not sure if it's even resulting in any different configuration than the one I had. 不知道它是否会导致与我的配置有所不同。 I also did a lot of php bin/console cache:clear as I was even getting 404 and 500 errors on the little debug bar at the bottom of the symfony welcome page (not displayed on the bar, but the bar actually wouldn't properly load). 我也做了很多php bin/console cache:clear因为我什至在symfony欢迎页面底部的小调试栏上都收到404和500错误(该栏上未显示,但该栏实际上无法正常显示)加载)。 The below file is my entire conf which I combined into one file. 下面的文件是我的整个配置文件,我将其合并为一个文件。 It serves from my Symfony4 app if the url path begins with /symfony , and the routes all work. 如果url路径以/symfony开头,则可以从我的Symfony4应用程序中获取服务,并且所有路由都可以使用。

# /etc/apache2/sites-available/wordpress_symfony.conf
Alias /symfony /var/www/symfony/app/public

<Directory /var/www/symfony/app/public>
    AllowOverride All
    Require all granted
    Allow from All
    FallbackResource /index.php
</Directory>

<Directory /var/www/symfony/app/public/bundles>
    FallbackResource disabled
</Directory>

<Directory /var/www/wordpress>
    Options Indexes FollowSymLinks
    AllowOverride All
    Require all granted
</Directory>

<VirtualHost *:80>
    ServerName domain.com
    ServerAlias www.domain.com
    ServerAdmin me@myemail.com
    DocumentRoot /var/www/wordpress

    ErrorLog ${APACHE_LOG_DIR}/error.log
    CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>

Also I added the symfony/apache-pack with composer which added this .htaccess in the symfony public directory. 我还添加了带作曲家的symfony / apache-pack,并将此.htaccess添加到symfony公共目录中。 The docs said I can get improved performance by moving all these rules into my .conf file and disabling overrides: 文档说,通过将所有这些规则移到我的.conf文件中并禁用替代项,可以提高性能:

# /var/www/symfony/app/public/.htaccess
DirectoryIndex index.php

<IfModule mod_negotiation.c>
    Options -MultiViews
</IfModule>

<IfModule mod_rewrite.c>
    RewriteEngine On
    RewriteCond %{REQUEST_URI}::$1 ^(/.+)/(.*)::\2$
    RewriteRule ^(.*) - [E=BASE:%1]
    RewriteCond %{HTTP:Authorization} .
    RewriteRule ^ - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]
    RewriteCond %{ENV:REDIRECT_STATUS} ^$
    RewriteRule ^index\.php(?:/(.*)|$) %{ENV:BASE}/$1 [R=301,L]
    RewriteCond %{REQUEST_FILENAME} -f
    RewriteRule ^ - [L]
    RewriteRule ^ %{ENV:BASE}/index.php [L]
</IfModule>

<IfModule !mod_rewrite.c>
    <IfModule mod_alias.c>
        RedirectMatch 307 ^/$ /index.php/
    </IfModule>
</IfModule>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM