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

如何用.htaccess为URL添加前缀?附当前Slim3配置代码

Got it, you want to add a /route/ prefix to incoming URLs for your Slim 3 app—like turning http://localhost/george_shop.html into http://localhost/route/george_shop.html either under the hood or making the user's browser show the prefixed URL. Let's adjust your existing .htaccess to handle this, with two tailored options:

Option 1: Internal Rewrite (User URL stays unchanged)

This approach keeps the user's browser URL as http://localhost/george_shop.html, but the server passes the prefixed path (/route/george_shop.html) to Slim for processing. Here's the modified .htaccess:

<IfModule mod_rewrite.c> 
RewriteEngine On 

# Serve static files/directories directly, no need to pass to Slim
RewriteCond %{REQUEST_FILENAME} -s [OR] 
RewriteCond %{REQUEST_FILENAME} -l [OR] 
RewriteCond %{REQUEST_FILENAME} -d 
RewriteRule ^.*$ - [NC,L] 

# Calculate the base path (keep your original logic here)
RewriteCond %{REQUEST_URI}::$1 ^(/.+)(.+)::\2$ 
RewriteRule ^(.*) - [E=BASE:%1] 

# Append the /route/ prefix before forwarding to index.php
RewriteRule ^(.*)$ %{ENV:BASE}index.php/route/$1 [NC,L] 
</IfModule>

How it works:

The final rewrite rule takes the original request path (e.g., george_shop.html) and appends it to index.php/route/, so Slim receives /route/george_shop.html as the request path to match against your routes. Just make sure your Slim routes include the /route/ prefix—you can use a route group to keep this clean:

// Slim 3 route group example
$app->group('/route', function () use ($app) {
    $app->get('/george_shop.html', function ($request, $response) {
        // Your route logic here
        return $response->write('Welcome to George\'s Shop!');
    });
});

Option 2: External Redirect (User URL updates to show the prefix)

If you want users to actually see the /route/ prefix in their browser address bar (e.g., they visit http://localhost/george_shop.html and get redirected to http://localhost/route/george_shop.html), use this configuration instead:

<IfModule mod_rewrite.c> 
RewriteEngine On 

# Serve static files/directories directly
RewriteCond %{REQUEST_FILENAME} -s [OR] 
RewriteCond %{REQUEST_FILENAME} -l [OR] 
RewriteCond %{REQUEST_FILENAME} -d 
RewriteRule ^.*$ - [NC,L] 

# 301 redirect non-static requests without /route/ prefix to the prefixed URL
RewriteCond %{REQUEST_URI} !^/route/
RewriteRule ^(.*)$ /route/$1 [R=301,NC,L]

# Calculate the base path
RewriteCond %{REQUEST_URI}::$1 ^(/.+)(.+)::\2$ 
RewriteRule ^(.*) - [E=BASE:%1] 

# Forward to index.php as usual
RewriteRule ^(.*)$ %{ENV:BASE}index.php [NC,L] 
</IfModule>

Notes:

  • R=301 is a permanent redirect, which is good for SEO. Use R=302 if this is a temporary change.
  • After the redirect, Slim will receive the prefixed URL directly, so your routes should still include the /route/ prefix (or use the route group as shown above).

内容的提问来源于stack exchange,提问作者adot

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.29 09:04:15