2

I have found this rule that does about 90% of what I want, but not quite everything. My main goals for the redirect are to 1) force www. 2) redirect .com to .org. This does that, but if I were at www.domain.com/page, it just redirects to the home page and not www.domain.org/page. How can I get it to do this last step as well?

RewriteEngine on
RewriteCond %{HTTP_HOST} !^www.domain.org$ [NC]
RewriteRule ^(.*)$ http://www.domain.org/ [L,R=301]
  • 1
    Are the .com and .org domains pointing to the same place ie. you have 1 hosting account? Or are they 2 entirely separate hosting accounts? – MrWhite Mar 10 '15 at 20:09
  • The original rewrite rule wouldn't "almost work" if they weren't hosted in the same place. – Stephen Ostermiller Mar 10 '15 at 20:25
  • They are in the same place pointed to the same folders. I just have them set up using vhosts. – Krista Mar 10 '15 at 20:30
2

You will want to use your .htaccess file in the root of each site. You will be creating 2 .htaccess files, one per-site. I am assuming that you have each site defined within Apache as two separate sites. This is important to do.

In your .com site:

RewriteEngine on
RewriteRule ^(.*)$ http://www.domain.org/$1 [L,R=301]

In your .org site:

RewriteEngine on
RewriteCond %{HTTP_HOST} ^domain.org$ [NC]
RewriteRule ^(.*)$ http://www.domain.org/$1 [L,R=301]

This will do two things:

  • Do a blanket redirect from your .com site to your .org site.
  • Redirect any non-www request to www preserving the original request.
  • 1
    You are missing the $1 on the first RewriteRule. However, the OPs current code would seem to suggest that both domains are actually hosted at the same place - although it's not clear, I've asked the OP to confirm. – MrWhite Mar 10 '15 at 20:23
  • @w3d Yes you are right! Fixed. Thanks!! I was making an assumption about how the sites were hosted. Either way, this solution should work okay. I tend to do really odd regex(s), but mostly stay out of general .htaccess stuff and why I tend to shy away from these questions. You, however, are the local regex/.htaccess guru along with bybe. – closetnoc Mar 10 '15 at 20:34
  • I moved the rules to my apache conf files instead and it worked beautifully. Thanks @closetnoc! One final question would be, with your exact rule, I was getting double slashes after the redirect (www.domain.org//some/page). I just took the / out in the rule and it seems to work. I'm not doing something wrong by removing that slash am I? – Krista Mar 10 '15 at 21:29
  • @Krista Removing the / in the substitution is OK. Alternatively you could include a / in the pattern eg. ^/(.*)$. (Or, include the directives in a <Directory> section.) The reason for the difference in behaviour between server config (outside of <Directory> sections) and per-directory .htaccess files is that in your server config the per-directory prefix (ie. / - a slash in this instance) is not removed before pattern matching. – MrWhite Mar 10 '15 at 22:33
  • @w3d Thanks for chiming in while I was out. I made the edit for future users. Thanks(!) again. – closetnoc Mar 11 '15 at 1:06
3

That should be:

RewriteEngine on
RewriteCond %{HTTP_HOST} !^www.domain.org$ [NC]
RewriteRule ^(.*)$ http://www.domain.org/$1 [L,R=301]

The "$1" tells mod_rewrite to add the matched text from inside the parentheses in the pattern to the rewritten URL, which is what you want.

  • ok first I just added the $1 and tried that. It still went back to the homepage. Then, I noticed you changed ^(.*)$ to ^/(.*)$ and that broke all of them – Krista Mar 10 '15 at 20:06
  • With this being .htaccess you should omit the slash (per-directory) prefix on the RewriteRule pattern (or at least make it optional). – MrWhite Mar 10 '15 at 20:14
  • @Krista: Clear your browser cache. 301 redirects are cached by the browser, so you are probably seeing the result of the old/incorrect cached redirect. – MrWhite Mar 10 '15 at 20:17
  • You added a / to the beginning of the rewrite rule. Rewrite rules can't begin with a slash in .htaccess, only in the apache conf file. – Stephen Ostermiller Mar 10 '15 at 20:24
  • @StephenOstermiller Thanks for pointing that out -- I've fixed it. I'm a lot more used to editing Apache conf files than .htaccess files. – Mike Scott Mar 10 '15 at 20:28

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.