0

I've been trying to get this up in my .htaccess file for 2days now, but i can't get it to work properly.

I'm working at a company and they need this specific redirects to their website and subdomain because their joomla setup requires it.

all scenarios:

main domain

http://www.my-main.be -> https://www.my-main.be

http://my-main.be -> https://www.my-main.be

https://www.my-main.be -> https://www.my-main.be

https://my-main.be -> https://www.my-main.be

subdomain

http://www.mysub.my-main.be -> http://mysub.my-main.be

http://mysub.my-main.be -> http://mysub.my-main.be

https://www.mysub.my-main.be -> http://mysub.my-main.be

https://mysub.my-main.be -> http://mysub.my-main.be

note:

  • mysub may be fixed, but is prefered dynamic so all the subdomains go to http://.
  • It is important to have www in the main domain
  • It is important to have www left out of the subdomain

Is this possible?

Leave a comment if i did not explain this well enough and i will clarify it more.

Thanks in advance

Community
  • 1
  • 1
Jonas S'jegers
  • 101
  • 1
  • 5
  • Are `DocumentRoot` for main domain and subdomain pointing to same location? – anubhava Sep 19 '17 at 20:53
  • Yes, there is only one instance of joomla installed – Jonas S'jegers Sep 20 '17 at 06:27
  • can you show your existing .htaccess in question – anubhava Sep 20 '17 at 06:50
  • I changed it alot in the past two days. the latest version i have now is not even close to working. but i might post it on fiverr as well, because it has to be ready soon – Jonas S'jegers Sep 20 '17 at 07:28
  • There is no stable version anymore. at the moment all links get redirected to https//www because that's the only way the main website works now. We have 100+ orders/day on the website so i can't be screwing around in it alot. you can see all the scenarios above and if you really need code examples you can see at https://stackoverflow.com/questions/29088257/htaccess-redirect-domain-to-https-subdomain-to-http-and-www-to-non-www or https://stackoverflow.com/questions/27882723/redirect-primary-domain-to-https-but-all-sub-domains-to-http-with-mod-rewrite-an – Jonas S'jegers Sep 20 '17 at 09:54
  • Yes, that example worked fairly good, but as you can see the main-domain links have to go to https//www – Jonas S'jegers Sep 20 '17 at 10:17

2 Answers2

0

You can have these rules just below RewriteEngine line:

RewriteEngine On

# for main domain
RewriteCond %{HTTP_HOST} !^www\. [NC,OR]
RewriteCond %{HTTPS} !on
RewriteCond %{HTTP_HOST} ^(?:www\.)?(my-main\.be)$ [NC]
RewriteRule ^ https://www.%1%{REQUEST_URI} [R=301,L,NE]

# for sub domain
RewriteCond %{HTTP_HOST} ^www\. [NC,OR]
RewriteCond %{HTTPS} on
RewriteCond %{HTTP_HOST} ^(?:www\.)?(mysub\.my-main\.be)$ [NC]
RewriteRule ^ http://%1%{REQUEST_URI} [R=301,L,NE]

# rest of rules go here

Test it after clearing your browser cache.

anubhava
  • 761,203
  • 64
  • 569
  • 643
0

Please checked rules below both rules will work well with dynamic sub domain also

RewriteEngine On
RewriteCond %{HTTPS} off [OR]
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteCond %{HTTP_HOST} ^(?:www\.)?(.+)$ [NC]
RewriteRule ^ https://www.%1%{REQUEST_URI} [L,NE,R=301]

RewriteCond %{HTTP_HOST} ^www\. [NC,OR]
RewriteCond %{HTTPS} on
RewriteCond %{HTTP_HOST} ^(?:www\.)?((.*))$ [NC]
RewriteRule ^(.*)$ http://%1/$1 [R=301,L]
Malhar
  • 21
  • 2