: .htaccess redirect to subfolder in different domain, maintaining old domain in the URL Redirect has been widely discussed and most problems solved, so I am sorry for opening yet another post
Redirect has been widely discussed and most problems solved, so I am sorry for opening yet another post about this, but none of the codes I am trying work.
I have a WordPress site hosted in mydomain.com/clientsdomain.com/wordpress
I would like to temporarily redirect clientsdomain.com/ to the abovementioned URL, maintaining the clientsdomain.com domain in the URL.
So for example clientsdomain.com/some/page would be pointing to mydomain.com/clientsdomain.com/wordpress/some/page
Is this even possible with .htaccess? Maybe som configuration or plugin option with WordPress?
More posts by @Kaufman445
2 Comments
Sorted by latest first Latest Oldest Best
.htaccess is unlikely to be sufficient (of course, in principle .htaccess is just a place for local configuration, it depends on which modules are available too).
EDIT: (more details on why mod_rewrite/redirect) doesn't work)
Assuming that by .htaccess you're referring to the usual mod_rewrite/redirect directives, this will not work because the browser will be redirected, so the actual (redirected) URL will be visible in the location bar (http://mydomain.com/clientsdomain.com/wordpress), not the one initially requested (http://clientsdomain.com/).
Redirect and RedirectMatch will of course perform an external redirection: this will not maintain the old domain in the URL as requested.
mod_rewrite and RewriteRule provide two options for external links (see table at the end of the documentation (and the documentation about the flags):
^localpath(.*) otherhost/otherpath otherhost/otherpath/pathinfo via external redirection
^localpath(.*) otherhost/otherpath [R]
otherhost/otherpath/pathinfo via external redirection (the [R] flag is redundant)
^localpath(.*) otherhost/otherpath [P]
otherhost/otherpath/pathinfo via internal proxy
The external redirection option (no flag or [R]) will be equivalent to Redirect and cause the URL in the address bar to change.
The internal proxy option ([P]) will not cause the URL in the address bar to change, but, unless the server behind the internal proxy is aware it needs to change the base URLs in the links it serves, this will cause problems with the links embedded within the page (this is what mod_proxy_html can address).
There are at least two possible options to keep the requested URL in the location bar:
iframe
Keeping the original URL is typically done with an iframe. You could write a quick script (e.g. in PHP) that takes a URL (or part of it) as a query parameter.
For example, clientsdomain.com/redirect.php?location=some/page would create a page simply containing an iframe for mydomain.com/clientsdomain.com/wordpress/some/page. To make all the pages redirect from clientsdomain.com/, you could use a rewrite in .htaccess to redirect.php?.
In this context, clientsdomain.com/some/page would be rewritten as clientsdomain.com/redirect.php?location=some/page, which would contain an iframe pointing to mydomain.com/clientsdomain.com/wordpress/some/page.
One of the downsides is that, whenever a user clicks on a link the location bar will not change. It might also confuse some JavaScript scripts relying on the fragment (#).
mod_proxy_html
Another solution, possibly harder to install especially if you're on a shared host, would be to use mod_proxy_html to reverse proxy clientsdomain.com/ to mydomain.com/clientsdomain.com/wordpress.
This might be doable with .htaccess only (provided the module is installed), but I'm not sure: it depends whether its configuration directives are allowed in .htaccess or need to be in the global configuration. Either way, you'll almost certainly need full access to the Apache Httpd installation to configure this, so whether it's in .htaccess or in one of the main config files shouldn't really matter at this stage.
(A plain reverse proxy with mod_proxy_http without mod_proxy_html would allow you to proxy the requests, but wouldn't replace the links within the pages.)
Adding to this, you'll probably need mod_header on top of this (with any of the mod_proxy_*). The Location headers are absolute URLs according to the HTTP specification, so any redirect sent by the proxied server to itself (even simply for adding a trailing slash, for example) will have absolute URLs to itself. You can fix this problem using something along these lines (check whether the regexp and paths are correct when deploying):
Header edit Location ^http://mydomain.com/clientsdomain.com/wordpress clientsdomain.com/
(This being said, if the target Wordpress server is not to be used with its initial address, there may be an option or plugin to set its base URL to the new one, which could make mod_proxy handling easier.)
Terms of Use Create Support ticket Your support tickets Stock Market News! © vmapp.org2024 All Rights reserved.