如何从Symfony2 URL中删除"web/app_dev.php"?

phi*_*opp 19 php .htaccess symfony

我刚刚创建了我的第一个Symfony2项目.但URL中的"/web/app_dev.php"部分让我很烦.应该可以在没有虚拟主机的情况下执行此操作...

但是当我通过.htaccess尝试这个时,我总是得到路由错误,并且"web /"总是被添加到url ...

编辑:整个项目也在一个名为"symfonyTest"的子目录中.演示页面的URL是"http://localhost/symfonyTest/web/app_dev.php/demo/",它应该变为"http:// localhost/symfonyTest/demo /".链接也应具有此语法.这可能吗?

Sha*_*uck 21

Symfony2带有一个内置的调试模式,这是你在使用app_dev.php添加访问url时所使用的模式.调试模式缓存显着少于生产模式,可以通过将浏览器定向到URL来访问,但省略app_dev.php.如果访问此URL不起作用,则可能意味着您需要清除生产缓存.

要清除缓存,请将终端(命令控制台)指向您Symfony项目的根目录.从那里输入以下命令:

php app/console cache:clear --env=prod --no-debug
Run Code Online (Sandbox Code Playgroud)

根据以下Symfony 3中的评论,这已经发生了变化:

php bin/console cache:clear --env=prod --no-debug
Run Code Online (Sandbox Code Playgroud)

这应该清除您的制作缓存,并允许您在没有app_dev.php的情况下访问网址.

至于网址的网页部分.删除它的最简单方法是将项目的Web根目录设置为Web文件夹.最好使用apache使用虚拟主机执行此操作,但有一些方法可以使用htaccess执行此操作.

此链接说明如何使用htaccess文件更改webroot. http://kb.siteground.com/how_to_change_my_document_root_folder_using_an_htaccess_file/

希望这可以帮助!


小智 11

<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/(.*) /$1
    </Directory>
</VirtualHost>
Run Code Online (Sandbox Code Playgroud)

这是我的httpd.conf的一部分


Fre*_*alo 10

我以前也有这个问题,请看一下我的配置然后试一试

在我的.htaccess里面:

大家好,我也遇到过这个问题.似乎"股票".htaccess是问题所在.我已经在我的环境中解决了这个问题,这是我的.htaccess注释:

<IfModule mod_rewrite.c>
    RewriteEngine On
    RewriteCond %{REQUEST_URI}::$1 ^(/.+)/(.*)::\2$
    RewriteRule ^(.*) - [E=BASE:%1]
    RewriteCond %{ENV:REDIRECT_STATUS} ^$
    RewriteRule ^app_dev.php(/(.*)|$) %{ENV:BASE}/$2 [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>
Run Code Online (Sandbox Code Playgroud)

在我的/ 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>
Run Code Online (Sandbox Code Playgroud)


Sgo*_*kes 5

如果您使用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>
Run Code Online (Sandbox Code Playgroud)

重新启动Apache后,您可以通过http://myurl.local访问项目(不要忘记在C:\Windows\system32\drivers\etc\hosts!下配置您的hosts文件).如果您想让dev环境(app_dev.php)不显示在url中,请将DirectoryIndex更改为app_dev.php.