Redirecting base url only using htaccess - apache

I'd like to redirect only the base url to an external site.
For instance, I want example.com redirected to anotherdomain.com but I don't want example.com/path to be redirected.
So far, example.com/path redirects to anotherdomain.com/path. :(
EDIT :
First, thank you for the help! example.com now redirects to another.com without affecting the children paths of example.com.
However, ideally, m.example.com won't redirect to another.com. So it's really just example.com redirecting to another.com.

Add this to your .htaccess in your DocumentRoot. I am assuming that you are hosting only one domain on the server.
RewriteEngine on
RewriteBase /
RewriteCond %{HTTP_HOST} !^m\.
RewriteRule ^$ http://anotherdomain.com [R,L]

Related

Redirect a domain request to a subfolder

I am trying to redirect a domain to a subfolder within the same server. Let me break it down on topics:
I have the domain domain.com
I created the subdomain subdomain.domain.com
I redirected (A record) subdomain.domain.com to the domain.com server's IP address
I am trying to create a htaccess file that redirects every user that comes from subdomain.domain.com to domain.com/aaa
I can't figure out how though. I suppose I need to create a rewriting rule but don't know how.
To Redirect sub.example.com to example.com/folder you can use the following Rule in your /public_html/. htaccess
RewriteEngine on
RewriteCond %{HTTP_HOST} ^sub\.example\.com$ [NC]
RewriteRule (.*) http://example.com/folder/$1 [L,R=301,NE]

What in .htaccess redirect subdomain apache

What I must to do, when I have got DNS to subdomain and when someone go to blog.example.com the website url is the same but the content is from the main page not from blog folder.
What I should to do? What must be in /var/www/blog/ in .htaccess if anything?
Please help!
You can use the following rule :
RewriteEngine On
#if host is blog.domain.com
RewriteCond %{HTTP_HOST} ^blog\.example\.com$
#then serve all requests from the Root folder.
RewriteRule ^(.*)$ /$1 [NC,L]
Replace blog.example.com with your subdomain in the http_host string.

.htaccess redirect folder to its subdomain causes redirect loop

I have a subdomain sub.domain.com
And the same contents are available on domain.com/sub
But now I want to redirect domain.com/sub to sub.domain.com
If I do that with
Redirect 301 /sub http://sub.domain.com
it causes a redirect loop.
How can this be solved technically? Users should not be able to access the subdomain content under domain.com/sub but should be redirected.
Better to use mod_rewrite rules here with conditions to restrict the rule only for domain.com requests:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^(?:www\.)?domain\.com$ [NC]
RewriteRule ^sub(/.*)?$ http://sub.domain.com/$1 [L,NC,R=302]
Make sure to test this after clearing browser cache.

OpenShift and redirect in .htaccess

I have a Wordpress app deployed on OpenShift and a domain alias associated, i.e. www.example.org. Now I would like to add another alias, i.e. www.example2.org, and gracefully redirect all the request from www.example.org => www.example2.org.
I tried to do this via .htaccess:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^www.example.org$
RewriteRule (.*)$ http://www.example2.org/$1 [R=301,L]
Whenever contact www.example.org it generates an infinite loop of redirects and I can not understand why.
Openshift rewrites the redirect header, but you can prevent this by adding the port number to the URL. See more here.
There's no need for RewriteCond. And example.org is not an alias of example2.org. It's a brand new A record. You can eventually redirect www.example.org via your domain registrar's dashboard or create index.php with header('Location: http://www.example2.org');.
RewriteEngine on
RewriteRule ^(.*)$ http://www.example2.org/$1 [R,QSA,L]

Need help configuring 301 permanent redirect in Apache for non www

I am trying to configure my Apache 2.2 version to use a 301 permanent redirect when someone types my url without the www. I want to configure this in the httpd.conf and not using .htaccess if possible. I have tried using Redirect permanent but the first variable has to be a directory and not a url. Any ideas how to configure boom.com requests to be redirected to www.boom.com using a 301 redirect in Apache? Thanks
Add the following:
# Canonical hostnames
RewriteEngine On
RewriteCond %{HTTP_HOST} !^www\.boom\.com [NC]
RewriteCond %{HTTP_HOST} !=""
RewriteRule ^/(.*) http://www.boom.com/$1 [L,R=301]
This will redirect all request which don't match www.boom.com to www.boom.com, with the same query path. (For example, boom.com/foo?foo=bar will be redirect to www.boom.com/foo?foo=bar).
If you have named virtual hosts you could put the extra RewriteCond entries #tux21b gave inside to isolate them. Also if you have mod_alias you could try this which should do the same thing:
<VirtualHost boom.com:80>
RedirectMatch permanent /.* http://www.boom.com$0
</VirtualHost>
I'm sure someone will comment if there's a reason to use one over the other.