.htaccess custom URL forwarding rules using pattern matching - regex

I have been attempting (without success) to implement the following logic in .htaccess:
www.domain.com/aa/ redirects to www2.domain.com/aa/
www.domain.com/bb/ redirects to www3.domain.com/bb/
www.domain.com/cc/ redirects to www4.domain.com/cc/
www.domain.com/dd/ redirects to www4.domain.com/dd/
www.domain.com/ee/ redirects to www4.domain.com/ee/
.htaccess rules also needs to preserve all directories/page names in the URL:
www.domain.com/aa/collectionA/productB
redirects to
www2.domain.com/aa/collectionA/productB
www.domain.com/bb/collectionC/productD
redirects to
www3.domain.com/bb/collectionC/productD
www.domain.com/cc/collectionD/productE
redirects to
www4.domain.com/cc/collectionD/productE
Any solutions would be most welcome! Thank you.

Place this code in your DOCUMENT_ROOT/.htaccess file of domain.com:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^(www\.)?domain\.com$ [NC]
RewriteRule ^aa(/.*)?$ http://www2.domain.com%{REQUEST_URI} [NC,NE,R=301,L]
RewriteCond %{HTTP_HOST} ^(www\.)?domain\.com$ [NC]
RewriteRule ^bb(/.*)?$ http://www3.domain.com%{REQUEST_URI} [NC,NE,R=301,L]
Place this code in your /cc/.htaccess, /dd/.htaccess and /ee/.htaccess:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^(www\.)?domain\.com$ [NC]
RewriteRule ^ http://www4.domain.com%{REQUEST_URI} [NC,NE,R=301,L]
Update: As per comments you can have this rule in root .htaccessof each domain:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^www2\.domain\.com$ [NC]
RewriteRule ^/?$ /aa/ [R=301,L]
RewriteCond %{HTTP_HOST} ^www3\.domain\.com$ [NC]
RewriteRule ^/?$ /ab/ [R=301,L]

Related

htaccess remove segment from url and redirect to https and www

I am deploying a Craft CMS site to a shared hosting account using Deployer.
The latest deployment is accessible from domain.com/current/public
My .htaccess file looks like the following, which strips current/public from the url and forces https:
RewriteEngine on
RewriteRule ^(.*)$ current/public/$1
RewriteCond %{HTTP:X-Forwarded-Proto} !=https
RewriteRule .* https://%{HTTP_HOST}%{REQUEST_URI} [R=302,L]
I now need to also redirect all urls to use www
How can I adjust my .htaccess to force www on all urls?
*** UPDATE ***
I have managed to solve the above with the following:
RewriteEngine on
RewriteRule ^(.*)$ current/public/$1
RewriteCond %{HTTP:X-Forwarded-Proto} !=https [OR]
RewriteCond %{HTTP_HOST} !^www\.
RewriteCond %{HTTP_HOST} ^(?:www\.)?(.+) [NC]
RewriteRule ^ https://www.%1%{REQUEST_URI} [R=301,L]
When I go to example.com/admin it redirects to example.com/current/public/admin. How can I adjust my htaccess file to remove 'current/public' from admin urls?
Your http->https and www adding rule should be top most rule so that it works on original URI instead of a rewritten URI due to other rule.
RewriteEngine on
RewriteCond %{HTTP:X-Forwarded-Proto} !=https [OR]
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteCond %{HTTP_HOST} ^(?:www\.)?(.+) [NC]
RewriteRule ^ https://www.%1%{REQUEST_URI} [R=301,L,NE]
RewriteRule ^(.*)$ current/public/$1 [L]
Make sure to clear browser cache before testing this rule.
Also add this redirect rule in /current/public/.htaccess:
RewriteCond %{THE_REQUEST} /current/public/(\S+) [NC]
RewriteRule ^ /%1 [R=301,L,NE]

htacces ad redirect - is this right?

