PHP实现Clean URL遇500错误:带斜杠路径触发问题求助
Hey there! Let's figure out why those trailing slashes (example.com/foo/) and nested paths (example.com/foo/bar) are throwing 500 errors, especially since you mentioned the issue crops up when calling your VisYdelse() function. We'll cover both the .htaccess rewrite rules and your PHP logic here.
1. First, Check Your .htaccess Configuration
A common culprit for these kinds of 500 errors is misconfigured rewrite rules that cause internal redirect loops, or rules that don't properly handle trailing slashes. Let's start here:
Verify Basic Rewrite Setup
Make sure your .htaccess has these foundational lines (if not, add them):
RewriteEngine On RewriteBase / # Disable directory listings (optional but helpful) Options -Indexes
Fix Trailing Slash Handling
If your rules don't account for trailing slashes, requests like example.com/foo/ might get caught in a loop. Add these rules to either remove trailing slashes (for non-directories) or handle them properly:
# Remove trailing slash if the requested path isn't a real directory RewriteCond %{REQUEST_FILENAME} !-d RewriteRule ^(.*)/$ /$1 [L,R=301] # Forward all non-file/non-directory requests to index.php RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_FILENAME} !-d RewriteRule ^(.*)$ index.php?url=$1 [L,QSA]
The QSA flag ensures any query strings get passed through to index.php, which is useful if you ever need them.
2. Debug the VisYdelse() Function in PHP
Since the error only happens when calling this function, the issue is likely in how it processes the URL path when there are extra slashes or segments. Here's how to diagnose:
Enable Error Display (Temporarily!)
Add these lines at the very top of your index.php to see the exact PHP error causing the 500 (don't leave this on in production!):
error_reporting(E_ALL); ini_set('display_errors', 1);
This will show you things like undefined variables, array index errors, or failed file includes—all common causes of 500s.
Inspect the URL Parameter
Print out the raw URL value being passed to your script to confirm what VisYdelse() is working with:
echo "URL received: " . $_GET['url'];
For example.com/foo/, this will output foo/ (with a trailing slash), and for example.com/foo/bar, it'll be foo/bar. If your function expects a single, clean segment (like foo), these values will break things.
Clean Up the URL Before Processing
Add this line to normalize the URL input before passing it to VisYdelse():
$cleanUrl = trim($_GET['url'], '/'); // Removes leading/trailing slashes // Now $cleanUrl is "foo" for /foo/ and "foo/bar" for /foo/bar
Then update your function call to use $cleanUrl instead of the raw $_GET['url'].
Check VisYdelse() Logic
Look for places in the function where it might assume only one path segment exists:
- If it uses
explode('/', $url)to split the path, a trailing slash will create an empty string at the end of the array (e.g.,explode('/', 'foo/')gives['foo', '']). - If it tries to access a specific array index (like
$parts[0]) without checking if it exists, nested paths might lead to unexpected behavior. - If it uses the URL segment to include files (e.g.,
include($url . '.php')), a trailing slash or nested path could point to a non-existent file, triggering a fatal error.
For nested paths like foo/bar, you might need to add logic to handle multiple segments—for example, routing to a sub-page or passing the second segment as a parameter to VisYdelse().
3. Check Apache Error Logs
If you still can't pinpoint the issue, take a look at your Apache server's error log (usually located at /var/log/apache2/error.log on Linux, or in your hosting control panel for shared hosts). It will have the exact error message and line number where the 500 occurred—this is often the fastest way to find the root cause.
内容的提问来源于stack exchange,提问作者Sebastian Støttrup

