htaccess subdomain silent redirect - apache

I have subdomains for each user, user1.domain.com that successfully points to domain.com/user1.
My question is how do I do it so that http://user1.domain.com/admin does a SILENT redirect to http://domain.com/admin?
I want to apply this to all subdirectories in the subdomain, so user1.domain.com/cart silent redirects to domain.com/cart & user1.domain.com/cart/checkout/confirmation -> domain.com/cart/checkout/confirmation so on and so forth. Bear in mind that this directories don't actually exist in the subdomains, only the root domain.
I've tried searching Stackoverflow but could not find an answer to my specific issue. Thanks!

It's not a redirect unless the browser knows about it, there's no such thing as a "silent" redirect. You have 2 options, you can either proxy on behalf of the browser, or you can internally rewrite the request if both user1.domain.com and domain.com share the same document root.
To proxy:
RewriteEngine On
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteCond %{HTTP_HOST} ^([^.]+)\.domain\.com$ [NC]
RewriteRule ^(.*)$ http://domain.com/%1/$1 [L,P]
The P flag indicates a proxy. This rule will not work unless you have mod_proxy loaded. If all your domains point to the same document root, then you can simply rewrite:
RewriteEngine On
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteCond %{HTTP_HOST} ^([^.]+)\.domain\.com$ [NC]
RewriteRule ^(.*)$ /%1/$1 [L]

Related

Rewriting sub-domain leads to a HTTP redirect rather

What I'm trying to do here is rewrite my https://demo.example.com/ sub-domain to my https://example.com/ domain (in another folder), without changing the URL. However, the sub-domain keeps on redirecting me to the folder. I'm not great at HTACCESS and rewriting, but this should be an easy task to do.
This is my current HTACCESS file:
RewriteEngine On
RewriteRule ^license/?(.*)$ /sites/site-licenses/$1.php [L]
RewriteRule ^portfolio/?(.*)$ /sites/site-portfolios/$1.php [L]
RewriteCond %{HTTP_HOST} ^demo\.hamiltondev\.net
RewriteRule ^(.*)$ https://hamiltondev.net/sites/site-demos/$1 [L,NC,QSA]
The first two rewrite rules is for the main domain https://example.com/license/--- and https://example.com/portfolio/---.
When a full url is used as a target in a RewriteRule, it automatically becomes a redirect. What you need is either use an absolute path (if demo and main domain share the same document root folder) or use a proxy request.
both domains share document root folder
RewriteCond %{HTTP_HOST} ^demo\.hamiltondev\.net$ [NC]
RewriteRule ^(.*)$ /sites/site-demos/$1 [L,NC,QSA]
otherwise, proxy
RewriteCond %{HTTP_HOST} ^demo\.hamiltondev\.net$ [NC]
RewriteRule ^(.*)$ https://hamiltondev.net/sites/site-demos/$1 [P,NC,QSA]

.htaccess redirect rules to www and htpps but to exclude subdomain

After reading all relevant answers here regarding .htaccess and redirects, and some experimentation with .htaccess rewrite conditions and rules my problem persists.
I managed to force www and https for my Magento site. Here is what I have at the moment:
RewriteCond %{HTTPS} off
RewriteRule .* https://www.example.com%{REQUEST_URI} [L,R=301]
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule .* https://www.example.com%{REQUEST_URI} [L,R=301]
After creating the subdomain test.example.com and a test environment at public_html/test/ I want to exclude it from the above rules since the subdomain will neither have a www of a https.
I tried to put this exception in the rules above but with no success.
RewriteCond %{HTTP_HOST} !^test\.example\.com$
For example, when I type http://test.example.com/admin/ to enter the Magento admin, it redirects me to https://www.example.com/admin/ Do I have to also edit the public_html/test/.htaccess file ?
Thank you in advance
Yes, this is happening because of your Rules, the 2nd rule checks if the host value doesn't start with www the redirect the host to www.example.com this rule also redirects any non-www http host to the main domain with www.
To fix this, you can use a single rule to redirect http to https:www excluding the subdomain
RewriteEngine on
RewriteCond %{HTTP_HOST} !^test\.example\.com$
RewriteCond %{HTTPS} off
RewriteCond %{HTTP_HOST} ^(?:www\.)?(.+)$
RewriteRule ^ https://www.%1%{REQUEST_URI} [NE,L,R]

