Htaccess Domain Redirects, How Do I Persist Current Page? - apache

I have numerous domains, the main domain is example.com, and the addon domains are example1.com, example2.com, example3.com.
I would like these domains to all redirect to the main domain, example.com.
I can do this with the following code:
RewriteCond %{HTTP_HOST} ^example1.com$ [OR]
RewriteCond %{HTTP_HOST} ^www.example1.com$
RewriteRule ^/?$ "http://example.com" [R=301,L]
This would redirect example1.com to example.com, simple.
However, if I type in example1.com/products, the URL does not change, no redirection occurs.
How can I make these addon domains redirect to the main domain and keep the users current location (web directory)?

Clear your browser cache and try :
RewriteEngine On
RewriteCond %{HTTP_HOST} ^(www\.)?example1\.com$ [NC]
RewriteRule ^(.*)$ http://example.com/$1 [NC,L,R]

RewriteCond %{HTTP_HOST} example1.com [OR]
RewriteCond %{HTTP_HOST} www.example1.com
RewriteRule (.*) "http://example.com/$1" [R=301,L]

Related

.htacess existing URL from old domain, redirects to index.php on new domain

Problem
I have a new TYPO3 website on a new domain. All important pages of the old website point to their new counterpart for which I added individual 301 redirects in the .htaccess. Those work fine!
However there are pages which have the exact same URL as the old website, which I cannot add individual redirects for (infinite loop). For example /contact/. When I access http://www.example-old.com/contact/ the page simply redirects to /index.php. Why?
Wishlist
All non-www should be redirected to www (works)
Other domains should be redirected to new (works)
Referrals from the old website, should be redirected to the same URL on the new domain, unless a manual 301 redirect is in place. (does not work)
Current setup
This is the .htacess snippet I'm working with:
<IfModule mod_rewrite.c>
# Enable URL rewriting
RewriteEngine On
RewriteBase /
# Point old domain to new domain
RewriteCond %{HTTP_HOST} ^example-old\.com$ [NC,OR]
RewriteCond %{HTTP_HOST} ^example-new\.com$ [NC]
RewriteRule ^(.*) http://www.example-new.com/$1 [R=301,L]
# Point idle domains to new domain
RewriteCond %{HTTP_HOST} ^/?(?:www\.)?example-01.nl [OR]
RewriteCond %{HTTP_HOST} ^/?(?:www\.)?example-02.de [OR]
RewriteCond %{HTTP_HOST} ^/?(?:www\.)?example-03.eu [OR]
RewriteCond %{HTTP_HOST} ^/?(?:www\.)?example-04.net [OR]
RewriteCond %{HTTP_HOST} ^/?(?:www\.)?example-05.org
RewriteRule ^(.*)$ http://www.example-new.com/$1 [R=301,L]
</IfModule>
I have tried every combination I could find to try and get this to work. What could be going wrong here?
Many Thanx!
Try to remove the leading "/?" and the inner "?:" also escape the last dot from your http host conditions:
RewriteCond %{HTTP_HOST} ^(www\.)?example-01\.nl$ [OR]
RewriteCond %{HTTP_HOST} ^(www\.)?example-02\.de$ [OR]
RewriteCond %{HTTP_HOST} ^(www\.)?example-03\.eu$ [OR]
RewriteCond %{HTTP_HOST} ^(www\.)?example-04\.net$ [OR]
RewriteCond %{HTTP_HOST} ^(www\.)?example-05\.org$
RewriteRule ^(.*)$ http://www.example-new.com/$1 [R=301,L]
If you remove the ^ from the first RewriteCond all request to the old domain will be redirected to the new domain.
If you want the specific redirects to have priority make sure you put them before this one.
# Point old domain to new domain
RewriteCond %{HTTP_HOST} example-old\.com$ [NC,OR]
RewriteCond %{HTTP_HOST} ^example-new\.com$ [NC]
RewriteRule ^(.*) http://www.example-new.com/$1 [R=301,L]

