.HTACCESS subdomain redirecting - php

I am having an issue with subdomain.. i have a subdomain content.domain.com
which i want to use for cookieless domain content..
but when i wrote it in website.. its automatically add full website url.. which i have added in .htaccess
can you please let me know how can i stop redirecting the subdomain ....
Please help......
here is code for htaccess for domain redicretion
RewriteEngine On
RewriteCond %{HTTPS} off
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} [R,L]
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule .* https://www.%{HTTP_HOST}%{REQUEST_URI} [L,R=301]

You can combine both rules into a single rule
Using a RewriteCond you can omit a subdomain.
Have it like this:
RewriteEngine On
RewriteCond %{HTTP_HOST} !^www\. [NC,OR]
RewriteCond %{HTTPS} !on
RewriteCond %{HTTP_HOST} ^(?!content\.domain\.com)(?:www\.)?(.+)$ [NC]
RewriteRule ^ https://www.%1%{REQUEST_URI} [R=301,L,NE]
Make sure you clear your browser cache while testing this change.

Related

how to redirect non www url become www. into htaccess

i just want to setting file .htaccess for redirect non www url to www. but the case is not common.
if the url like this https://<domain name>.com become https://www.<domain name>.com
i already set the code like this. but didn't work
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{HTTPS} off
RewriteCond %{HTTP_HOST} ^domain.com [NC]
RewriteCond %{HTTP_HOST} ^[^.]+\.[^.]+$
RewriteRule ^(.*)$ https://www.%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
</IfModule>
that code is working if we type just <domain name>.com in browser. but when someone type manualy on browser like this https://<domain name>.com i mean type manualy the https:// can it turn into https://www.<domain name>.com
so for this .htaccess i want to do result like this
if type
<domainName>.com it become https://www.<domainName>.com
www.<domainName>.com it become https://www.<domainName>.com
https://<domainName>.com it become https://www.<domainName>.com
http://<domainName>.com it become https://www.<domainName>.com
please help.
You may use this rule in site root .htaccess:
RewriteEngine On
# add www and turn on https in same rule
RewriteCond %{HTTP_HOST} !^www\. [NC,OR]
RewriteCond %{HTTPS} !on
RewriteCond %{HTTP_HOST} ^(?:www\.)?(.+)$ [NC]
RewriteRule ^ https://www.%1%{REQUEST_URI} [R=301,L,NE]
Try this:
# uniform host name
RewriteEngine On
RewriteCond %{HTTP_HOST} !^www\.example\.com$ [NC]
RewriteRule ^.*$ http://www.example.com$0 [R=301,L]
It includes a deep link forwarding.
You can try this
RewriteEngine On
RewriteCond %{HTTP_HOST} !=""
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteCond %{HTTPS}s ^on(s)|
RewriteRule ^ http%1://www.%{HTTP_HOST}%{REQUEST_URI} [R=301,L]

url-masking redirect http to https using .htaccess

I have created url-masking (url-mapping) on my portal.
Same php code are running with different urls.
My portal link is http://subdomain.domain.in (main-link) and client url is http://www.client-domain.com.
I want to redirect client URL HTTP to https using my .htaccess file.
Edit from comment:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^client-domain.in [NC]
RewriteRule ^(.*)$ https://%{SERVER_NAME}%{REQUEST_URI}/$1 [L,R=301]
RewriteCond %{HTTP_HOST} ^mydomain.in [NC]
RewriteRule ^(.*)$ http://%{SERVER_NAME}%{REQUEST_URI}/$1 [L,R=301]
you can use below code for the same
RewriteEngine On
RewriteCond %{HTTPS} off
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
it will redirect your url http to https.
My problem is solved now.
I have added this code in .htaccess, and client's url work with HTTPS.
RewriteCond %{HTTPS} off
RewriteCond %{HTTP_HOST} www.client-domain.com [NC]
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI}/$1 [R=301,L]
RewriteCond %{HTTPS} on
RewriteCond %{HTTP_HOST} ^my-domain.in [NC]
RewriteRule ^(.*)$ http://%{SERVER_NAME}%{REQUEST_URI}/$1 [L,R=301]

https non www redirect

I'm trying to get all my URLs to redirect to https:// without www.
If someone puts in www. or http:// or http:// they are redirected to https:// the problem comes if they put https://www. then it doesn't redirect to https://website
I'm using the following in my htaccess, any suggestions?
RewriteEngine on
RewriteCond %{HTTPS} off
RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC]
RewriteRule ^(.*)$ http://%1/$1 [R=301,L]
RewriteCond %{HTTPS} !on
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
This single rule can take care of both requirements:
RewriteCond %{HTTP_HOST} ^www\. [NC,OR]
RewriteCond %{HTTPS} off
RewriteCond %{HTTP_HOST} ^(?:www\.)?(.+)$ [NC]
RewriteRule ^ https://%1%{REQUEST_URI} [R=301,L,NE]
Make sure to remove both of you shown rules and test in a new browser to avoid old cache.

How to redirect domain with www or without www

I am using the following rewrite rule to redirect domain that has or has not www.
RewriteCond %{HTTP_HOST} !^www\.[NC]
RewriteCond %{HTTPS}s ^on(s)|
RewriteRule ^ https://www.%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
It works fine for any sub domain name like www.domainname.com/abc.php or domainname.com/abc.php. But not adding https in case of www.domainname.com or domainname.com.
I tried adding one more rule
RewriteCond %{HTTP_HOST} ^www\.domainname\.com$
RewriteRule ^https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
But, its not working.
Just use this. Replace example.com with your domain.
RewriteEngine on
RewriteCond %{HTTP_HOST} ^example\.com [NC,OR]
RewriteCond %{HTTPS} !^on
RewriteRule ^ https://www.example.com%{REQUEST_URI} [R=301,L]

Change to www if not a subdomain

Plot: I actually like www URL's but not when used with subdomain like www.whatever.example.com . First i was using two domains on my hosting account so I added a htaccess rewrite like below code.
RewriteEngine On
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,L]
Motive: Now after sometime I realised that I need a subdomain also pointed to my hosting account. But I don't want a ugly subdomain like www.whatever.example.com but at the sametime I want to redirect example.com users to www.example.com.
I am unable to figure out how to do that. Anyone here.
You can use this rule to only target your main domain:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^example\.com$ [NC]
RewriteRule ^ http://www.%{HTTP_HOST}%{REQUEST_URI} [R=301,L,NE]
Or else, to target any sub-URL down to 3 levels 'deep':
RewriteEngine On
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteCond %{HTTP_HOST} !^[^.]+\.[^.]+\.[^.]+$
RewriteRule ^ http://www.%{HTTP_HOST}%{REQUEST_URI} [R=301,L,NE]
Try this using .htaccess,
Ref
RewriteEngine On
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,L]

Categories