Redirect Subdomain Using htaccess and Keep URL

旧城冷巷雨未停 提交于 2019-11-29 16:23:00

Well, specifically for the url https://secure.example.com/path/ to display content found at http://www.example.com/path/, there's 2 things that you can do. The best thing is if the 2 domains are served from the same document root:

they are on the same host and same document root. The subdomain is literally a subfolder under the root setup with a subdomain. With help from my host we now have it resolving properly and the SSL installed.

The subdirectory might be a problem, since it may not be able to rewrite out of its own document root. For example:

  • domain -> document root
  • www.example.com -> /path/to/htdocs
  • secure.example.com -> /path/to/htdocs/secure

If you had an .htaccess file in /path/to/htdocs/secure to rewrite requests for https://secure.example.com/. The problem here is you need to rewrite to the parent directory, and apache won't let you do that. If it was the other way around, you could rewrite requests for http://www.example.com/ to /secure. Also, if the two domains had the same document root, you could also rewrite. But not if secure is a subdirectory to www's document root.

My host says that mod_proxy is installed. My Apache config shows these are installed: proxy_module (static) proxy_connect_module (static) proxy_ftp_module (static) proxy_http_module (static) proxy_ajp_module (static) proxy_balancer_module (static)

That means you can at least use the P rewrite flag to send the request off to mod_proxy, so you can do something like this:

RewriteEngine On
RewriteRule ^path/$ http://www.example.com/path/ [L,P]

in the htaccess file in your secure.example.com's document root. That will only proxy specifically the URI /path/, and not anything like /path/foo/bar.html. If you want everything starting with /path/, then you can match against it:

RewriteRule ^path/(.*)$ http://www.example.com/path/$1 [L,P]

If there are redirection issues, you may want to resort to using ProxyPass instead:

ProxyPass / http://www.example.com/
ProxyPassReverse / http://www.example.com/

It does the same thing except it rewrites location headers so redirects also get proxied.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!