简体   繁体   English

Symfony2将localhost / subdir / web / app.php / action重写为localhost / subdir / action

[英]Symfony2 rewrite localhost/subdir/web/app.php/action to localhost/subdir/action

I'm try to use my Symphony 2 project without the /web slug in URL. 我尝试在URL中没有/ web slug的情况下使用我的Symphony 2项目。 I've added the following to the .htaccess file in my root folder (localhost/subdir). 我已将以下内容添加到我的根文件夹(localhost / subdir)中的.htaccess文件中。

<IfModule mod_rewrite.c>
    Options +FollowSymlinks
    RewriteEngine On

    # Explicitly disable rewriting for front controllers
    RewriteRule ^web/app_dev.php - [L]
    RewriteRule ^web/app.php - [L]

    # Fix the bundles folder
    RewriteCond %{HTTP_HOST} ^localhost/subdir/$
    RewriteRule ^bundles/(.*)$ /web/bundles/$1  [QSA,L]

    RewriteCond %{HTTP_HOST} ^localhost/subdir/$
    RewriteCond %{REQUEST_FILENAME} !-f
    # Change below before deploying to production
    #RewriteRule ^(.*)$ /web/app.php [QSA,L]
    RewriteRule ^(.*)$ web/app_dev.php [QSA,L]
</IfModule>

This script loads my page, but It do not get any /web/bundle/mybundle/javascript/ , /images/ , or /css/ files. 这个脚本加载我的页面,但它没有得到任何/web/bundle/mybundle/javascript//images//css/ files。 These are giving me a 404 error. 这些给我一个404错误。 Because in the <head> tag it says /bundle/mybundle/javascript/ without the /web prefix. 因为在<head>标签中它表示/bundle/mybundle/javascript/没有/web前缀。 The same is on web-profiler, debug toolbar etc. web-profiler,调试工具栏等也是如此。

How can I fix my .htaccess to make this work? 如何修复我的.htaccess才能使其正常工作?

the web/ folder is ment to be the root web directory so you have a misconfiguration, tell your apache to have web as document root. web /文件夹是根网页目录,因此您的配置错误,告诉您的apache将web作为文档根目录。 in httpd.conf sth. 在httpd.conf中...... like: 喜欢:

<VirtualHost *:80>
  ServerName name.localhost.com
  DocumentRoot "/Projects/name/web"
  <Directory "/Projects/name/web">
    Options Indexes FollowSymLinks
    AllowOverride All
    Allow from All
  </Directory>
</VirtualHost>

otherwise for example your parameters.yml with database pass and so on could be accessible. 否则,例如你的parameters.yml与数据库传递等可以访问。

the web/.htaccess conf for rewrite the app.php/app_dev.php in url could look ike : 用于重写url中的app.php / app_dev.php的web / .htaccess conf可能看起来像ike:

# Use the front controller as index file. It serves as a fallback solution when
# every other rewrite/redirect fails (e.g. in an aliased environment without
# mod_rewrite). Additionally, this reduces the matching process for the
# start page (path "/") because otherwise Apache will apply the rewriting rules
# to each configured DirectoryIndex file (e.g. index.php, index.html, index.pl).
DirectoryIndex app.php




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

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

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