RewriteCond for a folder only on a specific domain extension

I have a site that can be accessed via 3 domains (domain.com, domain.ch, domain.fr). The three use exactly the same files and folder, though.
Regarding the .fr domain (and only this domain), I need the following:
redirect domain.fr (root) to domain.com/fr/france.
This has been achieved with the following rule:
RewriteCond %{HTTP_HOST} ^www\.domain\.fr$
RewriteRule ^/?$ "http\:\/\/www\.domain\.com\/fr\/france" [R=301,L]
It works. (There's also a functioning rule to force www. in front of every URL.)
What I can't get to work is:
redirect domain.fr/fr also to domain.com/fr/france.
and finally, redirect any URL domain.fr/fr/* to domain.com/fr/*
(keeping whatever * stands for).
The trick (to me) is that the same .htaccess file will also be present on domain.com and domain.ch, but those rules must not activate for those domains.
You can put these rules in your htaccess
# Redirect [www.]domain.fr and [www.]domain.fr/fr to www.domain.com/fr/france
RewriteCond %{HTTP_HOST} ^(www\.)?domain\.fr$ [NC]
RewriteRule ^(|fr)$ http://www.domain.com/fr/france [R=301,L]
# Redirect domain.fr/fr/* to domain.com/fr/*
RewriteCond %{HTTP_HOST} ^(www\.)?domain\.fr$ [NC]
RewriteRule ^fr/(.*)$ http://www.domain.com/fr/$1 [R=301,L]
Try :
RewriteCond %{HTTP_HOST} ^www\.domain\.fr$
RewriteCond %{REQUEST_URI} !^/fr/france
RewriteRule ^fr/(.*)$ http://www.domain.com/fr/france/$1 [R=301,L]

Redirect a.com/a.html to b.com/a.html

I need to redirect a.com/a.html to b.com/a.html htaccess Apache. I have several subdomains that are not being redirected to the b.com(which is fine). and the example below is only the first domain that needs to be redirected. Three other domains are needed to be redirected to b.com as well with all of their subpages as a.com/a.html to b.com/a.html. Any hints?
RewriteCond %{HTTP_HOST} ^(www\.)?nexus21\.com$ [NC,OR]
RewriteCond %{HTTP_HOST} ^tvlift\.com$ [NC]
RewriteRule ^ http://www.tvlift.com%{REQUEST_URI} [L,NE,R=301]
I would do each subdomain redirect separately. That way it is cleaner and more maintainable. If you are redirecting few known files then name them directly.
RewriteEngine on
RewriteRule "^/foo\.html$" "bar.html" [R]
If there are lots of file or you will be adding in future then use a pattern
RewriteEngine on
RewriteRule "^/docs/(.+)" "http://new.example.com/docs/$1" [R,L]
See this page
http://httpd.apache.org

Apache redirect subdomain to folder, process folder as subdomain

It may looks complicated but I need to change the structure of an existing website to bring it to a more seo friendly level. Current structure is subdomain oriented.
ex: sub.example.com/page/var/var2/...
I need to merge all sub-sites under the same top level domain.
ex: www.example.com/sub/page/var/var2/...
This part is already working for me using this:
# If empty subdomain, redirect to "www"
RewriteCond %{HTTP_HOST} ^example.com$
RewriteRule ^(.*) http://www.example.com/$1 [QSA,L,R=301]
# If not empty subdomain and not "www", redirect to directory
RewriteCond %{HTTP_HOST} !^www\.example\.com
RewriteCond %{HTTP_HOST} ^(.*)\.example\.com$
RewriteRule (.*) http://www.example.com/%1/$1 [QSA,R=301]
Obviously, as the folder not exists, I get "Not found" error.
This is the behavior I try to reach:
In the address bar, displays
www.example.com/sub/page/var/var2/...
but seamlessly process it as
sub.example.com/page/var/var2/...
Any idea with htaccess ?
http://www.website.com/page.html
RewriteEngine On
RewriteCond %{HTTP_HOST} !oldexample.com$ [NC]
RewriteRule ^(.*)$ http://www.newexample.com/$1 [L,R=301]
all sub-sites under the same top level domain.