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

如何通过.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 (so Foo.com or FOO.COM will also trigger the redirect).
  • ^account(/.*)?$: Matches the /account path, including any subpaths like /account/profile or /account/settings.
  • [R=301]: Uses a permanent redirect (use R=302 instead 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_rewrite is enabled on your Apache server.
  • The subdomain my.foo.com must be properly configured in your DNS to point to the same server as foo.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 (requires mod_proxy and mod_proxy_http to be enabled).
  • ^(.*)$: Matches every path on my.example.com (e.g., my.example.com/profile will fetch example.com/account/profile).
  • ProxyPassReverse: Updates any URLs in the response headers (like redirects) to use my.example.com instead of example.com/account, so users don't get redirected away from the subdomain.
  • ProxyPassReverseCookieDomain & ProxyPassReverseCookiePath: Ensures cookies set by example.com work correctly on my.example.com.

Prerequisites:

  • Enable mod_rewrite, mod_proxy, and mod_proxy_http on your Apache server (check your hosting control panel or Apache config if you're unsure).
  • my.example.com must have a valid DNS record pointing to your server and a properly configured virtual host.
  • Ensure example.com/account doesn't have CORS restrictions or hotlink protection that would block your server from fetching its content.

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.05.27 06:50:36