Mobile app version of vmapp.org
Login or Join
Rivera981

: How do I use a htaccess redirect to force www and use the .org domain while maintaining the URL path? I have found this rule that does about 90% of what I want, but not quite everything.

@Rivera981

Posted in: #Htaccess #Redirects

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 2) redirect .com to .org. This does that, but if I were at domain.com/page, it just redirects to the home page and not 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 ^(.*)$ www.domain.org/ [L,R=301]

10.02% popularity Vote Up Vote Down


Login to follow query

More posts by @Rivera981

2 Comments

Sorted by latest first Latest Oldest Best

 

@Jamie184

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 ^(.*)$ www.domain.org/ [L,R=301]


In your .org site:

RewriteEngine on
RewriteCond %{HTTP_HOST} ^domain.org$ [NC]
RewriteRule ^(.*)$ www.domain.org/ [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.

10% popularity Vote Up Vote Down


 

@Welton855

That should be:

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


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

10% popularity Vote Up Vote Down


Back to top | Use Dark Theme