多语言网站.htaccess配置:英文目录页面404问题求助
First, let's address the immediate issues with your current configuration and get the English (/en) section working properly.
Original Configuration Issues
Your existing rules only target the root directory (Croatian), so requests like /en/items/ aren't being mapped to /en/items.php. Additionally, there's a critical typo in one of your premium URL rules that would break even the Croatian version of that path:
# Wrong: Uses ? instead of & to add the place parameter (resets the query string) RewriteRule ^items/premium/(place_1)/(sale_type_1|sale_type_2)/(quart_1|quart_2|quart_3)/?$ items.php?premium=true?place=$1&sale_type=$2&quart=$3 [L,QSA]
The second ? wipes out existing query parameters, so the place value never reaches your PHP script. This should be an & instead.
Corrected .htaccess Configuration
Here's the updated file that supports both your default Croatian (root) and English (/en) sections:
RewriteEngine on # Remove .php extension from visible URLs (handles root and /en paths) RewriteCond %{THE_REQUEST} ^[A-Z]+\ /(en/)?([^/]+/)*[^.]+\.php(\?[^\ ]*)?\ HTTP/ RewriteRule ^(en/)?(([^/]+/)*[^.]+)\.php$ http://localhost/website/$1$2 [R=301,L] # Internally map requests to .php files (handles root and /en paths) RewriteCond %{REQUEST_FILENAME} !-d RewriteCond %{REQUEST_FILENAME}\.php -f RewriteRule ^(en/)?([^/]+)/$ $1$2.php [L] # Add trailing slash to URLs (works for both root and /en) RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_FILENAME} !-d RewriteCond %{REQUEST_URI} !(\.[a-zA-Z0-9]{1,5}|/)$ RewriteRule (.*)$ http://localhost/website/$1/ [R=301,L] # ------------------------------ # Croatian (Root) Pretty URLs # ------------------------------ RewriteRule ^items/offer/(place_1|place_2)/?$ items.php?place=$1 [L,QSA] RewriteRule ^items/offer/(place_1|place_2)/(sale_type_1|sale_type_2)/?$ items.php?place=$1&sale_type=$2 [L,QSA] RewriteRule ^items/offer/(place_1|place_2)/(sale_type_1|sale_type_2)/(type_1|type_2|type_3)/?$ items.php?place=$1&sale_type=$2&type=$3 [L,QSA] RewriteRule ^items/premium/(place_1)/(quart_1|quart_2|quart_3)/?$ items.php?premium=true&place=$1&quart=$2 [L,QSA] # Fixed the typo here (changed ? to &) RewriteRule ^items/premium/(place_1)/(sale_type_1|sale_type_2)/(quart_1|quart_2|quart_3)/?$ items.php?premium=true&place=$1&sale_type=$2&quart=$3 [L,QSA] RewriteRule ^items/featured/?$ items.php?featured=true [L,QSA] RewriteRule ^item/([0-9]+)/?$ item.php?id=$1 [L,QSA] # ------------------------------ # English (/en) Pretty URLs # ------------------------------ RewriteRule ^en/items/offer/(place_1|place_2)/?$ en/items.php?place=$1 [L,QSA] RewriteRule ^en/items/offer/(place_1|place_2)/(sale_type_1|sale_type_2)/?$ en/items.php?place=$1&sale_type=$2 [L,QSA] RewriteRule ^en/items/offer/(place_1|place_2)/(sale_type_1|sale_type_2)/(type_1|type_2|type_3)/?$ en/items.php?place=$1&sale_type=$2&type=$3 [L,QSA] RewriteRule ^en/items/premium/(place_1)/(quart_1|quart_2|quart_3)/?$ en/items.php?premium=true&place=$1&quart=$2 [L,QSA] RewriteRule ^en/items/premium/(place_1)/(sale_type_1|sale_type_2)/(quart_1|quart_2|quart_3)/?$ en/items.php?premium=true&place=$1&sale_type=$2&quart=$3 [L,QSA] RewriteRule ^en/items/featured/?$ en/items.php?featured=true [L,QSA] RewriteRule ^en/item/([0-9]+)/?$ en/item.php?id=$1 [L,QSA] # ------------------------------ # Error Handling # ------------------------------ # Serve language-specific 404 pages RewriteRule ^404/?$ 404.php [L] RewriteRule ^en/404/?$ en/404.php [L] # Redirect 404s to the appropriate language version ErrorDocument 404 /website/404/ RewriteCond %{REQUEST_URI} ^/en/ RewriteRule ^en/.*$ /website/en/404/ [R=301,L]
Key Changes Explained
- Language-Agnostic Extension Handling: The first two rule sets now capture the optional
en/prefix, so they work for both root and English paths. - Duplicated Pretty URL Rules: We added a copy of all your pretty URL rules prefixed with
^en/, pointing to the corresponding/en/...phpfiles. - Fixed Typo: Corrected the
?placeto&placein the premium rule to ensure all query parameters are passed correctly. - Language-Specific 404s: Added rules to serve the English 404 page when requests are made under the
/endirectory.
Best Practices for Multi-Language .htaccess Setup
Here’s a step-by-step approach to maintain this setup going forward:
- Group Rules by Language: Keep root and language-specific rules separated to make the file easier to edit and debug.
- Test with 302 First: Use
R=302instead ofR=301temporarily when testing redirects. 301s are cached by browsers, which can make adjustments frustrating. Switch to 301 once everything works. - Use QSA Flag: Always include
QSA(Query String Append) in your pretty URL rules to preserve any extra query parameters passed to the URL. - Avoid Hardcoded URLs: For portability, replace hardcoded
http://localhost/websitewithhttp://%{HTTP_HOST}/websiteto adapt to your domain automatically. - Scalable Regex for Multiple Languages: If you add more languages later, use a regex group like
^(en|fr|de)/to avoid duplicating rules for every language (e.g.,RewriteRule ^([a-z]{2})/items/offer/(...)/?$ $1/items.php?place=$1 [L,QSA]).
内容的提问来源于stack exchange,提问作者RobiZzT

