htaccess for rewriting only subdomain - apache

I am trying to make a redirect for a subdomain but i can't figure it out.
This what I have now:
RewriteCond %{HTTP_HOST} ^(mysubdomain).myhost.com$ [NC]
RewriteRule (.*) http://www.myhost.com/folder/index.html [R=301,L]
But when I go to the subdomain address in my browser, there's no redirect.
I need this for only one specific subdomain.
Where am I going wrong?

Should be:
RewriteEngine on
RewriteCond %{HTTP_HOST} ^sub.domain.com$ [NC]
RewriteRule ^.*$ http://www.domain.com/folder/ [R=301,L]

Try this, since you only need it from one domain. The () are pointless in both cases. Especially on the 2nd line where you aren't passing the old URL to the new URL.
RewriteCond %{HTTP_HOST} ^www.old-domain.com$ [NC]
RewriteRule ^.*$ http://www.myhost.com/folder/index.html [R=301,L]

Related

htaccess http to https with www. Without redirecting sub domain

I have a RewriteRule that redirects my main domain to https://www.sta-games.com which works fine, but when I try to access my subdomain http://files.sta-games.com, it redirects to my main domain.
Heres my redirect rules
#HTTPS Redirection
RewriteCond %{HTTPS} off
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule ^(.*)$ https://www.%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
Can anyone see the problem?
You need to adjust your rules so that it looks for the whole domain instead of just part of it. Right now you are only looking for the absence of www. So that is why your subdomain redirects.
First you need to combine your rules because it seems you want to redirect if https is not on and there is no www, so make that one rule. Then use your actual domain name in the rule. Replace example.com with your domain. This should fix your issue.
#HTTPS Redirection
RewriteCond %{HTTPS} off [OR]
RewriteCond %{HTTP_HOST} !^(www\.)?example\.com
RewriteRule ^(.*)$ https://www.example.com%{REQUEST_URI} [L,R=301]
Have an additional skip condition for all domains except the main domain:
RewriteCond %{HTTP_HOST} ^(www\.)?sta-games\.com$ [NC]
RewriteCond %{HTTPS} off [OR]
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule ^ https://www.sta-games.com%{REQUEST_URI} [L,R=301,NE]
Test this after clearing your browser cache.

.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.

Redirect Subdomain to new domain

Hi guys trying to get a 301 redirect working and having trouble. I need to redirect sub.domain1.com to www.domain2.com and make sure that any file names or parameters get sent over with it.
This is what I was trying:
RewriteCond %{HTTP_HOST} ^domain1.com [NC]
RewriteRule ^(.*)$ http://www.domain2.com/$1 [L,R=301]
I also tried this:
RewriteCond %{QUERY_STRING} ^$
RewriteRule ^sub\.domain1\.com$ /www.domain2.com? [R=301,NE,NC,L]
Where am I messing up?
You missed the subdomain part and proper escaping.
RewriteCond %{HTTP_HOST} ^sub\.domain1\.com$ [NC]
RewriteRule ^(.*)$ http://www.domain2.com/$1 [L,R=301]
Further explain can be found in this question.
Rule of thumb for rewriterules: from the most complex to the less complex.
And don't forget the QSA directive (QSA = Query String Append = "make sure that any file names or parameters get sent over with it")
RewriteCond %{HTTP_HOST} ^sub\.domain1\.com$ [NC]
RewriteRule ^(.*)$ http://www.domain2.com/$1 [QSA,R=301,L]
Tell me if it works.

.htaccess rewrite subdirectory to a different subdirectory on a different domain

I'm trying to configure a redirect for the domain kb3mmy.net. I'd like it to work that if somebody goes to kb3mmy.net/blog it redirects them to christiaanconover.com/category/kb3mmy/. This is the only redirect I want to take place like this, so I can hard code the subdirectories.
Also, there's an existing redirect rule set up on kb3mmy.net that sends any traffic to the root of that domain to a sub page on christiaanconover.com, but any sub page traffic to kb3mmy.net to the corresponding sub page on christiaanconover.com, so that rule cannot be affected by the new one. Here's the code for the existing rule:
RewriteRule ^$ http://christiaanconover.com/kb3mmy/ [L,R=301]
RewriteRule ^(.*)$ http://christiaanconover.com/$1 [R=301]
Any help is appreciated.
RewriteRule ^blog/?(.*)$ http://christiaanconover.com/category/kb3mmy/ [R=301,L]
RewriteRule ^$ http://christiaanconover.com/kb3mmy/ [L,R=301]
RewriteRule ^(.*)$ http://christiaanconover.com/$1 [R=301,L]
How about:
RewriteCond %{HTTP_HOST} kb3mmy.net
RewriteRule ^blog/?(.*)$ http://christiaanconover.com/category/kb3mmy/ [R=301,L]
RewriteCond %{HTTP_HOST} kb3mmy.net
RewriteRule ^$ http://christiaanconover.com/kb3mmy/ [L,R=301]
RewriteCond %{HTTP_HOST} kb3mmy.net
RewriteRule ^(.*)$ http://christiaanconover.com/$1 [L,R=301]

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