I need to redirect all my users to another site keeping the current page structure in order to redirect them to the right page on the new domain.
My goal is redirect only the main domain, not the subdomain so I create this snippet but I'm not sure this is right:
RewriteCond %{HTTP_HOST} domain.com
RewriteRule /(.*) http://otherdomain.com [R=301,L]
I already have this in my htaccess that redirect users from www to non-www site:
RewriteCond %{HTTPS} !=on
RewriteCond %{HTTP_HOST} ^www\.(.+)$ [NC]
RewriteRule ^(.*)$ http://%1/$1 [R=301,L]
How can I make sure that if a user comes from www.domain.com he will be redirect to the new domain?
You can place new rule just below earlier rule like this:
RewriteEngine On
RewriteCond %{HTTPS} !=on
RewriteCond %{HTTP_HOST} ^www\.(.+)$ [NC]
RewriteRule (.*) http://%1/$1 [R=301,L]
RewriteCond %{HTTP_HOST} ^domain\.com$
RewriteRule (.*) http://otherdomain.com/$1 [R=301,L]
However if there is only one domain hosted then you can replace those 2 rules by this single rule:
RewriteEngine On
RewriteCond %{HTTPS} !=on
RewriteCond %{HTTP_HOST} ^(www\.)?domain\.com$
RewriteRule (.*) http://otherdomain.com/$1 [R=301,L]

htaccess redirect to HTTPS except a few urls

I am new to the htaccess redirect stuff but want to do smth special - and I dont know whats the recommend way and dont know if this is still possible or not.
I have this in my .htaccess file:
RewriteCond %{HTTPS} off
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule ^(.*)$ https://www.%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
Now every URL is redirected to the HTTPS version - this is fine and necessery. But now there are a few exceptions.
For example these urls HAS to be HTTP instead of HTTPS:
http://www.mywebsite.com/another/url/which/has/to/be/http
http://www.mywebsite.com/and_again?var=a
Is it possible to solve this with the htaccess and when its possible maybe you can send me a reference link or describe how to do this.
Edit
I now have this code:
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule ^ https://www.%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
RewriteCond %{HTTPS} off
RewriteCond %{THE_REQUEST} !\s/+(/commerce_paypal/*)\s [NC]
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
The goal is that every (!) url gets redirected to HTTPS except ANY url which has commerce_paypal at the beginning.
For example:
mydomain.com/commerce_paypal <- http
mydomain.com/commerce_paypal/smth/else <- http
mydomain.com/what/ever <- https
You can have a RewriteCond to add exceptions in the http->http rule:
RewriteEngine On
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule ^ https://www.%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
# force https:// for all except some selected URLs
RewriteCond %{HTTPS} off
RewriteCond %{THE_REQUEST} !/commerce_paypal/ [NC]
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
# force http:// for selected URLs
RewriteCond %{HTTPS} on
RewriteCond %{THE_REQUEST} /commerce_paypal/ [NC]
RewriteRule ^ http://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
Reference: Apache mod_rewrite Introduction
Apache mod_rewrite Technical Details
RewriteCond %{THE_REQUEST} !/commerce_paypal/ [NC]
worked for me. I tried many similar condition rewrites without luck.

Redirecting a couple of pages to https from .htaccess file

I want to redirect couple of pages to https. As I've already use one condition to redirect all requests to http, for those coupoe of pages, it shows too many redirects.
Look at my code below:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^example\.com$ [NC]
RewriteRule ^(.*)$ www.example.com/$1 [L,R=301]
RewriteCond %{HTTPS} off
RewriteRule ^login\.php$ https://www.example.com/login.php
This doesn't work. "Too many redirects".
Anyone can help?
The first rule makes no sense.
Shorten to this:
RewriteEngine On
RewriteCond %{HTTPS} off
RewriteRule ^login\.php$ https://www.example.com/login.php [L,R]
If the idea of the first rule was to force www, add this:
# From http://stackoverflow.com/a/4958847/1078583
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 Redirect all domain except two files

My actual htaccess redirect my complete old site to my new site, I want to redirect all domain but except two file .php (protectetfile1.php and protectedfile2.php).
This is my htaccess
RewriteEngine On
Rewritecond %{http_host} ^olddomain.org [NC]
Rewriterule ^(.*)$ http://newdomain.net/$1 [L,R=301]
Use this rule:
RewriteEngine On
Rewritecond %{http_host} ^olddomain\.org [NC]
RewriteCond %{REQUEST_URI} !/(protectetfile1|protectetfile2)\.php [NC]
Rewriterule ^(.*)$ http://newdomain.net/$1 [L,R=301]