您好,我在部署期间 Symfony 项目根目录下的 .htaccess 出现问题。
目前正在开发 Symfony 6 项目,我在显示资源时遇到了问题,所以现在我已经修复了它。 但是当我将自己重定向到我的域名https://www.example.com时,另一个问题出现了,网址会自动重定向到:
https://www.example.com/public
所以我需要帮助,我不理解项目根目录中的 .htaccess 文件,这些文件会重定向由 apache 公开创建的 .htaccess 文件。
这是我位于项目根目录的 .htaccess 文件:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ public/index.php [L,QSA]
</IfModule>
这是我公开的 .htaccess 文件:
# 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>
# This Option needs to be enabled for RewriteRule, otherwise it will show an error like
# 'Options FollowSymLinks or SymLinksIfOwnerMatch is off which implies that RewriteRule directive is forbidden'
Options +FollowSymlinks
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>
谢谢,如果有人有答案,我将非常感激。
您的文档根目录不正确。来自配置 Web 服务器文档:
<VirtualHost *:80>
# ...
DocumentRoot /var/www/project/public
<Directory /var/www/project/public>
AllowOverride None
# Copy .htaccess contents here
</Directory>
</VirtualHost>
根据提供的 .htaccess 文件,您的 Symfony 项目似乎未正确配置为直接从域的根目录为应用程序提供服务。
要解决此问题并从 URL 中删除“/public”部分,您需要进行以下更改:
.htaccess 文件位于项目根目录下
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ public/index.php [L]
</IfModule>
“public”目录下的.htaccess文件:
<IfModule mod_rewrite.c>
RewriteEngine On
# ... Existing rules
# Redirect to URI without front controller to prevent duplicate content
RewriteCond %{REQUEST_URI}::$0 ^(/.+)/(.*)::\2$
RewriteRule .* - [E=BASE:%1]
# ... Existing rules
</IfModule>
您还可以尝试在 Symfony 中编写一条路由来强制从“/public”重定向到主域, 有些喜欢
redirect_public_to_home:
path: /public
controller: App\Controller\DefaultController::redirectPublicToHome
此路由定义指定当向“/public”发出请求时,应由 DefaultController 类中的redirectPublicToHome 操作处理。
如果 DefaultController 类尚不存在,请创建它。在示例中,假设控制器位于 src/Controller/DefaultController.php。将以下代码添加到类中:
namespace App\Controller;
use Symfony\Component\HttpFoundation\RedirectResponse;
use Symfony\Component\Routing\Annotation\Route;
class DefaultController
{
/**
* @Route("/public", name="redirect_public_to_home")
*/
public function redirectPublicToHome(): RedirectResponse
{
return new RedirectResponse('/', RedirectResponse::HTTP_MOVED_PERMANENTLY);
}
}
此代码定义了redirectPublicToHome操作,该操作返回到根URL(“/”)的重定向响应,状态代码为301(永久移动)。
确保路由配置文件和 DefaultController 命名空间和文件路径与 Symfony 项目中使用的约定匹配
# This RewriteRule is used to dynamically discover the RewriteBase path.
# See https://httpd.apache.org/docs/current/mod/mod_rewrite.html#rewriterule
# Here we will compare the stripped per-dir path *relative to the filesystem
# path where the .htaccess file is read from* with the URI of the request.
#
# If a match is found, the prefix path is stored into an ENV var that is later
# used to properly prefix the URI of the front controller index.php.
# This is what makes it possible to host a Symfony application under a subpath,
# such as example.com/subpath
# The convoluted rewrite condition means:
# 1. Match all current URI in the RewriteRule and backreference it using $0
# 2. Strip the request uri the per-dir path and use ir as REQUEST_URI.
# 3. Evaluate the RewriteCond, assuming your DocumentRoot is /var/www/html,
# this .htaccess is in the /var/www/html/public dir and your request URI
# is /public/hello/world:
# * strip per-dir prefix: /var/www/html/public/hello/world -> hello/world
# * applying pattern '.*' to uri 'hello/world'
# * RewriteCond: input='/public/hello/world::hello/world' pattern='^(/.+)/(.*)::\$' => matched
# 4. Execute the RewriteRule:
# * The %1 in the RewriteRule flag E=BASE:%1 refers to the first group captured in the RewriteCond ^(/.+)/(.*)
# * setting env variable 'BASE' to '/public'
RewriteCond %{REQUEST_URI}::$0 ^(/.+)/(.*)::$
RewriteRule .* - [E=BASE:%1]
# Sets the HTTP_AUTHORIZATION header removed by Apache
RewriteCond %{HTTP:Authorization} .+
RewriteRule ^ - [E=HTTP_AUTHORIZATION:%0]
# Removes the /index.php/ part from a URL, if present
RewriteCond %{ENV:REDIRECT_STATUS} =""
RewriteRule ^index\.php(?:/(.*)|$) %{ENV:BASE}/$1 [R=301,L]
# If the requested filename exists, simply serve it.
# Otherwise rewrite all other queries to the front controller.
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ %{ENV:BASE}/index.php [L]