php - Symfony 从路由中删除前三个字符

标签 php apache .htaccess symfony

我有简单的 Symfony 2.8 应用程序。在本地主机上它工作正常。但是当我将它上传到共享主机时,只有“/”路由有效。但是在每条大于等于 4 个字符的路由中,前三个字符都消失了。

例如:

I go to domain.com/123456 and Symfony says No route found for "GET 465"
I go to domain.com/admin and Symfony says No route found for "GET in"
I go to domain.com/blog and Symfony says No route found for "GET g"
...

当我使用 .htaccess 将所有对 Web 目录的直接访问重定向到根目录时,它甚至发生在本地主机上。我有 here 的 .htaccess 解决方案。

/.htaccess

<IfModule mod_rewrite.c>
    RewriteEngine On
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteRule ^(.*)$ web/$1 [QSA,L]
</IfModule>

/web/.htaccess

<IfModule mod_rewrite.c>
    RewriteEngine On
    RewriteCond %{THE_REQUEST} ^(GET|HEAD)\ /web/
        RewriteRule ^(.*)$ /$1 [L,R=301]
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteRule ^(.*)$ app.php [QSA,L]
</IfModule>

路由器:调试输出

 -------------------------- ---------- -------- ------ ----------------------------------- 
  Name                       Method     Scheme   Host   Path                               
 -------------------------- ---------- -------- ------ ----------------------------------- 
  _wdt                       ANY        ANY      ANY    /_wdt/{token}                      
  _profiler_home             ANY        ANY      ANY    /_profiler/                        
  _profiler_search           ANY        ANY      ANY    /_profiler/search                  
  _profiler_search_bar       ANY        ANY      ANY    /_profiler/search_bar              
  _profiler_purge            ANY        ANY      ANY    /_profiler/purge                   
  _profiler_info             ANY        ANY      ANY    /_profiler/info/{about}            
  _profiler_phpinfo          ANY        ANY      ANY    /_profiler/phpinfo                 
  _profiler_search_results   ANY        ANY      ANY    /_profiler/{token}/search/results  
  _profiler                  ANY        ANY      ANY    /_profiler/{token}                 
  _profiler_router           ANY        ANY      ANY    /_profiler/{token}/router          
  _profiler_exception        ANY        ANY      ANY    /_profiler/{token}/exception       
  _profiler_exception_css    ANY        ANY      ANY    /_profiler/{token}/exception.css   
  _twig_error_test           ANY        ANY      ANY    /_error/{code}.{_format}           
  admin-index                ANY        ANY      ANY    /admin                             
  admin-settings             ANY        ANY      ANY    /admin/nastaveni                   
  admin-blog-index           GET        ANY      ANY    /admin/blog/                       
  admin-blog-new             GET|POST   ANY      ANY    /admin/blog/napsat-clanek          
  admin-blog-show            GET        ANY      ANY    /admin/blog/{id}                   
  admin-blog-edit            GET|POST   ANY      ANY    /admin/blog/{id}/upravit           
  admin-blog-delete          DELETE     ANY      ANY    /admin/blog/{id}                   
  blog-detail                ANY        ANY      ANY    /blog/clanek/{id}/{articleSlug}    
  blog-index                 ANY        ANY      ANY    /blog/{tagSlug}/{currentPage}      
  homepage                   ANY        ANY      ANY    /                                  
  about-us                   ANY        ANY      ANY    /o-projektu                        
  travelTimeline             ANY        ANY      ANY    /plan-cesty                        
 -------------------------- ---------- -------- ------ ----------------------------------- 

我有默认的 routing.yml 和 security.yml

你能看出错误吗?


---编辑---

新的/.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
    RewriteRule ^bundles/(.*)$ /web/bundles/$1  [QSA,L]

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

/web/.htaccess(源自 Symfony)

# 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

# By default, Apache does not evaluate symbolic links if you did not enable this
# feature in your server configuration. Uncomment the following line if you
# install assets as symlinks or if you experience problems related to symlinks
# when compiling LESS/Sass/CoffeScript assets.
# Options FollowSymlinks

# Disabling MultiViews prevents unwanted negotiation, e.g. "/app" should not resolve
# to the front controller "/app.php" but be rewritten to "/app.php/app".
<IfModule mod_negotiation.c>
    Options -MultiViews
</IfModule>

<IfModule mod_rewrite.c>
    RewriteEngine On

    # Determine the RewriteBase automatically and set it as environment variable.
    # If you are using Apache aliases to do mass virtual hosting or installed the
    # project in a subdirectory, the base path will be prepended to allow proper
    # resolution of the app.php file and to redirect to the correct URI. It will
    # work in environments without path prefix as well, providing a safe, one-size
    # fits all solution. But as you do not need it in this case, you can comment
    # the following 2 lines to eliminate the overhead.
    RewriteCond %{REQUEST_URI}::$1 ^(/.+)/(.*)::\2$
    RewriteRule ^(.*) - [E=BASE:%1]

    # Sets the HTTP_AUTHORIZATION header removed by apache
    RewriteCond %{HTTP:Authorization} .
    RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]

    # Redirect to URI without front controller to prevent duplicate content
    # (with and without `/app.php`). Only do this redirect on the initial
    # rewrite by Apache and not on subsequent cycles. Otherwise we would get an
    # endless redirect loop (request -> rewrite to front controller ->
    # redirect -> request -> ...).
    # So in case you get a "too many redirects" error or you always get redirected
    # to the start page because your Apache does not expose the REDIRECT_STATUS
    # environment variable, you have 2 choices:
    # - disable this feature by commenting the following 2 lines or
    # - use Apache >= 2.3.9 and replace all L flags by END flags and remove the
    #   following RewriteCond (best solution)
    RewriteCond %{ENV:REDIRECT_STATUS} ^$
    RewriteRule ^app\.php(/(.*)|$) %{ENV:BASE}/$2 [R=301,L]

    # If the requested filename exists, simply serve it.
    # We only want to let Apache serve files and not directories.
    RewriteCond %{REQUEST_FILENAME} -f
    RewriteRule .? - [L]

    # Rewrite all other queries to the front controller.
    RewriteRule .? %{ENV:BASE}/app.php [L]
