Change to www if not a subdomain - php

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]

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]

.HTACCESS subdomain redirecting

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.

How can I redirect www without moving to the homepage?

Well I'm using this code to redirect the domain and ip:
RewriteEngine On
rewritecond %{http_host} ^www.lucrebem.com.br [nc]
rewriterule ^(.*)$ http://lucrebem.com.br/$1 [r=301,nc]
RewriteRule ^(.*)['"$] /$1 [R=301,L]
RewriteCond %{HTTP_HOST} ^216\.245\.194\.194
RewriteRule (.*) http://lucrebem.com.br/$1 [R=301,L]
What it does is removing the www from the url, but the problem is, if an user hits www.mysite.com/thecategory/thearticle, he won't be redirected to mysite.com/thecategory/thearticle, he will be redirected to mysite.com/index.php
That's not what I'm trying to achieve and I believe this is affecting my rankings.
I would do it this way and you can combine you rules into one instead of separate rules.
RewriteEngine On
RewriteCond %{HTTP_HOST} ^www\.lucrebem\.com\.br [NC,OR]
RewriteCond %{HTTP_HOST} ^216\.245\.194\.194
RewriteRule ^ http://lucrebem.com.br%{REQUEST_URI} [R=301,L]
Here you go Buddy, this should remove the www and redirect it to domain + slugs:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^www.domain.com$ [NC]
RewriteRule ^(.*)$ http://domain.com/$1 [R=301,L]
<VirtualHost *:80>
ServerName www.example.com
Redirect permanent / http://example.com/
</VirtualHost>
or
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC]
RewriteRule ^(.*)$ http://%1/$1 [R=301,L]
Here is how I fixed it:
### Makes www Redirect to non-www without redirecting to index page
RewriteCond %{HTTP_HOST} ^www\.lucrebem\.com\.br
RewriteRule (.*) http://lucrebem.com.br/$1 [R=301,L]
And here is a Bonus for people who wants to redirect the Ip too:
#Redirects IP, working fine, don't change anything below
RewriteCond %{HTTP_HOST} ^216\.245\.194\.194
RewriteRule (.*) http://lucrebem.com.br/$1 [R=301,L]
:)

redirect folder subdomain to subdomain.domain.com in htaccess

I have a domain name domain.com and some folders inside it. When i am accessing the folders inside this domain i can access the folder like this.
http://www.domain.com/folder
I want to convert this url with htaccess to
http://www.folder.domain.com
I would like to have similar urls for all folders so its a wildcard entry.
Please tell me how can it be done with htaccess.
RewriteEngine on
RewriteCond %{HTTP_HOST} ^domain.com
RewriteRule ^(.*)$ http://subdomains.domain.com/$1 [L,NC,QSA]
You can use:
RewriteEngine on
RewriteCond %{HTTP_HOST} ^(?:www\.)?domain\.com [NC]
RewriteRule ^([^/]+)(/.*)?$ http://www.$1.domain.com$2 [R=301,L,NC]
RewriteCond %{HTTP_HOST} ^www\.(.+)\.domain\.com [NC]
RewriteRule ^(.*)$ /%1/$1 [L]
To redirect requests to www.example.com/blog to blog.example.com, try this :
RewriteEngine on
RewriteCond %{HTTP_HOST} ^(www\.)?example\.com$
RewriteRule ^blog/(.*)$ http://blog.example.com/$1 [L,QSA,R=301]
RewriteCond %{HTTP_HOST} ^blog\.example\.com$
RewriteCond %{REQUEST_URI} !^blog/
RewriteRule ^(.*)$ /blog/$1 [L,QSA]
For other you have to set dynamic name and pass as parameter, for simple blog you can take help of this question.

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]

Categories