如何从 Symfony2 URL 中删除“web/app_dev.php”?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/9669458/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to remove "web/app_dev.php" from Symfony2 URLs?
提问by phil-opp
I just created my first Symfony2 project. But the "/web/app_dev.php" part in the URL annoys me. It should be possible to do this withoutVirtual hosts...
我刚刚创建了我的第一个 Symfony2 项目。但是 URL 中的“/web/app_dev.php”部分让我很恼火。没有虚拟主机应该可以做到这一点......
But when I try this through .htaccess I always get routing errors and the "web/" is always added to the url...
但是当我通过 .htaccess 尝试这个时,我总是遇到路由错误,并且“web/”总是被添加到 url 中...
EDIT: The whole project is also in a subdirectory named "symfonyTest". The Url to the demo page is "http://localhost/symfonyTest/web/app_dev.php/demo/" and it should become "http://localhost/symfonyTest/demo/". Links should also have this syntax. Is this possible?
编辑:整个项目也在一个名为“symfonyTest”的子目录中。演示页面的 URL 是“http://localhost/symfonyTest/web/app_dev.php/demo/”,它应该变成“http://localhost/symfonyTest/demo/”。链接也应该有这种语法。这可能吗?
回答by Shattuck
Symfony2 comes with a built in debug mode, which is what you are using when you access url's with the app_dev.php addition. The debug mode caches significantly less than the production mode which can be accessed by directing your browser to the url, but leaving out the app_dev.php. If accessing this url doesn't work then it probably means that you need to clear your production cache.
Symfony2 带有一个内置的调试模式,当你通过 app_dev.php 添加访问 url 时,你正在使用这种模式。调试模式的缓存明显少于生产模式,生产模式可以通过将浏览器定向到 url 来访问,但不包括 app_dev.php。如果访问此 url 不起作用,则可能意味着您需要清除生产缓存。
To clear the cache direct you terminal (command console) to the root of you Symfony project. From there type the following command:
要清除缓存,请将您的终端(命令控制台)定向到 Symfony 项目的根目录。从那里输入以下命令:
php app/console cache:clear --env=prod --no-debug
Per comments below in Symfony 3 this has moved:
根据下面 Symfony 3 中的评论,这已经发生了变化:
php bin/console cache:clear --env=prod --no-debug
This should clear your productions cache and allow you to access urls without the app_dev.php.
这应该会清除您的作品缓存,并允许您在没有 app_dev.php 的情况下访问 url。
As for the web part of the url. The easiest way to remove that is to set the web root of your project to the web folder. It's best to do this with apache using virtual hosts, but there are ways to do it with the htaccess.
至于网址的网页部分。删除它的最简单方法是将项目的 web 根目录设置为 web 文件夹。最好通过使用虚拟主机的 apache 来做到这一点,但也有办法使用 htaccess 做到这一点。
This link explains how to use the htaccess file to change the webroot. http://kb.siteground.com/how_to_change_my_document_root_folder_using_an_htaccess_file/
此链接解释了如何使用 htaccess 文件更改 webroot。 http://kb.siteground.com/how_to_change_my_document_root_folder_using_an_htaccess_file/
Hope this helps!
希望这可以帮助!
回答by usrmrz
<VirtualHost *:80>
DocumentRoot "path_to_symfonyTest/web"
ServerName "symfonyTest"
<Directory "path_to_symfonyTest/web">
DirectoryIndex app_dev.php
AllowOverride All
Order allow,deny
Allow from all
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ app_dev.php [QSA,L]
RedirectMatch permanent ^/app_dev\.php/(.*) /
</Directory>
</VirtualHost>
That's part of my httpd.conf
那是我的 httpd.conf 的一部分
回答by Frederick G. Sandalo
I used to have this problem too, please take a look at my configurations and try them out
我以前也有这个问题,请看一下我的配置试试看
inside my .htaccess:
在我的 .htaccess 中:
Hi all, I have been having this problem too. And it seems that the "stock" .htaccess is the problem. I have solved this in my environment and here is my .htaccess with notes:
大家好,我也遇到了这个问题。似乎“股票”.htaccess 是问题所在。我已经在我的环境中解决了这个问题,这是我的 .htaccess 文件:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REQUEST_URI}:: ^(/.+)/(.*)::$
RewriteRule ^(.*) - [E=BASE:%1]
RewriteCond %{ENV:REDIRECT_STATUS} ^$
RewriteRule ^app_dev.php(/(.*)|$) %{ENV:BASE}/ [R=301,L] ##### this is the part that you should tweak, have the .htaccess point the request to app_dev.php, since the routing.yml is empty initially
RewriteCond %{REQUEST_FILENAME} -f
RewriteRule .? - [L]
RewriteRule .? %{ENV:BASE}/app_dev.php [L] ##### this is the part that you should tweak, have the .htaccess point the request to app_dev.php, since the routing.yml is empty initially
</IfModule>
<IfModule !mod_rewrite.c>
<IfModule mod_alias.c>
# When mod_rewrite is not available, we instruct a temporary redirect of
# the startpage to the front controller explicitly so that the website
# and the generated links can still be used.
RedirectMatch 302 ^/$ /app.php/
# RedirectTemp cannot be used instead
</IfModule>
</IfModule>
inside my "symfony" entry in /etc/apache2/sites-available:
在 /etc/apache2/sites-available 中的“symfony”条目中:
<VirtualHost 127.0.1.15>
ServerAdmin webmaster@localhost
ServerName symfony
DocumentRoot /var/www/Symfony/web
DirectoryIndex app.php
<Directory /var/www/Symfony/web>
Options Indexes FollowSymLinks MultiViews
AllowOverride All
Order allow,deny
allow from all
</Directory>
ErrorLog ${APACHE_LOG_DIR}/error.log
# Possible values include: debug, info, notice, warn, error, crit,
# alert, emerg.
LogLevel warn
CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>
回答by Sgoettschkes
If you use an apache virtual host, you can get it working the way you desire. Here is an example virtual host from my xampp:
如果您使用 apache 虚拟主机,则可以按照您希望的方式工作。这是我的 xampp 中的示例虚拟主机:
<VirtualHost *:80>
ServerName myurl.local
# Basic stuff
DocumentRoot "C:/path/to/symfony/web"
DirectoryIndex app.php
<Directory "C:/path/to/symfony/web">
AllowOverride All
Allow from All
</Directory>
</VirtualHost>
After restarting your apache, you can access the project through http://myurl.local(don't forget to configure your hosts file under C:\Windows\system32\drivers\etc\hosts
!). If you want to have the dev environment (app_dev.php) not showing up in the url, change the DirectoryIndex to app_dev.php.
重启你的apache后,就可以通过http://myurl.local访问项目了(别忘了配置下你的hosts文件C:\Windows\system32\drivers\etc\hosts
!)。如果您希望开发环境 (app_dev.php) 不显示在 url 中,请将 DirectoryIndex 更改为 app_dev.php。
回答by Jzapata
//enable mod rewrite
sudo a2enmod rewriteChange all occurrences of AllowOverride Noneby AllowOverride All
File: (/etc/apache2/apache2.conf) or (/etc/apache2/sites-available/000-default.conf) - for me work with /etc/apache2/apache2.conf file.
//启用mod重写
sudo a2enmod rewrite通过AllowOverride AllFile: ( /etc/apache2/apache2.conf) 或 ( /etc/apache2/sites-available/000-default.conf)更改所有出现的AllowOverride None- 对我来说使用 /etc/apache2/apache2。配置文件。
Example:
例子:
<Directory /var/www/web/>
# enable the .htaccess rewrites
Options Indexes FollowSymLinks
#AllowOverride None #commented
AllowOverride All
Require all granted
</Directory>
- //Restart apache service:
sudo service apache2 restart
- //重启apache服务:
sudo service apache2 restart
回答by Jovan Perovic
You would need .htaccess
file in parent folder of web
, however, I wouldn't suggest doing so. That folder, apart from web
, contains complete source, binaries, vendors and much more therefore you would need to apply many rules just to allow web-access to web
folder and disallow web-access to everything else.
您需要.htaccess
的父文件夹中的文件web
,但是,我不建议这样做。该文件夹除 之外还web
包含完整的源代码、二进制文件、供应商等等,因此您需要应用许多规则来允许对web
文件夹进行 Web 访问并禁止对其他所有内容进行 Web 访问。
So, it's doable, but I would definitely go for vhost solution...
所以,这是可行的,但我肯定会选择 vhost 解决方案......
回答by user2948817
you just need to copy the content of .htaccess
file to the your-website.conf
你只需要将文件的内容复制.htaccess
到your-website.conf
inside the TAG Directory
在 TAG 目录中