Symfony .htaccess to redirect in public/index.php

ki1q1bka  于 2023-08-06  发布在  PHP
关注(0)|答案(3)|浏览(94)

您好,我在部署期间在Symfony项目的根目录下的.htaccess有问题。
目前工作的Symfony 6项目,我有一个问题,显示资产,所以现在我已经修复它。但另一个问题是,当我将自己重定向到我的域名https://www.example.com时,URL会自动重定向到:

https://www.example.com/public

字符串
所以我需要帮助,我不明白的.htaccess文件从项目的根,这将重定向.htaccess这是在公共创建的Apache。
下面是我的.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>


谢谢,如果有人有答案,我会很感激。

rkue9o1l

rkue9o1l1#

您的文档根目录不正确。在配置Web服务器文档中:

<VirtualHost *:80>
    # ...
    DocumentRoot /var/www/project/public

    <Directory /var/www/project/public>
        AllowOverride None

        # Copy .htaccess contents here
    </Directory>
</VirtualHost>

字符串

v09wglhw

v09wglhw2#

根据提供的.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>

lstz6jyr

lstz6jyr3#

您也可以尝试在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(Moved Permanently)。
确保路由配置文件、DefaultController名称空间和文件路径与Symfony项目中使用的约定相匹配

相关问题