子文件夹内的 Symfony4 路由 [英] Symfony4 routing inside a subfolder

查看:21
本文介绍了子文件夹内的 Symfony4 路由的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的 /var/www/html/app/ 中有一个 symfony 应用程序,我想通过 host/app/ 访问它,但我不能让它发挥作用.

I have a symfony app in my /var/www/html/app/ and I'd like to access it via host/app/ but I can't make it work.

I have a conf file with this

<Directory /var/www/html/app>

    AllowOverride All
    Order Allow,Deny

    Allow from All

    <IfModule mod_rewrite.c>

        Options -MultiViews
        RewriteEngine On

#       RewriteCond %{REQUEST_FILENAME} !-f
#       RewriteCond %{REQUEST_FILENAME} !-d

#       RewriteRule ^(.*)$ public/$1 [L]

    </IfModule>
</Directory>

它被评论是因为我试图在此处进行重定向,但没有成功,所以我做了以下 .htaccess.

It's commented because I tried to do the redirect here but it didn't worked so I made the following .htaccess.

而这个htaccess在/var/www/html/app/.htaccess

And this htaccess in /var/www/html/app/.htaccess

<IfModule mod_rewrite.c>
    Options -MultiViews
    RewriteEngine On

    RewriteRule ^(.*)$ public/$1 [L]
</IfModule>

有了这个 .htaccess,当我转到 ://host/app 时,我会转到 symfony 应用程序(不是 apache 目录结构),这似乎是一个好的开始.但问题是我收到一个异常No Routes found for/app/.这是正常的,因为 /app/ 是我服务器中的子文件夹,而不是 symfony 路由的开始.

With this .htaccess, When I go to ://host/app I go to the symfony app (not the apache directory structure) and that seems to be a good start. But the problem is I'm getting an Exception No Routes found for /app/. That's normal because /app/ is the subfolder in my server, not the start of symfony routes.

如何使 symfony 路由在 /app/ 文件夹路径之后开始?

How can I make the symfony routes beginning after the /app/ folder path ?

我知道在子文件夹中有一些关于 symfony2/3 路由的问题,但答案需要将 srcpublic 文件分开,这不是我想要的.

I know there is some questions about symfony2/3 routing in subfolder but the answer requires to separate src and public files, that's not what I'd like to have.

我只希望我的路由在访问 ://host/app 时工作.

I just want my routes working when accessing ://host/app.

有关信息,当我访问 ://host/app/public/*Symfony routes*

新建 app.conf别名/rapp "/var/www/html/app/public"

New app.conf Alias /rapp "/var/www/html/app/public"

<Directory /var/www/html/app>

    AllowOverride None
    Order Allow,Deny

    Allow from All

    <IfModule mod_rewrite.c>

        Options -MultiViews
        RewriteEngine On
        #RewriteBase /app

        RewriteCond %{REQUEST_FILENAME} !-f
        RewriteCond %{REQUEST_FILENAME} !-d

        RewriteRule ^(?!public/index.php)$ public/index.php$1 [L]

    </IfModule>
</Directory>

推荐答案

我设法解决了问题!!!

I managed to resolve the problem !!!

我不确定配置是否正确",但它似乎有效.

I'm not sure the config is "proper" though but it seems to work.

这是我的工作 app.conf 文件(在 /etc/apache2/sties-enabled/app.conf 中)

Here is my working app.conf file (in /etc/apache2/sties-enabled/app.conf)

Alias /app "/var/www/html/app/public"

<Directory /var/www/html/app>

    AllowOverride None
    Order Allow,Deny

    Allow from All

    <IfModule mod_rewrite.c>

        Options -MultiViews
        RewriteEngine On

        RewriteRule (/public) - [L]

        RewriteCond %{REQUEST_FILENAME} !-f
        RewriteCond %{REQUEST_FILENAME} !-d

        RewriteRule ^(.*)$ index.php/$1 [L]

    </IfModule>
</Directory>

第一个 RewriteRule 是在我将像 example.com/app/page-1 这样的 url 重写为 example.com/app 时防止内部重写循环/public/index.php/page-1.由于 page-1 不是目录或文件,它会无限循环.所以我假设只要我有 /public 部分,重写就起作用了,我们通过什么都不做来停止重写,并用 [L] 标志结束重写.

The first RewriteRule is to prevent from internal Rewrite loop when I rewrite url like example.com/app/page-1 to example.com/app/public/index.php/page-1. As page-1 is not a directory or a file it will loop infinitely. So I assume as soon as I have a /public part, the rewrite worked and we stop the rewrite by doing nothing and end the rewrite with the [L] flag.

我不明白为什么我必须删除最终"RewriteRule 中的 public/ 部分(RewriteRule ^(.*)$ index.php/$1 [L]) 不过.

I'm not suire why I had to remove the public/ part in the "final" RewriteRule (RewriteRule ^(.*)$ index.php/$1 [L]) though.

如果有人有改进此配置的想法,请随时发表评论.

If someone have ideas to improve this config, feel free to leave comment.

这篇关于子文件夹内的 Symfony4 路由的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