1

The last couple of hours I have been learning about .htaccess rewrite. I haven't been able to get it right. I have read a lot of posts her on SO, but can't solve my problem. This is my situation:

www.example.com 

redirects to

example.example2.com

Now I want to show the original domain in the URL. I have this:

RewriteCond %{HTTP_HOST} ^example\.example2\.com/$ [NC]
RewriteRule ^(.*)$ http://www.example.com/$1 [L,R=301]

This doesn't work. I still see the example.example2.com instead of www.example.com

The .htaccess file is located at the top level in the folder for the subdomain.

I should note again that this is new to me and this is what I have been able to come up with from examples I found on the internet. Can someone tell how to get this right?

Thanks a lot

Edit:

  1. I'm trying to install a Drupal multisite configuration. My problem seemed solvable with .htaccess instructions as explained here and here. Because of this I posted it here and not on the Drupal part of SO. The idea of a Drupal multisite configuration is to direct all traffic to the installment and then differentiate according to the request made.

  2. I have no direct control over the redirect from www.example.com to example.example2.com. My hosting company provides a direct redirect or via frame. I can check either one in a form. I don't know what they do. With a direct redirect the URL changes from www.example.com to example.example2.com. (where the Drupal installation is now) If I choose frame the URL stays the same (of course), but hovering over a link still reveals the example.example2.com identity.

  3. I didn't start out with a multisite configuration. I will move example.exampl2.com to www.example2.com/sites/www.example.com as in the instruction for a multisite. In the meanwhile the site is at example.example2.com. But that means I have two installments to maintain, which is not preferred. So I started out by trying to have example.example2.com look like www.example.com. Then at least visitors would never notice differences.

I now have this

RewriteCond %{ENV:REDIRECT_STATUS} 200
RewriteRule ^ - [L]
RewriteCond %{HTTP_HOST} ^example\.example2\.nl$ [NC]
RewriteRule ^(.*)$ http://www.example.com/$1 [L,R]

And it results in a loop.

Sigur
  • 317
  • 1
  • 3
  • 12

2 Answers2

0

First thing you have your requirement reversed. You probably want to redirect example.example2.com to www.example.com.

Second thing what you are trying to do will be possible with mod_proxy support only. Enable mod_proxy in httpd.conf then enable mod_rewrite and .htaccess through httpd.conf and then put this code in your .htaccess under DOCUMENT_ROOT directory:

Options +FollowSymLinks -MultiViews
# Turn mod_rewrite on
RewriteEngine On
RewriteBase /

RewriteCond %{HTTP_HOST} ^example\.example2\.com$ [NC]
RewriteRule ^ http://www.example.com%{REQUEST_URI} [L,P]

Note that flag P here is used for proxy support in mod_rewrite.

anubhava
  • 761,203
  • 64
  • 569
  • 643
  • Hm, I don't think the req. are reversed, but I will elaborate a bit. Nobody knows about example.example2.com, but I have my CMS installed there; www.example.com will be communicated to users. There is nothing installed at www.example.com. It 's just a domain I own. Nobody needs to know about example.example2.com, so I want to hide that. – Sigur Apr 19 '13 at 08:22
  • So the customer request will only come for WWW. Domain? Then where do you need to hide example domain? – anubhava Apr 19 '13 at 09:41
  • I will be serving several websites from 1 URL. Depending on the redirect to subfolders there needs to be a rewrite. The 'customer' or user of the site doesn't need to know this. He will just see the requested URL. – Sigur Apr 19 '13 at 09:56
  • so If I understand you correctly you will expose just one domain to customers. But I don't understand who & how will access example domain: `example.example2.com` ? – anubhava Apr 19 '13 at 12:30
  • Nobody will access example.example2.com directly. That constructed URL is just a technical solution, which is of no importance to visitors. That's why I need a rewrite. URL's constructed by a system show technical design, but that is not necessarily how one would want to expose the system to users/visiters. SO is full of this kind questions about of rewriting URL's. – Sigur Apr 19 '13 at 14:38
  • If nobody will ever access `example.example2.com` then may I ask why is your app even using links with above domain `example.example2.com`? – anubhava Apr 19 '13 at 15:52
  • Thanks you anubhava for being so persistent in your questions. I really appreciate your help. I will edit my question tomorrow and be more specific. I was being general on purpose, but it seems this isn't working out. Thanks for your attention I hope you'll be back later. – Sigur Apr 19 '13 at 20:07
  • Sure @Sigur, once you edit your question, leave a comment here and I will try my best to answer it. – anubhava Apr 19 '13 at 20:09
  • I have added more info to my question. Thanks – Sigur Apr 22 '13 at 21:13
  • `I have no direct control over the redirect from www.example.com to example.example2.com` Problem seems be this. Your hosting co. is redirecting from www to example and by your rules in your .htaccess you're trying to do reverse. There is no way other than to find how to stop this redirection from your hosting company. – anubhava Apr 23 '13 at 05:06
  • Ok many thanks. I will rethink my situation and try and find a solution. – Sigur Apr 23 '13 at 11:35
0

Your .htaccess is almost perfect. There's just one issue, HTTP_HOST is the host only, without a trailing slash /. The RewriteCond should be

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

Now, there is a loop rewriting from www.example.com to example.example2.com and back again. To break this endless loop, you must add a terminating condition, which stops the rules. Prefix the rules with

RewriteCond %{ENV:REDIRECT_STATUS} 200
RewriteRule ^ - [L]

When everything works as you expect, you can change R to R=301.

Never test with 301 enabled, see this answer Tips for debugging .htaccess rewrite rules for details.

Community
  • 1
  • 1
Olaf Dietsche
  • 72,253
  • 8
  • 102
  • 198
  • I tried this. I know the rewrite is happening because now I have another problem. The redirect is now bouncing between the two domains. FF is telling me 'Firefox has detected that the server is redirecting the request for this address in a way that will never complete.' Maybe the solution I was trying to implement isn't the answer to my problem. Please read my comments I gave to anubhava. Thanks for your help. – Sigur Apr 19 '13 at 20:09
  • @Sigur It seems, you have an endless rewrite loop. Please see the updated answer. If this doesn't help, show the other rule in your question. – Olaf Dietsche Apr 19 '13 at 20:44
  • I have added more information on the redirect and the rules on how they are now in the .htaccess file – Sigur Apr 22 '13 at 21:15