htaccess: exluce some domain in RewriteCond - apache

this is my .htaccess code
so if the user type just domain.com will be redirected to www.domain.com
RewriteBase /
RewriteCond %{HTTP_HOST} !^www.domain.com$ [NC]
RewriteRule ^(.*)$ http://www.domain.com/$1 [L,R=301]
my problem now is that i have a new domain pointing to the same domain path
so even the new domain is redirected "transparently" to domain.com...
how can i exclude some domain name from that rule?
thanks!

You could try making the rewrite generic, so all requests that are not starting with www are redirected, but on the correct/requested domain.
RewriteCond %{HTTP_HOST} !^www\.(.*)$ [NC]
RewriteRule ^(.*)$ http://www.%1/$1 [L,R=301]
Or, you can check instead for if the domain starts with domain.com:
RewriteCond %{HTTP_HOST} ^domain\.com$ [NC]
RewriteRule ^(.*)$ http://www.domain.com [L,R=301]
Hopefully this helps.

Related

Redirect all subdomain to main domain except one selected

If I want to redirect all subdomains to a main domain, except one subdomain that I need redirect to a subfolder. Is there any way?
Redirect selected subdomain to subfolder
Redirect all subdomains to main domain
This is my Apache configuration:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^mySubdomain\.domain\.com$ [NC]
RewriteRule ^ http://domain.com/mySubdomain [L,R]
RewriteCond %{HTTP_HOST} !^domain\.com$ [NC]
RewriteRule (.*) http://domain.com/$1 [L,R=301,QSA]
The first rule is correct to serve a particular subdomain from a directory. For the second rule just add 1 AND condition that if the current request is not for domain.com AND not for mySubdomain.domain.com then redirect request to domain.com as follows:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^mySubdomain\.domain\.com$ [NC]
RewriteRule ^(.*)$ http://domain.com/mySubdomain/$1 [R]
RewriteCond %{HTTP_HOST} !^domain\.com$ [NC]
RewriteCond %{HTTP_HOST} !^mySubdomain\.domain\.com$ [NC]
RewriteRule ^(.*)$ http://domain.com/$1 [R=301]

Redirect subdomain to main domain in Joomla

I need to redirect all subdomains of my website to the main domain starting with www:
I have tried to add this code to my .htaccess:
RewriteBase /
RewriteCond %{HTTP_HOST} !^domain.com$ [NC]
RewriteRule ^(.*)$ http://www.domain.com/$1 [L,R=301]
but I have a redirect loop error.
What do you suggest?
Thanks
Your rule will indeed cause redirect loop. Have it like this:
RewriteEngine On
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [L,R=301,NE]
If you want demo.domain.com to be redirected to www.domain.com:
RewriteEngine On
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule ^(.*)$ http://www.domain.com/$1 [L,R=301,NE]
I'm using this code in my .htaccess file at the root directory of the domain or subdomain
Redirect all requests from subdomain:
Redirect / http://www.example.com/
Redirect all requests from folder sub on the domain to the subdomain:
Redirect /sub http://sub.example.com/

Properly redirecting a subdomain to subdirectory invisibly

I know similar things have been asked before, but none of the solutions I've found have seemed to work out. I'm by far an expert when it comes to mod_rewrite and it's ilk, so apologies if I'm missing something obvious.
I am trying to invisibly redirect subdomains to an index.php file in a subdirectory; this file takes the value of the subdomain as part of the query string, which is working fine.
The problem I'm having is that now everything in this subdirectory is being redirected to the index.php file, which I don't want to happen.
This is what I have thus far:
RewriteEngine On
RewriteBase /
# User dashboards
RewriteCond %{HTTP_HOST} ^(.*)\.example\.com [NC]
RewriteRule ^.*$ app/index.php?user=%1 [L,NC,QSA]
What I'm looking for is a situation where http://subdomain.example.com/ will lead to /app/index.php?user=subdomain, but http://subdomain.example.com/assets/stylesheet.css will go to /app/assets/stylesheet.css.
Thanks in advance!
If i understood well your example, you can do it this way:
redirects example.com to www.example.com to avoid having "empty" subdomain
internally rewrites every root subdomains (except www) to /app/index.php?user=subdomain
internally rewrites other things with "app" prefix
Which is represented by this code
RewriteEngine on
# redirects example.com to www.example.com to avoid having "empty" subdomain
RewriteCond %{HTTP_HOST} ^example.com$
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,L]
# internally rewrites every root subdomains (except www) to /app/index.php?user=subdomain
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteCond %{HTTP_HOST} ^([^.]+)\. [NC]
RewriteRule ^/?$ /app/index.php?user=%1 [L,NC,QSA]
# internally rewrites other things with "app" prefix
RewriteCond %{THE_REQUEST} !app/
RewriteRule ^/?(.+)$ /app/$1 [L,NC,QSA]
EDIT: as you asked in below comments, here is how to manage also www subdomain
RewriteEngine on
# redirects example.com to www.example.com to avoid having "empty" subdomain
RewriteCond %{HTTP_HOST} ^example.com$
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,L]
# internally redirects www subdomain root to /site/index.php
RewriteCond %{HTTP_HOST} ^www\. [NC]
RewriteRule ^/?$ /site/index.php [L]
# internally rewrites every other root subdomains to /app/index.php?user=subdomain
RewriteCond %{HTTP_HOST} ^([^.]+)\. [NC]
RewriteRule ^/?$ /app/index.php?user=%1 [L,NC,QSA]
# internally rewrites other things with "app" prefix
RewriteCond %{THE_REQUEST} !app/
RewriteRule ^/?(.+)$ /app/$1 [L,NC,QSA]
Let add second rule to redirect assets to app/assets:
RewriteCond %{HTTP_HOST} ^(.*)\.example\.com [NC]
RewriteCond %{REQUEST_URI} !\.(css|js|png|jpg|gif)$ [NC]
RewriteRule ^.*$ app/index.php?user=%1 [L,QSA]
RewriteRule ^assets/(.*)$ app/assets/$1 [L,QSA]
or load all css/js/images directly from app:
RewriteCond %{HTTP_HOST} ^(.*)\.example\.com [NC]
RewriteRule ^.*\.(css|js|png|jpg|gif)$ app/$0 [NC, QSA]
RewriteRule ^.*$ app/index.php?user=%1 [L,QSA]
EDIT: Sorry, I haven't tested it before, so there is working example:
RewriteRule ^assets/(.*)$ app/assets/$1 [L,QSA]
RewriteCond %{HTTP_HOST} ^(.*)\.example\.com [NC]
RewriteRule !^app/assets/ app/index.php?user=%1 [L,QSA]

.htaccess redirection - two sharing websites

I have two websites that is actually the same where example.com shares all files from examples.com. So whatever changes made in exampples.com, example.com automatically gets updated. That means they have the same .htaccess file.
The problem is, I want to both sites redirects to non www to a www url. I got this:
RewriteEngine on
RewriteCond %{HTTP_HOST} ^example\.com$ [NC]
RewriteRule ^(.*)$ http://www.example.com/$1 [R=301,L]
RewriteCond %{HTTP_HOST} ^[^\.]+\.com$ [NC]
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,L]
This should do it:
RewriteEngine on
RewriteCond %{HTTP_HOST} ^example\.com$ [NC,OR]
RewriteCond %{HTTP_HOST} ^examples\.com$ [NC]
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,L]
Basically you're adding an OR condition to say if either example.com or examples.com doesn't begin with www. then add it to the respective domain name.
replace example.com with %{HTTP_HOST} to make your rules host independent

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