</IfModule>

<IfModule !mod_rewrite.c>
    <IfModule mod_alias.c>
        # When mod_rewrite is not available, we instruct a temporary redirect of
        # the start page 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>

使用此配置和组件 paragonie/random_compact 版本 1.4,我有时只会收到错误,我可以通过页面刷新来处理它。

最佳答案

我为您提供了另一种选择,使用虚拟主机和 symfony 默认的 .htaccess

<VirtualHost 192.168.0.1:80>
    ServerName mydomain.com

    DocumentRoot /var/www/myproject/web

    <Directory />
            Header set Access-Control-Allow-Origin "*"
            Options FollowSymLinks
            AllowOverride None
    </Directory>

    <Directory /var/www/myproject/web>
             Options Indexes FollowSymLinks MultiViews
             AllowOverride All
             Order allow,deny
             allow from all
    </Directory>

    ErrorLog ${APACHE_LOG_DIR}/error-myproject.log

    # Possible values include: debug, info, notice, warn, error, crit,
    # alert, emerg.
    LogLevel notice

    CustomLog ${APACHE_LOG_DIR}/access-myproject.log combined

</VirtualHost>

还有web/.htaccess文件(有点长,网上没找到)

DirectoryIndex app.php

# By default, Apache does not evaluate symbolic links if you did not enable this
# feature in your server configuration. Uncomment the following line if you
# install assets as symlinks or if you experience problems related to symlinks
# when compiling LESS/Sass/CoffeScript assets.
# Options FollowSymlinks

# Disabling MultiViews prevents unwanted negotiation, e.g. "/app" should not resolve
# to the front controller "/app.php" but be rewritten to "/app.php/app".
<IfModule mod_negotiation.c>
    Options -MultiViews
</IfModule>

<IfModule mod_rewrite.c>
    RewriteEngine On

    # Determine the RewriteBase automatically and set it as environment variable.
    # If you are using Apache aliases to do mass virtual hosting or installed the
    # project in a subdirectory, the base path will be prepended to allow proper
    # resolution of the app.php file and to redirect to the correct URI. It will
    # work in environments without path prefix as well, providing a safe, one-size
    # fits all solution. But as you do not need it in this case, you can comment
    # the following 2 lines to eliminate the overhead.
    RewriteCond %{REQUEST_URI}::$1 ^(/.+)/(.*)::\2$
    RewriteRule ^(.*) - [E=BASE:%1]

    # Sets the HTTP_AUTHORIZATION header removed by apache
    RewriteCond %{HTTP:Authorization} .
    RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]

    # Redirect to URI without front controller to prevent duplicate content
    # (with and without `/app.php`). Only do this redirect on the initial
    # rewrite by Apache and not on subsequent cycles. Otherwise we would get an
    # endless redirect loop (request -> rewrite to front controller ->
    # redirect -> request -> ...).
    # So in case you get a "too many redirects" error or you always get redirected
    # to the start page because your Apache does not expose the REDIRECT_STATUS
    # environment variable, you have 2 choices:
    # - disable this feature by commenting the following 2 lines or
    # - use Apache >= 2.3.9 and replace all L flags by END flags and remove the
    #   following RewriteCond (best solution)
    RewriteCond %{ENV:REDIRECT_STATUS} ^$
    RewriteRule ^app\.php(/(.*)|$) %{ENV:BASE}/$2 [R=301,L]

    # If the requested filename exists, simply serve it.
    # We only want to let Apache serve files and not directories.
    RewriteCond %{REQUEST_FILENAME} -f
    RewriteRule .? - [L]

    # Rewrite all other queries to the front controller.
    RewriteRule .? %{ENV:BASE}/app.php [L]
</IfModule>

<IfModule !mod_rewrite.c>
    <IfModule mod_alias.c>
        # When mod_rewrite is not available, we instruct a temporary redirect of
        # the start page 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>

关于php - Symfony 从路由中删除前三个字符,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38111472/

相关文章:

javascript 在 php foreach 中操作 html 元素

PHP:使用未定义的常量 ENT_HTML5 - 假定为 'ENT_HTML5'

php - 如何编写与 laravel 相同的 DD() 函数?

apache - 使用 ajp 协议(protocol)连接到备用备用主机别名

apache http 重定向到 https 多个重写条件

apache - 如何使用 apache 在 OVH 互通服务器上禁用 X-Powered-By?

.htaccess - mod-rewrite 重定向但阻止直接访问

php - SQL插入到重复键

regex - 在 htaccess RewriteRule 中排除特定子域或无子域

.htaccess - 谁能想到子请求重写何时有用?