登录后无法通过Router重定向至主页面问题求助
Why Your Redirect Isn't Working
Got it, let's break down why your current setup isn't working and fix it right away:
- Your
header()call is missing the requiredLocation:prefix — the server won't recognize it as a redirect instruction without this. - The relative path
/../../../public/index.phpdoesn't align with your URL structure. Chances are your web server uses thepublicfolder as the document root, sopublic/doesn't show up in the actual browser URL. That path would try to point to a non-existent location outside your subdomain's root.
Fixes to Get You Redirected
Here are two straightforward, reliable solutions:
1. Root-Relative Path (Simplest Option)
Since your target is the root of sub.mydomain.de, just redirect directly to / — this will automatically map to your public/index.php file (assuming your server is configured correctly):
// After your login validation passes if ($login_success) { // Enable output buffering if you're worried about accidental prior output ob_start(); header("Location: /"); exit(); // Always exit after redirect to stop further script execution }
2. Absolute URL (Most Reliable Option)
If you want to eliminate any path ambiguity, use the full absolute URL of your main page:
if ($login_success) { ob_start(); header("Location: https://sub.mydomain.de/"); exit(); }
Key Reminders
- No Output Before
header(): Even a single space, newline, or HTML tag before your<?phptag will break the redirect. If you can't avoid accidental output, addob_start();at the very top oflogin.phpto enable output buffering. - Exit After Redirect: Always call
exit()ordie()right after theheader()call. This stops the rest of your script from running, preventing unexpected behavior or partial page renders.
内容的提问来源于stack exchange,提问作者F.Ghost
相关产品推荐
相关产品推荐

