Redirect domain when there isn't a subdomain with htaccess - apache

When someone types http://example.com I need it to be redirected to http://www.example.com but if someone goes to http://somenthing.example.com it should not be redirected.
I've been trying different rules with no luck, can anyone point to me the right htaccess rule to use here?

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

Use this rule (place it on the top of your .htaccess .. as order of rules matters):
RewriteEngine On
# force www
RewriteCond %{HTTP_HOST} =example.com [NC]
RewriteRule .* http://www.example.com%{REQUEST_URI} [L,R=301]

Related

apache Rewrite rule appends /html directory for no reason

I have a domain foo.tech.
I want to use a new domain footech.io instead.
The redirect also has to make sure all the URLs work.
E.g foo.tech/bar goes to footech.io/bar
Here is my .htaccess file:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^foo.tech [NC]
RewriteRule ^(.*) http://footech.io/$1 [R=301,L]
For some reason, it decides to add /html at the end of my domain.
So now if I visit foo.tech it will redirect to footech.io/html
If I visit foo.tech/bar it will redirect to footech.io/html/bar
Please help.
Update:
I think the /html comes from the $1
I've tried to make the rewrite rule as follows:
RewriteRule ^(.*) http://footech.io/$1/$1 [R=301,L]
going to foo.tech leads to footech.io/html//html/
going to foo.tech/bar leads to footech.io/html/bar/html/bar
final update:
I made it work now using this:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^foo.tech [NC]
RewriteRule ^html/(.*) http://footech.io/$1 [R=301,L]
This seems to fix it
RewriteEngine On
RewriteCond %{HTTP_HOST} ^foo.tech [NC]
RewriteRule ^html/(.*) http://footech.io/$1 [R=301,L]

htaccess redirect with sub-domain

I have a domain example.com and in .htaccess, I am forwarding non-www to www address using below code.
RewriteEngine On
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,L]
Now I created a subdomain test.example.com and because of above rule, its going to http://www.test.example.com, so I thought to write it's own htaccess file to redirect www to non-www. So in the subdomain root, I have .htaccess file with below rule.
RewriteEngine On
RewriteCond %{HTTP_HOST} !^test\.example\.com$ [NC]
RewriteRule ^(.*)$ http://test.example.com/$1 [R=301,L]
It's not working and going in a redirect loop, how do I fix it?
Take out the ! and the escapes
RewriteEngine On
RewriteCond %{HTTP_HOST} ^test.example.com$
RewriteRule ^(.*)$ http://test.example.com/$1 [R=301,L]
You don't need 2 rules. Just keep this rule in main .htaccess for your main domain:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^example\.com$ [NC]
RewriteRule ^ http://www.%{HTTP_HOST}%{REQUEST_URI} [NE,R=301,L]
Now RewriteCond %{HTTP_HOST} ^example\.com$ condition will only impact main domain leaving your sub domain untouched.
I found a very easy solution for this problem.
My main site document root was /home/example/public_html and subdomain document root was /home/example/public_html/test.
I just changed the document root of subdomain to /home/example/test and it worked.

.htaccess - redirect domain name

I have a domain name www.domainA.com I want to redirect it to domainB in following manner.
www.domainA.com -> www.domainB.com
www.domainA.com/anything -> www.domainB.com/rebrand
How I can do this in htaccess, I have done following code but it redirecting to /rebrand/ only.
RewriteCond %{REQUEST_URI} ^\/
RewriteRule ^\/$ http://www.domainB.com/ [L,R=301]
RewriteCond %{HTTP_HOST} ^domainA\.com$ [NC]
RewriteRule ^(.*)$ http://www.domainB.com/rebrand/ [L,R=301]
URIs that go through rules in an htaccess file has the leading slash stripped off, so you can't match against it. For the second rule, it's matching the / request because the first rule isn't being applied and your regex matches anything or nothing, you can fix that by changing the * to +:
RewriteCond %{HTTP_HOST} ^domainA\.com$ [NC]
RewriteRule ^/?$ http://www.domainB.com/ [L,R=301]
RewriteCond %{HTTP_HOST} ^domainA\.com$ [NC]
RewriteRule ^(.+)$ http://www.domainB.com/rebrand/ [L,R=301]
Redirecting through htaccess is tricky sometimes, there are many ways of implementing this but there is one simple way which worked for me
Options +FollowSymLinks
RewriteEngine on
RewriteRule (.*) [newdomain.com...] [R=301,L]
You can get more info from webmaster world forum

.htaccess Redirection [force www except index.php]

I'm trying to do an htaccess redirection based on this condition :
Redirect all pages without "www" http://siteweb.com to http://www.siteweb.com/index.html
Except http://siteweb.com/index.php
The http://siteweb.com/index.php must to be redirected to http://www.siteweb.com/index.php
Actually i use this code [but something wrong on it :s]
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule !^/index.php$ http://www.%{HTTP_HOST}/$1 [R=301,L]
RewriteCond %{REQUEST_URI} ^/index\.php$ [NC]
RewriteRule ^(.*)$ http://%{HTTP_HOST}/$1 [R=301,L]
Thanks in advance.
You can use this code in your .htaccess:
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule (?!^index\.php$)^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,L,NC]
This will redirect all URIs except index.php to a domain with www. However your question is confusing because you also wrote that:
The http://siteweb.com/index.php must to be redirected To http://www.siteweb.com/index.php
Which tells me that may be you want to redirect every URI including index.php to a domain with www.

RewriteCond and RewriteRule in .htaccess

I have a client folder located at http://www.example.com/client
However, I've now installed SSL on the server, and want to add a permanent redirect using HTACCESS so that whenever /client is accessed, that it redirects to: https://www.example.com/client
Anybody know how to do that?
I've redirected my domains in the past like this:
RewriteCond %{HTTP_HOST} ^example\.com$ [NC]
RewriteRule ^(.*)$ http://www.example.com/$1 [L,R=301]
RewriteCond %{HTTP_HOST} ^www.example\.com$ [NC]
RewriteRule ^(.*)$ http://www.example.com/$1 [L,R=301]
This should not affect the solution, but the site must still redirect to www.example.com FIRST, and then to https://www.example.com/client if for example, http://www.example.co.za/client is entered.
Try this:
RewriteCond %{HTTPS} !on
RewriteRule ^client(/.*)?$ https://www.example.com%{REQUEST_URI} [L,R=301]
RewriteEngine On
RewriteRule ^/?$ https://www.example.com/client [301,NC,L]
It tells the apache, whenever the url is https://www.example.com or either with slash at the end, will redirect to ur /client