在子文件夹中的Symfony4路由 [英] Symfony4 routing inside a subfolder
问题描述
我的 / var / www / html / app /
中有一个symfony应用,我想通过 host / app访问它/
但我无法使其正常工作。
我有一个与此$ b关联的文件
$ b<目录/ var / www / html / app>
AllowOverride全部
订单允许,Deny
全部
< IfModule mod_rewrite.c>
选项-MultiViews
RewriteEngine On
#RewriteCond%{REQUEST_FILENAME}!-f
#RewriteCond%{REQUEST_FILENAME}!-d
#RewriteRule ^(。*)$ public / $ 1 [L]
< / IfModule>
< / Directory>
被评论是因为我尝试在此处进行重定向,但没有成功,所以我做了以下内容.htaccess。
此htaccess位于 /var/www/html/app/.htaccess
< IfModule mod_rewrite.c>
选项-MultiViews
RewriteEngine On
RewriteRule ^(。*)$ public / $ 1 [L]
< / IfModule>
有了这个.htaccess,当我进入:// host / app
我去了symfony应用程序(不是apache目录结构),这似乎是一个不错的开始。
但问题是我收到异常未找到/ app /
的路由。
这很正常,因为 / app /
是服务器中的子文件夹,而不是symfony路由的开始。
如何在 / app /
文件夹路径之后创建symfony路由?
我知道有有关子文件夹中symfony2 / 3路由的一些问题,但答案需要分隔 src
和 public
文件,这不是我需要的文件
我只希望我的路线在访问:// host / app
时有效。 / p>
有关信息,当我访问:// host / app / public / * Symfony路线*
时,效果很好/ p>
编辑:
新 app.conf
Alias / rapp / var / www / html / app / public
<目录/ var / www / html / app>
AllowOverride无
订单允许,拒绝
全部允许
< IfModule mod_rewrite.c>
选项-MultiViews
RewriteEngine在
上#RewriteBase / app
RewriteCond%{REQUEST_FILENAME}!-f
RewriteCond%{REQUEST_FILENAME} !-d
RewriteRule ^(?! public / index.php)$ public / index.php $ 1 [L]
< / IfModule>
< / Directory>
我设法解决了这个问题!!!
虽然我不确定配置是否正确,但似乎可以正常工作。
这是我的工作 app.conf
文件(在 /etc/apache2/sties-enabled/app.conf
中)
别名/ app / var / www / html / app / public
<目录/ var / www / html / app>
AllowOverride无
订单允许,拒绝
全部允许
< IfModule mod_rewrite.c>
选项-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
部分,重写就可以了,我们什么都不做就停止了重写,并以<$ c结尾$ c> [L] 标志。
我不知道为什么必须删除 public /
中的最终 RewriteRule
( RewriteRule ^(。*)$ index.php / $ 1 [L]
如果有人有改进此配置的想法,请随时发表评论。
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>
It's commented because I tried to do the redirect here but it didn't worked so I made the following .htaccess.
And this htaccess in /var/www/html/app/.htaccess
<IfModule mod_rewrite.c>
Options -MultiViews
RewriteEngine On
RewriteRule ^(.*)$ public/$1 [L]
</IfModule>
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.
How can I make the symfony routes beginning after the /app/
folder path ?
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.
I just want my routes working when accessing ://host/app
.
For information, It works great when I access ://host/app/public/*Symfony routes*
Edit:
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.
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>
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.
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屋!