如何通过.htaccess实现URL重写:保持storeadmin URL并渲染store页面
Got it, let's fix this for you step by step:
The Core Issue with Your Current Rules
Your first rule uses R=301, which is an external redirect—this tells the browser to navigate to the new URL directly, so the address bar changes, which is exactly what you want to avoid.
Your second attempt with the N flag caused an infinite loop: the rule rewrites /storeadmin/... to /store/..., then the N flag forces Apache to reprocess the rules. Your second rule then tried to rewrite back to /storeadmin/..., creating a never-ending loop that Apache kills with a 500 internal server error.
The Correct Internal Rewrite Rule
You need an internal rewrite—this lets Apache serve content from /store/%1/user/login while keeping the original /storeadmin/%1/user/login in the browser's address bar. Here's the working rule set:
# Match requests starting with storeadmin (with optional leading slash) RewriteCond %{REQUEST_URI} ^/?storeadmin/([a-z]{2})/user/login/?$ [NC] # Internally rewrite to the store path - NO R=301 flag! RewriteRule ^ /store/%1/user/login [L,NC]
What Each Part Does
[NC]: Makes the match case-insensitive (soStoreAdminorSTOREADMINwill also trigger the rule)[L]: Tells Apache this is the last rule to process for this request, preventing unintended follow-up rules from interfering- No
Rflag: This is critical—without it, Apache handles this as an internal rewrite instead of an external redirect. The browser never learns the path changed, so the address bar stays unchanged.
Key Notes to Avoid Issues
- If you're using
.htaccess, make sure your Apache config hasAllowOverride All(or at leastAllowOverride FileInfo) enabled for the target directory—otherwise, rewrite rules won't take effect. - This rule won't cause loops: the rewritten path starts with
/store/, which doesn't match theRewriteCond(which targets/storeadmin/), so Apache won't reapply the rule.
Why Your Previous Attempts Failed
R=301: Forces an external redirect, which modifies the browser's address barNflag + second rewrite rule: Created an infinite loop where Apache kept switching between/storeadmin/and/store/paths until it hit the internal recursion limit, triggering a 500 error
内容的提问来源于stack exchange,提问作者Toma Tomov

