You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何配置.htaccess实现URL含/restaurant/路径时定向至对应子文件夹?

Fixing .htaccess Routing for Your Restaurant Subfolder

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/menu or mydomain.com/restaurant/about will now be directed straight to the corresponding files in your restaurant subfolder

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

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.27 04:08:12