如何通过.htaccess实现foo.com/account到my.foo.com的URL重写及内容映射?
Alright, let's walk through how to solve both of these URL handling scenarios with .htaccess—I'll break down each one clearly, including the code you need and key checks to make sure everything works as expected.
1. Redirect foo.com/account to my.foo.com (Address Bar Updates)
This is an external redirect, meaning the user's browser address bar will update to show my.foo.com instead of foo.com/account. Here's the code to add to your .htaccess file in the root directory of foo.com:
# Enable mod_rewrite (required for all rewrite rules) RewriteEngine On # Redirect foo.com/account (and any subpaths) to my.foo.com RewriteCond %{HTTP_HOST} ^foo\.com$ [NC] RewriteRule ^account(/.*)?$ https://my.foo.com$1 [R=301,L]
What each part does:
[NC]: Makes the host check case-insensitive (soFoo.comorFOO.COMwill also trigger the redirect).^account(/.*)?$: Matches the/accountpath, including any subpaths like/account/profileor/account/settings.[R=301]: Uses a permanent redirect (useR=302instead if this is a temporary change). Permanent redirects are better for SEO.[L]: Tells Apache this is the last rule to process if it matches, preventing other rules from interfering.
Prerequisites:
- Make sure
mod_rewriteis enabled on your Apache server. - The subdomain
my.foo.commust be properly configured in your DNS to point to the same server asfoo.com, and have a valid virtual host setup (or be bound to your hosting account).
2. Proxy my.example.com to show example.com/account (Address Bar Remains)
This is a reverse proxy—the user sees my.example.com in their address bar, but Apache fetches content from example.com/account and serves it as if it's hosted directly on my.example.com. Here's the code:
# Enable required modules RewriteEngine On # Target requests to my.example.com RewriteCond %{HTTP_HOST} ^my\.example\.com$ [NC] # Proxy all requests to example.com/account, keep address bar unchanged RewriteRule ^(.*)$ https://example.com/account/$1 [P,L] # Fix relative links and cookies in the proxied content ProxyPassReverse / https://example.com/account/ ProxyPassReverseCookieDomain example.com my.example.com ProxyPassReverseCookiePath /account /
What each part does:
[P]: Triggers Apache's reverse proxy functionality (requiresmod_proxyandmod_proxy_httpto be enabled).^(.*)$: Matches every path onmy.example.com(e.g.,my.example.com/profilewill fetchexample.com/account/profile).ProxyPassReverse: Updates any URLs in the response headers (like redirects) to usemy.example.cominstead ofexample.com/account, so users don't get redirected away from the subdomain.ProxyPassReverseCookieDomain&ProxyPassReverseCookiePath: Ensures cookies set byexample.comwork correctly onmy.example.com.
Prerequisites:
- Enable
mod_rewrite,mod_proxy, andmod_proxy_httpon your Apache server (check your hosting control panel or Apache config if you're unsure). my.example.commust have a valid DNS record pointing to your server and a properly configured virtual host.- Ensure
example.com/accountdoesn't have CORS restrictions or hotlink protection that would block your server from fetching its content.
内容的提问来源于stack exchange,提问作者RatherLogical
相关产品推荐
相关产品推荐

