0

I'd like to force the use a different hostname/domain name for a particular directory on my site.

Currently, there is a directory that contains several webpages at mainsite.com/directory. For the pages that reside in just that directory, I want the domain name to change in users' address bar to differentsite.com. Ideally, the pages appear to be at the root level of differentsite.com.

What rule(s) can I add to my WordPress .htaccess file to accomplish this?

kma
  • 3
  • 1

1 Answers1

0

Create a block of the following 3 lines for each directory you want to do this for. Do this after the RewriteEngine on directive. In this example, www.example.com is the domain that you want to be used for the directory.

RewriteCond %{REQUEST_URI} ^/directory1(.*)
RewriteCond %{HTTP_HOST} !^www\.example\.com$
RewriteRule .* http://www.example.com%1 [L,R]

Once you've tested it, change [L,R] to [L,R=301] to denote a permanent redirect for better SEO friendliness. I don't have it to start because it complicates testing since it causes browsers to cache the redirect.

For the above to work, you'll need to make sure the server is configured to listen for the other domain and when that domain is accessed, use the full path to the subfolder (directory1 in this case) as the document root for the domain.

If you don't know how to do that and don't mind it having the subdirectory in the URL, then configure the other domain to use the same document root as the main domain and then use the following rules:

RewriteCond %{REQUEST_URI} ^/directory1.*
RewriteCond %{HTTP_HOST} !^www\.example\.com$
RewriteRule .* http://www.example.com%{REQUEST_URI} [L,R]
sa289
  • 1,318
  • 2
  • 18
  • 44
  • Thanks so much for this answer!!! I'm unfamiliar with the syntax so still having trouble even though your answer was very clear. I can't seem to get the code to work. Here is the code resident in the two .htaccess files on the two different servers (posted in the next comment). – kma Jun 18 '15 at 01:02
  • @kma try using fixee.org to paste it in and then provide a link to that – sa289 Jun 18 '15 at 01:06
  • http://fixee.org/paste/lndz371/ – kma Jun 18 '15 at 01:10
  • Try putting it before the `#BEGIN WordPress`. Be careful to copy exactly including not adding slashes (lines 14 and 21 in the example you pasted) and not removing a number after the percent sign (line 16). Let me know if that still doesn't solve it. – sa289 Jun 18 '15 at 16:13
  • Thanks again for your help, @sa289!! I'm getting closer. My code (thanks to your suggestions) is successfully redirecting to the homepage of example.com. However, it's not pointing to the /about directory of the original site. Ideally, I want the URL to change to example.com but show the content present at originalsite.com/about. So, the URL changes but the content is the same. Below is the code I'm using. http://fixee.org/paste/a92j0gh/ – kma Jun 19 '15 at 01:28
  • The most natural solution is to have the other site have its DocumentRoot (aka home directory) set to the about folder. If that's not an option, you could do a rewrite trick to make requests made to the root get sent to the about folder. At quick glance, http://stackoverflow.com/questions/10953615/invisibly-rewriting-root-to-subdirectory-using-mod-rewrite-ajaxplorer might contain a solution for you. – sa289 Jun 19 '15 at 15:00
  • @kma I hope you were able to get your issue resolved. If my answer was helpful to you, I'd appreciate if you could mark it as accepted so I can get credit for it. Thanks – sa289 Jul 13 '15 at 20:44