Skip to content

Avoid index.php duplication in security redirects - #1646

Open
Dominic-Mayers wants to merge 1 commit into
symfony:mainfrom
Dominic-Mayers:fix-index-duplication
Open

Avoid index.php duplication in security redirects#1646
Dominic-Mayers wants to merge 1 commit into
symfony:mainfrom
Dominic-Mayers:fix-index-duplication

Conversation

@Dominic-Mayers

@Dominic-Mayers Dominic-Mayers commented Feb 3, 2026

Copy link
Copy Markdown

This change addresses environments where internal URL rewriting (e.g., via mod_rewrite) is unavailable, forcing the index.php entry point or script name to remain visible in the request path.

When this 'dirty' absolute path is stored as a target for later redirection, Symfony's logic may prepend the script name again, resulting in duplication (e.g., /index.php/index.php/). Switching to an absolute URL (including scheme and host) prevents this re-processing.

Fixes #1645

// page, after a successful login you are redirected to a page in the previous
// locale. This code regenerates the referrer URL whenever the login page is
// browsed, to ensure that its locale is always the current one.
$this->saveTargetPath($request->getSession(), 'main', $this->generateUrl('admin_index'));

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The Symfony\Component\Security\Http\Firewall\ExceptionListener class, part of the security core component, has stored an absolute URL. It should remain an absolute URL.

Comment thread src/Controller/SecurityController.php Outdated
// locale. This code regenerates the referrer URL whenever the login page is
// browsed, to ensure that its locale is always the current one.
$this->saveTargetPath($request->getSession(), 'main', $this->generateUrl('admin_index'));
$this->saveTargetPath($request->getSession(), 'main', $this->generateUrl('admin_index', [], \Symfony\Component\Routing\Generator\UrlGeneratorInterface::ABSOLUTE_URL));

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

please add a use statement instead of using the FQCN inline

@Dominic-Mayers Dominic-Mayers Feb 5, 2026

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sure, I will do that. I did not do that first because I have seen recommendations elsewhere to the opposite when the namespace is used only once for a constant. It is done as you suggested.

This change addresses environments where internal URL rewriting (e.g., via mod_rewrite) is unavailable, forcing the index.php entry point or script name to remain visible in the request path.

When this 'dirty' absolute path is stored as a target for later redirection, Symfony's logic may prepend the script name again, resulting in duplication (e.g., /index.php/index.php/). Switching to an absolute URL (including scheme and host) prevents this re-processing.

Fixes symfony#1645
@Dominic-Mayers

Copy link
Copy Markdown
Author

The problem comes from how Symfony interprets the stored target during the post-authentication redirect.

TargetPathTrait::saveTargetPath() stores the value verbatim:
https://github.com/symfony/symfony/blob/7.0/src/Symfony/Component/Security/Http/Util/TargetPathTrait.php#L29-L36

When the user is redirected after login, the success handler uses that value directly to build the redirect response:
https://github.com/symfony/symfony/blob/7.0/src/Symfony/Component/Security/Http/Authentication/DefaultAuthenticationSuccessHandler.php#L86-L97

If the stored value is an absolute path (e.g. /index.php/...), Symfony treats it as an application path and applies the request context. The context base URL may already contain the front controller (/index.php):
https://github.com/symfony/symfony/blob/7.0/src/Symfony/Component/HttpFoundation/Request.php#L880-L915

In environments without URL rewriting (where the front controller must appear in the URL), this leads to duplicated front controllers:

baseUrl (/index.php) + stored path (/index.php/...)
→ /index.php/index.php/...

Symfony’s behavior can therefore be summarized as:

  • Path info (relative to the front controller) → context handling applies. ✅
  • Absolute path (/...) → treated as application-relative; context handling applies. ❌ : If rewrite is not used and the front controller therefore needs to be part of the URL, it will be prepended again, because the code assumes that path info has been saved.
  • Full URL (https://...) → considered final and used as-is, no base URL prepending. ✅

The demo currently stores an absolute path while implicitly expecting “no handling,” causing duplicated front controllers in non-rewritten setups. Storing a full absolute URL (this PR) makes the intent explicit and ensures correct behavior in all environments.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

The index.php url in the address bar at login is not well managed

2 participants