I can't seem to get my head around configuring an .htaccess file that sends to different control pages based on the subdomain, then uses wildcards for user subdomains.
What I need is essentially this:
If domain.com, redirect to www.domain.com/index.php
If www.domain.com, rewrite to www.domain.com/index.php
If img.domain.com, rewrite to www.domain.com/img/
If api.domain.com, rewrite to www.domain.com/api/index.php
If admin.domain.com, rewrite to www.domain.com/admin/index.php
If (anything else).domain.com, rewrite to www.domain.com/users/index.php - also sending the value of the subdomain as a variable.
All of the tutorials I've seen send all requests to a single page which is then routed, I would like the htaccess file to do some of the work beforehand.
If your subdomains are not pointing to same documentroot's main domain (you will have to enable mod_proxy)
RewriteEngine on
# 1. www-rule: domain.com to www.domain.com
RewriteCond %{HTTP_HOST} ^domain\.com$ [NC]
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,L]
# 2. skip www.domain.com
RewriteCond %{HTTP_HOST} ^www\. [NC]
RewriteRule . - [L]
# 3/4/5. manage img, api and admin subdomains
RewriteCond %{HTTP_HOST} ^(img|api|admin)\. [NC]
RewriteRule ^(.*)$ http://www.domain.com/%1/$1 [P]
# 6. manage users
RewriteCond %{HTTP_HOST} ^(.*)\.domain\.com$
RewriteRule ^(.*)$ http://www.domain.com/users/index.php?user=%1 [P]
If your subdomains are pointing to same documentroot's main domain (mod_proxy is not needed here)
RewriteEngine on
# 1. www-rule: domain.com to www.domain.com
RewriteCond %{HTTP_HOST} ^domain\.com$ [NC]
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,L]
# 2. skip www.domain.com
RewriteCond %{HTTP_HOST} ^www\. [NC]
RewriteRule . - [L]
# 3/4/5. manage img, api and admin subdomains
RewriteCond %{HTTP_HOST} ^(img|api|admin)\. [NC]
RewriteRule ^(.*)$ /%1/$1 [L]
# 6. manage users
RewriteCond %{HTTP_HOST} ^(.*)\.domain\.com$
RewriteRule ^(.*)$ /users/index.php?user=%1 [L]
EDIT:
RewriteEngine on
# 1. www-rule: redirect domain.com to www.domain.com
RewriteCond %{HTTP_HOST} ^domain\.com$ [NC]
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,L]
# 2. www.domain.com rewritten to index.php
RewriteCond %{HTTP_HOST} ^www\. [NC]
RewriteRule . /index.php [L]
# 3. img.domain.com rewritten to img folder
RewriteCond %{HTTP_HOST} ^img\. [NC]
RewriteRule ^(.*)$ /img/$1 [L]
# 4/5. api or admin subdomains rewritten to /api/index.php or /admin/index.php
RewriteCond %{HTTP_HOST} ^(api|admin)\. [NC]
RewriteRule . /%1/index.php [L]
# 6. manage users
RewriteCond %{HTTP_HOST} ^(.+)\.domain\.com$
RewriteRule . /users/index.php?user=%1 [L]
Related
There are 10 different domains names. I would like to redirect some of them to:
somedomain1.com, somedomain2.com -> somenewdomainONE.com/en
somedomain3.com, somedomain4.com -> somenewdomainTWO2.com/fr
I have tried this:
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} ^(www.)?somedomain1.com$ [NC, OR]
RewriteCond %{HTTP_HOST} ^(www.)?somedomain2.com$ [NC]
RewriteRule ^(.*)$ http://www.newdomainONE.com/en [L,R=301]
RewriteCond %{HTTP_HOST} ^(www.)?somedomain3.com$ [NC, OR]
RewriteCond %{HTTP_HOST} ^(www.)?somedomain4.com$ [NC]
RewriteRule ^(.*)$ http://www.somenewdomainTWO.com/fr [L,R=301]
I'm not sure why, but this redirects all domains to http://www.newdomainONE.com/en even when I try accessing somedomain3 or somedomain4.com. I'm also getting a redirect loop depending on what I try.
You can just use:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^(www\.)?(domain1|domain2)\.com$ [NC]
RewriteRule ^(.*)$ http://www.newdomainONE.com/en/$1 [L,R=301]
RewriteCond %{HTTP_HOST} ^(www\.)?(domain3|domain4)\.com$ [NC]
RewriteRule ^(.*)$ http://www.newdomainTWO.com/fr/$1 [L,R=301]
Make sure to test in a new browser to avoid old 301 cache.
I have a primary domain "example.co.uk" and 6 other domains + 1 IP address pointed to the same folder root.
I would like to use HTTPS for the primary domain i.e. https://www.example.co.uk and redirect all the other domains to the homepage of this domain (avoiding SEO issues and content duplication with the example.com domain and IP address)
How do I achieve this using my current .htaccess file?
RewriteEngine on
#Redirect IP address to example.co.uk.
RewriteCond %{HTTP_HOST} ^12\.34\.567\.890
RewriteRule (.*) https://www.example.co.uk/$1 [R=301,L]
# Redirect example.com to example.co.uk.
RewriteCond %{HTTP_HOST} ^(www\.)?example\.com$ [NC]
RewriteRule ^(.*)$ https://www.example.co.uk/$1 [R=301,L]
# Redirect example2.co.uk to example.co.uk.
RewriteCond %{HTTP_HOST} ^(www\.)?example2\.co\.uk$ [NC]
RewriteRule ^(.*)$ https://www.example.co.uk/$1 [R=301,L]
# Redirect example3.co.uk to example.co.uk.
RewriteCond %{HTTP_HOST} ^(www\.)?example3\.co\.uk$ [NC]
RewriteRule ^(.*)$ https://www.example.co.uk/$1 [R=301,L]
# Redirect example4.com to example.co.uk.
RewriteCond %{HTTP_HOST} ^(www\.)?example4\.com$ [NC]
RewriteRule ^(.*)$ https://www.example.co.uk/$1 [R=301,L]
# Redirect example5.co.uk to example.co.uk.
RewriteCond %{HTTP_HOST} ^(www\.)?example5\.co\.uk$ [NC]
RewriteRule ^(.*)$ https://www.example.co.uk/$1 [R=301,L]
# Redirect example6.com to example.co.uk.
RewriteCond %{HTTP_HOST} ^(www\.)?example6\.com$ [NC]
RewriteRule ^(.*)$ https://www.example.co.uk/$1 [R=301,L]
You can reduce the number of rules by negating the condition check against the HTTP HOST. Then, you'll just need the rule to check HTTPS:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^(www\.)?example\.co\.uk$ [NC]
RewriteCond %{HTTPS} off
RewriteRule ^(.*)$ https://www.example.co.uk/$1 [L,R=301]
# redirect all the other domains to the homepage
RewriteCond %{HTTP_HOST} !^www\.example.co.uk$ [NC]
RewriteRule ^(.*)$ https://www.example.co.uk/ [L,R=301]
In your question, you said you wanted to redirect all the other domains to the homepage of this domain, but your rules aren't doing that at all. Your rules are redirecting the request to the same request but just this domain. If you want to keep that logic, then you need to add a $1 to the end of the https://www.example.co.uk/.
i have several domains (same name, but different endings) for the different languages. the .com domain is the main domain and has for each language a directory like /en/
now i want to redirect each of these domains ( e.g. http://example.us/ ) to http://example.com/en/
is this possible with the .htaccess file?
actual i only have a redirection from www.example.com to http://example.com
RewriteEngine On
RewriteCond %{HTTP_HOST} ^www\.example\.com$ [NC]
RewriteRule ^(.*)$ http://example.com/$1 [L,R=301]
You can do it this way
RewriteEngine On
RewriteCond %{HTTP_HOST} ^www\.example\.com$ [NC]
RewriteRule ^(.*)$ http://example.com/$1 [L,R=301]
RewriteCond %{HTTP_HOST} \.(at|ch)$ [NC]
RewriteRule ^(.*)$ http://example.com/de/$1 [L,R=301]
RewriteCond %{HTTP_HOST} \.us$ [NC]
RewriteRule ^(.*)$ http://example.com/en/$1 [L,R=301]
RewriteCond %{HTTP_HOST} \.fr$ [NC]
RewriteRule ^(.*)$ http://example.com/fr/$1 [L,R=301]
But if you have more domains (maybe a lot) you could use RewriteMap
I need to know how to redirect example.com/folder/ to www.example.com/folder/ using htaccess. At moment htaccess redirects example.com/folder/ to www.example.com. This is what htaccess says at moment:
RewriteCond %{HTTP_HOST} ^www.example.com$
RewriteRule . - [E=REWRITEBASE:/]
RewriteRule ^api/?(.*)$ %{ENV:REWRITEBASE}webservice/dispatcher.php?url=$1 [QSA,L]
RewriteCond %{HTTP_HOST} ^www.example.com$
RewriteRule ^folder$ /folder/ [L,R]
RewriteCond %{HTTP_HOST} ^www.example.com$
RewriteRule ^folder/(.*) /$1 [L]
What changes are needed?
Just use this condition:
RewriteCond %{HTTP_HOST} !^$
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteCond %{HTTPS}s ^on(s)|
RewriteRule ^ http%1://www.%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
First check that the host value is not empty (may be with HTTP/1.0).
The check if we already "use" www and then chrck for SSL (https)
If you want to keep subdomains intact, you should change the second line to this (heads up this is not 100% generic since you have to hardcode in your domain!):
RewriteCond %{HTTP_HOST} ^example.com [NC]
I have set up virtual subdomain. and i want to make htaccess to achive the following flow
For
www.domain.com
it should call the index file but for the subdomain
abc.domain.com
The rewrite rule should be like
RewriteRule www.domain.com/index.php?var=abc
i mean it should (in HTACCESS) pass the subdomain to the index file as an argument
and for the Other file requests like
abc.domain.com/file.php
The subdomain should be rewritten like www.domain.com/file.php?var=abc
I mean the Rewrite rule like
RewriteRule www.domain.com/file.php?var=abc
This would be how you could do them on an individual basis.
RewriteCond %{HTTP_HOST} ^abc\.domain\.com$ [NC]
RewriteRule (.*)$ http://www.domain.com/file.php?var=abc [L]
RewriteCond %{HTTP_HOST} ^def\.domain\.com$ [NC]
RewriteRule (.*)$ http://www.domain.com/file.php?var=def [L]
The solution is something like this
RewriteCond %{HTTP_HOST} !^www\.domain\.com$ [NC]
RewriteCond %{HTTP_HOST} ^(.*)\.domain\.com$ [NC]
RewriteCond %{QUERY_STRING} ^$
RewriteRule ^(.*)$ http://www.domain.com/index.php?subdomain=%1 [L]
RewriteCond %{HTTP_HOST} !^www\.domain\.com$ [NC]
RewriteCond %{HTTP_HOST} ^(.*)\.domain\.com$ [NC]
RewriteCond %{QUERY_STRING} !^$
RewriteRule ^(.*)$ http://www.domain.com%{REQUEST_URI}?%{QUERY_STRING}&subdomain=%1 [L]