简体   繁体   English

适用于旧应用程序的重写规则的Apache重写规则,用于在子目录中运行Symfony应用程序

[英]Apache rewrite rules for running a Symfony application in a sub-directory when rewrite rules already exist for legacy application

Version Information: Apache v2.4.18 on Ubuntu, PHP version 7.1,Symfony v3.2 版本信息:Ubuntu上的Apache v2.4.18,PHP版本7.1,Symfony v3.2

I've been trying to get this working for a couple of days now and keep hitting problems, I have an existing PHP application using a custom built framework with the following Apache VirtualHost configuration: 我一直在尝试使它工作两天,并不断解决问题,我有一个现有的PHP应用程序,该应用程序使用具有以下Apache VirtualHost配置的自定义构建框架:

<VirtualHost *:80>
    ServerName dvlp.mydomain.com
    DocumentRoot /var/www/my-site/com

    RewriteEngine   On

    # Do nothing for the Home page ('^/$'), specific directories, static files.
    RewriteRule ^/(?:$|shared|asset)|\.(?:php|ico|txt|xml) - [L]

    # Search engine friendly request URIs (most, but not all, without a query string).
    RewriteRule ^/([a-z0-9-]+/?)$                 /index.php?param1=$1 [L,QSA]
    RewriteRule ^/([a-z0-9-]+)/([a-z0-9-\.]+/?)$  /index.php?param1=$1&param2=$2 [L,QSA]
    RewriteRule ^/([a-z0-9-]+)/([a-z0-9-]+)/(.+)$ /index.php?param1=$1&param2=$2&param3=$3 [QSA]

    <Directory />
            Options FollowSymLinks
            AllowOverride None
            ErrorDocument 404 /404.php
    </Directory>
</VirtualHost>

What I am trying to do is install a Symfony application to work within a sub-directory. 我想做的是安装一个Symfony应用程序以在子目录中工作。 I've created my Symfony application in /var/www/symfony-app and created a symbolic link /var/www/my-site/com/my-symfony which points to /var/www/symfony-app/web . 我已经在/ var / www / symfony-app中创建了我的Symfony应用程序,并创建了一个指向/ var / www / symfony-app / web的符号链接/ var / www / my-site / com / my-symfony

I then tried the following on line 6 of my VirtualHost file: 然后,我在VirtualHost文件的第6行尝试了以下操作:

RewriteRule ^/(?:$|my-symfony|shared|asset)|\.(?:php|ico|txt|xml) - [L]

Visiting dvlp.mydomain.com/my-symfony in the browser takes me to the directory listings for the /var/www/symfony-app/web folder. 在浏览器中访问dvlp.mydomain.com/my-symfony会将我带到/ var / www / symfony-app / web文件夹的目录列表。 I tried adding an index.php file to /var/www/symfony-app/web which simply includes app_dev.php and it loaded the home page, but II tried visiting dvlp.mydomain.com/my-symfony/edit-personal-details I get the 404 error page from my existing application. 我尝试将index.php文件添加到/ var / www / symfony-app / web中 ,其中仅包含app_dev.php并加载了主页,但是我尝试访问dvlp.mydomain.com/my-symfony/edit-personal-详细信息,我从现有应用程序中获取404错误页面。

I realised now that I need separate rewrite rules for this sub-directory, for Symfony this is usually written as the following: 我现在意识到,我需要为该子目录使用单独的重写规则,对于Symfony,通常将其编写如下:

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ app.php [QSA,L]

So I undid the change I made to line 6 of the VirtualHost file and added a new line as follows: 因此,我撤消了对VirtualHost文件的第6行所做的更改,并添加了新行,如下所示:

RewriteRule ^/my-symfony(.*)$ /my-symfony/index.php [L,QSA]

This results in a 404 error: 这将导致404错误:

No route found for "GET /my-symfony/" (from "http://dvlp.mydomain.com/")

And if I try visiting dvlp.mydomain.com/my-symfony/edit-personal-details I get the same 404 error: 如果我尝试访问dvlp.mydomain.com/my-symfony/edit-personal-details,则会收到相同的404错误:

No route found for "GET /my-symfony/edit-personal-details" (from "http://dvlp.mydomain.com/")

The last thing I have tried is adding a prefix to my Symfony routing.yml : 我尝试的最后一件事是在我的Symfony routing.yml中添加一个前缀:

my_symfony:
    resource: "@MySymfonyBundle/Controller/"
    type:     annotation
    prefix:   /my-symfony

This then loads the pages correctly but with no images or stylesheets because they are trying to load from the DocumentRoot (eg dvlp.mydomain.com/css instead of dvlp.mydomain.com/my-symfony/css). 然后,这将正确加载页面,但没有图像或样式表,因为它们正尝试从DocumentRoot加载(例如dvlp.mydomain.com/css而不是dvlp.mydomain.com/my-symfony/css)。

I would appreciate any input as to where I am going wrong. 对于我要去哪里出错,我将不胜感激。 Thanks. 谢谢。

I finally managed to get this working as intended by adding a "Location" tag containing specific RewriteRules and a RewriteBase for the Symbolic link. 我终于设法通过添加包含特定RewriteRules和用于Symbolic链接的RewriteBase的“ Location”标签来按预期工作。 For information, final VirtualHost config below: 有关信息,请在下面进行最终的VirtualHost配置:

<VirtualHost *:80>
    ServerName dvlp.my-domain.com
    DocumentRoot /var/www/my-site/com

    RewriteEngine On

    <Location /my-symfony>
        RewriteBase /my-symfony
        RewriteCond %{REQUEST_FILENAME} !-f
        RewriteRule ^(.*)$ app_dev.php [QSA,L]
    </Location>

    # Do nothing for the Home page ('^/$'), specific directories, static files.
    RewriteRule ^/(?:$|my-symfony|shared|asset)|\.(?:php|ico|txt|xml) - [L]

    # Search engine friendly request URIs (most, but not all, without a query string).
    RewriteRule ^/([a-z0-9-]+/?)$                 /index.php?param1=$1 [L,QSA]
    RewriteRule ^/([a-z0-9-]+)/([a-z0-9-\.]+/?)$  /index.php?param1=$1&param2=$2 [L,QSA]
    RewriteRule ^/([a-z0-9-]+)/([a-z0-9-]+)/(.+)$ /index.php?param1=$1&param2=$2&param3=$3 [QSA]

    <Directory />
        Options FollowSymLinks
        AllowOverride None
        ErrorDocument 404 /404.php
    </Directory>
</VirtualHost>

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

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