简体   繁体   中英

Symfony remove "index.php" from twig generated path

I have just set up a new Symfony 5 application with a basic Twig template, routing using annotations.

I have routes set up for 'home' and 'terms and conditions' pages:

<?php

namespace App\Controller;

use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\Routing\Annotation\Route;

class GeneralController extends AbstractController {

    /**
     * @Route("/", name="main_home")
     */
    public function homePage() {
        return $this->render('general/home.html.twig', ['title' => "Welcome"]);
    }

    /**
     * @Route("/terms", name="terms_and_conds")
     */
    public function termsAndConditions() {
        return $this->render('general/terms.html.twig', ['title' => "Terms and Conditions"]);
    }
}

These both work fine when I point my browser at:

https://example.com/

https://example.com/terms

In my Twig base template, I want to provide a link to the "terms" page:

<a href="{{ path('terms_and_conds') }}">Terms of Use</a>

however the URL which is generated is

https://example.com/index.php/terms

How do I ensure that the correct link is generated, excluding the index.php?

My /routes/annotations.yaml is:

controllers:
    resource: ../../src/Controller/
    type: annotation

kernel:
    resource: ../../src/Kernel.php
    type: annotation

I am running Symfony 5.0.4 on PHP 7.4.2 as FPM application served by Nginx on CentOS 7 with Plesk Obsidian.

Edit:

Here's my nginx.conf

Note that this is generated by Plesk and I am unable to change it directly, however I can do what I want in the included vhost_nginx.conf file.

#ATTENTION!
#
#DO NOT MODIFY THIS FILE BECAUSE IT WAS GENERATED AUTOMATICALLY,
#SO ALL YOUR CHANGES WILL BE LOST THE NEXT TIME THE FILE IS GENERATED.
server {
    listen 111.222.333.444:443 ssl http2;

    server_name example.com;
    server_name www.example.com;
    server_name ipv4.example.com;

    ssl_certificate             /usr/local/psa/var/certificates/scfecEZhs;
    ssl_certificate_key         /usr/local/psa/var/certificates/scfecEZhs;
    ssl_client_certificate      /usr/local/psa/var/certificates/scfsFsYXH;

    client_max_body_size 128m;

    proxy_read_timeout 120;

    root "/var/www/vhosts/example.com/public";
    access_log "/var/www/vhosts/system/example.com/logs/proxy_access_ssl_log";
    error_log "/var/www/vhosts/system/example.com/logs/proxy_error_log";

    if ($host ~* ^www\.example\.com$) {
        rewrite ^(.*)$ https://example.com$1 permanent;
    }

    #extension letsencrypt begin
    location ^~ /.well-known/acme-challenge/ {
        root /var/www/vhosts/default/htdocs;

        types { }
        default_type text/plain;

        satisfy any;
        auth_basic off;
        allow all;

        location ~ ^/\.well-known/acme-challenge.*/\. {
            deny all;
        }
    }
    #extension letsencrypt end

    #extension sslit begin

    add_header Strict-Transport-Security "max-age=15768000; includeSubDomains" always;

    #OCSP Stapling
    ssl_stapling on;
    ssl_stapling_verify on;

    #extension sslit end

    location / {
        proxy_pass https://111.222.333.444:7081;
        proxy_set_header Host             $host;
        proxy_set_header X-Real-IP        $remote_addr;
        proxy_set_header X-Forwarded-For  $proxy_add_x_forwarded_for;
        access_log off;

    }

    location ~ "^/" {
        proxy_pass https://111.222.333.444:7081;
        proxy_set_header Host             $host;
        proxy_set_header X-Real-IP        $remote_addr;
        proxy_set_header X-Forwarded-For  $proxy_add_x_forwarded_for;
        access_log off;

    }

    location @fallback {
        proxy_pass https://111.222.333.444:7081;
        proxy_set_header Host             $host;
        proxy_set_header X-Real-IP        $remote_addr;
        proxy_set_header X-Forwarded-For  $proxy_add_x_forwarded_for;
        access_log off;

    }

    location ~ ^/(.*\.(ac3|avi|bmp|bz2|css|cue|dat|doc|docx|dts|eot|exe|flv|gif|gz|htm|html|ico|img|iso|jpeg|jpg|js|mkv|mp3|mp4|mpeg|mpg|ogg|pdf|png|ppt|pptx|qt|rar|rm|svg|swf|tar|tgz|ttf|txt|wav|woff|woff2|xls|xlsx|zip))$ {
        try_files $uri @fallback;
    }

    location ~ ^/~(.+?)(/.*?\.php)(/.*)?$ {
        alias /var/www/vhosts/example.com/web_users/$1/$2;
        fastcgi_split_path_info ^((?U).+\.php)(/?.+)$;
        fastcgi_param PATH_INFO $fastcgi_path_info;
        fastcgi_pass "unix:///var/www/vhosts/system/example.com/php-fpm.sock";
        include /etc/nginx/fastcgi.conf;

    }

    location ~ ^/~(.+?)(/.*)?$ {
        proxy_pass https://111.222.333.444:7081;
        proxy_set_header Host             $host;
        proxy_set_header X-Real-IP        $remote_addr;
        proxy_set_header X-Forwarded-For  $proxy_add_x_forwarded_for;
        access_log off;

    }

    location ~ \.php(/.*)?$ {
        fastcgi_split_path_info ^((?U).+\.php)(/?.+)$;
        fastcgi_param PATH_INFO $fastcgi_path_info;
        fastcgi_pass "unix:///var/www/vhosts/system/example.com/php-fpm.sock";
        include /etc/nginx/fastcgi.conf;

    }

    add_header X-Powered-By PleskLin;

    include "/var/www/vhosts/system/example.com/conf/vhost_nginx.conf";
}
server {
    listen 111.222.333.444:80;

    server_name example.com;
    server_name www.example.com;
    server_name ipv4.example.com;

    client_max_body_size 128m;

    proxy_read_timeout 120;

    if ($host ~* ^www\.example\.com$) {
        rewrite ^(.*)$ https://example.com$1 permanent;
    }

    location / {
        return 301 https://$host$request_uri;
    }
}

and the included vhost_nginx.conf

    if (!-f $request_filename) {
        rewrite ^/(.*)$ /index.php last;
    }

    location ~ ^/index\.php(/|$) {
        fastcgi_pass unix:/run/plesk-php74-fpm.sock;
        fastcgi_split_path_info ^(.+\.php)(/.*)$;
        include fastcgi_params;

        fastcgi_param SCRIPT_FILENAME $realpath_root$fastcgi_script_name;
        fastcgi_param DOCUMENT_ROOT $realpath_root;

        internal;
    }

    location ~ \.php$ {
        return 404;
    }

Replace:

if (!-f $request_filename) {
    rewrite ^/(.*)$ /index.php last;
}

With:

location / {
    try_files $uri /index.php$is_args$args;
}

Reference: https://symfony.com/doc/current/setup/web_server_configuration.html#nginx

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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