如何解决忽略Symfony路由,改为提供文件夹
我有一个Symfony 4项目,没有什么特别之处,但是在public/blog
内有一个Wordpress项目,当我转到“ mydomain / blog”时,我想提供该文件夹,而不是由Symfony的路由系统处理“。
我的.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 index.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. "/index" should not resolve
# to the front controller "/index.php" but be rewritten to "/index.php/index".
<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 index.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}::$0 ^(/.+)/(.*)::\2$
RewriteRule .* - [E=BASE:%1]
# Sets the HTTP_AUTHORIZATION header removed by Apache
RewriteCond %{HTTP:Authorization} .+
RewriteRule ^ - [E=HTTP_AUTHORIZATION:%0]
# Redirect to URI without front controller to prevent duplicate content
# (with and without `/index.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 ^index\.php(?:/(.*)|$) %{ENV:BASE}/$1 [R=301,L]
# If the requested filename exists,simply serve it.
# We only want to let Apache serve files and not directories.
# Rewrite all other queries to the front controller.
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ %{ENV:BASE}/index.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 307 ^/$ /index.php/
# RedirectTemp cannot be used instead
</IfModule>
</IfModule>
# BEGIN cPanel-generated php ini directives,do not edit
# Manual editing of this file may result in unexpected behavior.
# To make changes to this file,use the cPanel MultiPHP INI Editor (Home >> Software >> MultiPHP INI Editor)
# For more information,read our documentation (https://go.cpanel.net/EA4ModifyINI)
<IfModule php7_module>
php_flag display_errors Off
php_value max_execution_time 30
php_value max_input_time 60
php_value max_input_vars 1000
php_value memory_limit 128M
php_value post_max_size 15M
php_value session.gc_maxlifetime 1440
php_value session.save_path "/var/cpanel/php/sessions/ea-php73"
php_value upload_max_filesize 10M
php_flag zlib.output_compression Off
</IfModule>
<IfModule lsapi_module>
php_flag display_errors Off
php_value max_execution_time 30
php_value max_input_time 60
php_value max_input_vars 1000
php_value memory_limit 128M
php_value post_max_size 15M
php_value session.gc_maxlifetime 1440
php_value session.save_path "/var/cpanel/php/sessions/ea-php73"
php_value upload_max_filesize 10M
php_flag zlib.output_compression Off
</IfModule>
# END cPanel-generated php ini directives,do not edit
# php -- BEGIN cPanel-generated handler,do not edit
# Set the “ea-php73” package as the default “PHP” programming language.
<IfModule mime_module>
AddHandler application/x-httpd-ea-php73 .php .php7 .phtml
</IfModule>
# php -- END cPanel-generated handler,do not edit
我不了解如何正确配置此文件,但我读了一些有关放置该文件以提供文件夹的信息(如果它存在但仍无法正常工作),它仍然被Symfony的路由捕获,因此我收到404错误
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
解决方法
这似乎有点无法解决,但是...如果正确配置WordPress(在/blog
子目录中),则实际上不需要做任何特别的事情。
.htaccess
子目录中应该已经有一个附加的/blog
文件,其中包含mod_rewrite指令,用于处理WordPress的URL路由。
例如,/blog/.htaccess
文件应包含以下内容:
# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . index.php [L]
</IfModule>
# END WordPress
如果是这种情况,那么对/blog/<anything>
的任何请求都将被路由到WordPress。根(Symfony).htaccess
文件中的mod_rewrite伪指令被完全覆盖。 (除非在服务器配置中启用了mod_rewrite继承;但这似乎不太可能。)
但是,/blog/.htaccess
文件可能不正确,可能会将请求路由回Symfony。例如,如果其编写方式如下( default .htaccess
文件),则对/blog/<anything>
的任何请求都将被重写为文档根目录中的/index.php
(即Symfony):
# !!! THIS IS INCORRECT !!!
# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
# END WordPress
您需要修改它,使其看起来像第一个.htaccess
文件。或类似以下内容(更多是“ WordPress方式”,但本质上是相同的事情),例如:
# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /blog
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /blog/index.php [L]
</IfModule>
# END WordPress
以上内容与第一个.htaccess
文件相同,除了(不必要地)对子目录进行了硬编码。如果您让WordPress修改.htaccess
文件,那么我相信它会像这样更新它。
您还需要确保正确配置了WordPress WP_SITEURL
(站点URL)和WP_HOME
(主页URL)设置(在仪表板或wp-config.php
文件中)以进行投放/blog
子目录中的文件,并且/blog
出现在URL中。
但是请注意,根.htaccess
文件中的其他非mod_rewrite指令仍将继承自WordPress安装。尽管查看您的指令,这可能是一件好事。除了...如果您的系统上实际上未启用mod_rewrite,但是我认为您已经在使用“漂亮” URL?在这种情况下必须如此。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。