如何配置.htaccess实现URL含/restaurant/路径时定向至对应子文件夹?
Got it, let's sort out this routing issue. The problem right now is that your root .htaccess is catching all requests (except test.pdf and PNGs) and sending them to index.php—including those targeting your /restaurant/ subfolder. Here's how to fix it:
Solution: Update Your Root .htaccess
Add an additional condition to exclude /restaurant/-prefixed requests from your existing rewrite rule. This lets Apache handle those requests normally, routing them directly to your restaurant subfolder (and respecting any .htaccess rules inside it if you have them).
Here's the modified code:
RewriteEngine On RewriteBase / # Exclude restaurant subfolder requests from the main rewrite rule RewriteCond %{REQUEST_URI} !^/restaurant/ [NC] RewriteCond %{REQUEST_URI} !(test\.pdf|\.png)$ [NC] RewriteRule ^(.+)$ index.php [QSA,L]
What This Does:
- The new
RewriteCond %{REQUEST_URI} !^/restaurant/ [NC]line tells Apache: "if the requested URL does NOT start with/restaurant/(case-insensitive)" - Combined with your existing conditions, this means only non-restaurant, non-PDF/PNG requests get forwarded to the root
index.php - Requests like
mydomain.com/restaurant/menuormydomain.com/restaurant/aboutwill now be directed straight to the corresponding files in yourrestaurantsubfolder
Alternative Approach (If You Need Explicit Rewriting)
If you want to explicitly rewrite the restaurant requests instead of just excluding them (though the above method is cleaner), you can add a dedicated rewrite rule before your existing one:
RewriteEngine On RewriteBase / # Forward all restaurant requests to the subfolder RewriteRule ^restaurant/(.*)$ restaurant/$1 [L] # Existing rules for other requests RewriteCond %{REQUEST_URI} !(test\.pdf|\.png)$ [NC] RewriteRule ^(.+)$ index.php [QSA,L]
The [L] flag ensures that once this rule matches, Apache stops processing any further rules, so the restaurant requests don't get caught by the root index.php rewrite.
Quick Check
Make sure your restaurant subfolder has the necessary files (like its own index.php) and that permissions are set correctly so Apache can access them. If you have a .htaccess inside the restaurant folder, double-check its RewriteBase is set to /restaurant/ if needed.
内容的提问来源于stack exchange,提问作者Roman Ganoshenko