.htaccess 301 redirect issue with https Urls

we want to redirect our .com domain name to .net domain, HTTP work for us, but when user use HTTPS path, they redirect to 404 page of .com domain. Please check our code and help me, how to redirect .com domain HTTP and HTTPS both user to some other .net domain.
RewriteEngine on
RewriteCond %{HTTP_HOST} ^domain\.com$ [OR]
RewriteCond %{HTTP_HOST} ^www\.domain\.com$
RewriteRule ^(.*)$ "http\:\/\/domain\.net\/$1" [R=301,L]
Replace your rule by this rule:
RewriteEngine on
RewriteCond %{HTTP_HOST} ^(www\.)?domain\.com$ [NC]
RewriteCond %{HTTPS}s on(s)|
RewriteRule ^ http%1://domain.net%{REQUEST_URI} [L,R=301,NE]

.htaccess rewrite but keep start URL

I have main domain, for example, domain1.com. Also I have domain2.com which is addon domain, and I want to redirect domain2.com to domain.com?parameter=value, which is basically domain1.com, but with another template, but that path in URL stays same, domain2.com, not domain1.com. .htaccess file is created inside addon domain directory (public_html/domain2.com), and I have these two lines:
RewriteCond % ^domain2.com
RewriteRule ^(.*) http://domain1.com?parameter=value [P]
Rewrite works, but URL in browser changes to domain1.com, also I need to rewrite all request, that comes to domain2.com and www.domain2.com. Now only request that com to non-www domain works, but URL in browser changes.
EDIT: U have this code to .htacces, but URL change to domain1.com, instead to stay domain2.com.
RewriteCond %{HTTP_HOST} ^domain2\.com$ [OR]
RewriteCond %{HTTP_HOST} ^www\.domain2\.com$
RewriteRule ^/?$ "http\:\/\/domain1\.com\/\?parameter\=value" [R=301,L]
Try this in your domain2/.htaccess
RewriteEngine on
RewriteBase /
RewriteCond %{HTTP_HOST} ^domain2\.com$ [NC]
RewriteRule (.*) http://www.domain2.com/$1 [R,L,QSA]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)/?$ http://domain1.com/$1 [R,QSA,NC]

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/

Apache SSL Rewrite with Wildcard Subdomains

I'm attempting to setup an SSL redirect in Apache using RewriteEngine that will do the following:
Redirect traffic to either http://mydomain.com or http://www.mydomain.com to use HTTPS
Redirect traffic to any other subdomain https://*.mydomain.com to use HTTP instead of HTTPS
My reasoning for this is that I'm developing a project that's using a free SSL certificate until launch. This certificate covers the base domain, but none of the wildcard subdomains, and it's a pain to need to bypass the warning every time I visit one of the subdomains.
Edit:
I believe I'm close here, but I still can't get the HTTPS to HTTP redirect to work properly.
RewriteEngine on
# Redirect domain and www to HTTPS
RewriteCond %{HTTPS} off
RewriteCond %{HTTP_HOST} =mydomain.com [or]
RewriteCond %{HTTP_HOST} =www.mydomain.com
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
# Redirect wildcard subdomains to HTTP
RewriteCond %{HTTPS} on
RewriteCond %{HTTP_HOST} ^(.+)\.mydomain\.com$ [NC]
RewriteRule (.*) http://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
Figured it out. By moving these rules from the sites-available/default file to an .htaccess inside of the website root, I was able to get this working properly.
RewriteEngine on
# Redirect domain and www to HTTPS
RewriteCond %{HTTPS} off
RewriteCond %{HTTP_HOST} =mydomain.com [or]
RewriteCond %{HTTP_HOST} =www.mydomain.com
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
# Redirect wildcard subdomains to HTTP
RewriteCond %{HTTPS} on
RewriteCond %{HTTP_HOST} ^(.+)\.mydomain\.com$ [NC]
RewriteCond %{HTTP_HOST} !=www.mydomain.com
RewriteRule (.*) http://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]