GoDaddy Apache服务器URL Rewrite失效问题求助
Hey there, let's work through why your .htaccess rewrite isn't taking effect and get your friendly URLs up and running smoothly!
First, Let's Spot the Core Problem in Your Original Rule
Looking at your current code, the biggest issue is a mismatched capture group:
RewriteRule ^photographer/([A-Za-z-]+)/?$ photographers/photographer.php?id=$2&name=$1 [NC,L]
You only defined one capture group (([A-Za-z-]+)), so $2 doesn't exist—it will be empty. That's why your dynamic page isn't receiving the id parameter correctly. Plus, your desired friendly URL /photographer/john-doe doesn't include an id value, so we need to adjust how we pass parameters to your PHP script.
Fix 1: Adjust the Rewrite Rule (Matching Your Friendly URL Structure)
Since your friendly URL only includes the name (john-doe), we'll update the rule to pass just that parameter to your PHP page. Your script can then fetch the corresponding id from your database (or wherever you store that mapping).
Replace your existing .htaccess code with this:
Options +FollowSymLinks -MultiViews RewriteEngine On RewriteBase / # Internal reverse redirect: Map friendly URL to dynamic PHP page RewriteCond %{REQUEST_FILENAME} !-d RewriteCond %{REQUEST_FILENAME} !-f RewriteRule ^photographer/([A-Za-z0-9-]+)/?$ photographers/photographer.php?name=$1 [NC,L] # Optional: Redirect old dynamic URLs to friendly ones (for SEO & user experience) RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /photographers/photographer\.php\?id=\d+&name=([A-Za-z0-9-]+)\ HTTP/ RewriteRule ^photographers/photographer\.php$ /photographer/%1? [R=301,L,NC]
Let's break this down:
- The first set of rules handles the internal reverse redirect: when a user visits
/photographer/john-doe, Apache silently serves/photographers/photographer.php?name=john-doewithout changing the URL in the browser. - The optional second rule does a 301 permanent redirect: if someone (or a search engine) visits the old dynamic URL, they'll be sent to the friendly version. The
?at the end strips the original query string from the redirect.
Fix 2: GoDaddy-Specific Checks
GoDaddy's Apache setup has a few quirks—make sure these boxes are checked:
- .htaccess location: Save the file in your website's root directory (the same level as your
photographersfolder), and ensure the filename is exactly.htaccess(don't forget the leading dot, and keep it lowercase). - Enable AllowOverride: Log into your GoDaddy hosting panel, navigate to Apache Settings, and confirm
Allow Overridesis set toAll(this lets Apache read and apply your .htaccess rules). - Clear browser cache: Old cached URLs might interfere with testing—clear your cache or use incognito mode to verify the rewrites work as expected.
If You Later Want to Include ID in the Friendly URL
If you decide to add the id to your friendly URL (e.g., /photographer/1/john-doe), use this rule instead:
RewriteCond %{REQUEST_FILENAME} !-d RewriteCond %{REQUEST_FILENAME} !-f RewriteRule ^photographer/(\d+)/([A-Za-z0-9-]+)/?$ photographers/photographer.php?id=$1&name=$2 [NC,L]
内容的提问来源于stack exchange,提问作者Fabien

