Reputation: 11
I have a WordPress site that I need to rewrite the URL for some of the pages to show that they are coming from a specific subdomain to match already existing print pieces. I am trying to do this via htaccess and mod_proxy, and am getting close, but need some help.
Here is the code I have now which somewhat works:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^subdomain\.domain\.edu$ [NC]
RewriteRule ^ http://othersubdomain.domain.edu/folder1/folder2/folder3%{REQUEST_URI} [L,P]
If I type in subdomain.domain.edu, I get the correct page (which is sitting at http://othersubdomain.domain.edu/folder1/folder2/folder3) and the browser URL stays written as subdomain.domain.edu. This works great!
The problem is with the inner pages. For example, if I type in subdomain.domain.edu/contact, I get redirected to the proper page, but the URL does not stay rewritten, it shows the full URL of where it is being served.
Can someone help me rewrite the inner pages' URL as well? I feel like I am so close!
Upvotes: 1
Views: 1026
Reputation: 3669
Well, you are not using mod_proxy here primarily. You are using the proxy flag of mod_rewrite. At the very least, you need to use ProxyPassReverse to ensure that the URL stays the same.
Also, mod_rewrite is not suggested in your use case. The page returned by your backend server may have absolute URLs embedded which refer the othersubdomain.domain.edu directly. You should use mod_proxy's ProxyPass or ProxyPassMatch along with mod_proxy_html. Let me know in comments if you are still unable to figure it out.
UPDATE:
First of all, take some time to sit down and read the complete documentation about mod_proxy. It is one of the most powerful apache modules and learning it in depth will go a long way. Secondly, don't use .htaccess for this.
I am assuming you have a Virtual Host configured for your specific sub-domain. If not, you should configure that first. Now, in your Virtual Host config, add these lines:
<VirtualHost *:80>
ServerName subdomain.domain.edu
ProxyPass / http://othersubdomain.domain.edu/folder1/folder2/folder3/
ProxyPassReverse / http://othersubdomain.domain.edu/folder1/folder2/folder3/
ProxyPassReverseCookieDomain othersubdomain.domain.edu subdomain.domain.edu
ProxyPassReverseCookiePath /folder1/folder2/folder3/ /
</VirtualHost>
The above configuration directives will proxy a request for http://subdomain.domain.edu/foo/bar to http://othersubdomain.domain.edu/folder1/folder2/folder3/foo/bar. When the response is available, its headers will be changed so that it appears to the user that it has been generated from subdomain.domain.edu. The Cookie directives are needed if your backend server generates any cookies (for example if the users have to sign in).
This will take you a long way. After that, if you want to change the links in the page content, embedded stylesheets or javascript, you should refer to the documentation for mod_proxy_html.
Upvotes: 2