0

I need to:

  • add www, so user will:

    • type https://example.com/test.html
    • see in browser https://www.example.com/test.html
    • get file from example.com/test.html
  • add http -> https redirect, so user will:

    • type http://www.example.com/test.html
    • see in browser https://www.example.com/test.html
    • get file from example.com/test.html
  • add subdomain silent redirect, so user will:

    • type https://new.example.com/test.html
    • see in browser https://new.example.com/test.html
    • get file from example.com/new/test.html
  • and of course combinated, for example:

    • type http://new.example.com/test.html
    • see in browser https://new.example.com/test.html
    • get file from example.com/new/test.html

Is this possible via .htaccess file, eventually how?

www and https forwarding is already working fine for me, but I do not know, how to do silent forwaring with subdomains:

RewriteEngine On

# https
RewriteCond %{HTTP:X-Forwarded-Proto} !https
RewriteCond %{HTTPS} off
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]

# WWW
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteCond %{HTTP_HOST} !^new\. [NC]
RewriteRule (.*) https://www.%{HTTP_HOST}%{REQUEST_URI} [L,R=301]

Thank you.

Foreen
  • 369
  • 2
  • 17
  • 1
    Please share your htaccess rule file in your question, which is encouraged as an efforts to put by questioners in their questions, thank you. – RavinderSingh13 May 09 '21 at 13:10
  • I have tried something, but I do not have anything in .htacces file now, because everything that I tried was not working. – Foreen May 09 '21 at 13:17
  • 1
    Just as an effort try adding whatever you tried there is nothing right or wrong, else question may come in off topic category the, thank you. Once I add it, I will try to help here, cheers. – RavinderSingh13 May 09 '21 at 13:18
  • 1
    Thank you, I added rules for www and https, that is working fine for me, but I do not know how to add silent forwarding. – Foreen May 09 '21 at 13:23
  • 1
    Are `new.example.com` and `example.com` pointing to same directory on same host? – anubhava May 09 '21 at 13:37
  • Yes, they are. And I want to separate them. – Foreen May 09 '21 at 13:47

2 Answers2

1

You may try these rules in site root .htaccess:

DirectoryIndex index.php
RewriteEngine On

# add www for main domain
RewriteCond %{HTTP_HOST} ^example\.com$ [NC]
RewriteRule ^ https://www.%{HTTP_HOST}%{REQUEST_URI} [NE,R=301,L]

# add https for all domains
RewriteCond %{HTTPS} !on
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [NE,R=301,L]

# for new domain silently forward to new/ directory
RewriteCond %{HTTP_HOST} ^new\.example\.com [NC]
RewriteRule !^new/ new%{REQUEST_URI} [NC,L]

Make sure you don't have any other code except these lines in site root and you must clear browser cache completely.

anubhava
  • 761,203
  • 64
  • 569
  • 643
  • Hello and thanks, I do not know, this is not working for me, too. I am still getting error 'too many redirects', but I think that the problem is at my hosting provider. I have solved it by PHP. – Foreen May 14 '21 at 09:52
  • Test in Chrome dev tool with **caching disabled** and check in Network tab what are 301/302 redirect URLs you get. – anubhava May 14 '21 at 09:57
0

With your shown attempts/samples, could you please try following. Please make sure to you your htaccess rules in following way and clear your browser cache before testing your URLs.

RewriteEngine ON
RewriteCond %{HTTPS} !on
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [NE,R=301,L]

RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule ^ https://www.%{HTTP_HOST}%{REQUEST_URI} [NE,R=301,L]
    
RewriteCond %{HTTP_HOST} ^new\.example\.com [NC]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)/?$ new/index.php?$1 [QSA,L]

RewriteCond %{HTTP_HOST} ^new\.example\.com [NC]
RewriteCond %{REQUEST_URI} !^/new [NC]
RewriteRule ^(.*)/?$ new/$1 [L]

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)/?$ index.php?$1 [QSA,L]
RavinderSingh13
  • 130,504
  • 14
  • 57
  • 93
  • Thank you, but when I add this to .htacces file (so in .htaccess is only your code), browser says to me: The page isn’t redirecting properly, page redirected you too many times. – Foreen May 09 '21 at 13:36
  • 1
    @TadeasekF, ok, there was a typo, I have fixed it, please have edited rules in your file and clear browser cache and try again then. – RavinderSingh13 May 09 '21 at 13:37
  • Thank you, but same error. I have even tried browser, that I have never used, but the error is same. – Foreen May 09 '21 at 13:45
  • 1
    @TadeasekF, ok, I have edited the rules now, could you please try them now and let me know how it goes, thank you. – RavinderSingh13 May 09 '21 at 13:48
  • Thank you a lot, but still same error; ERR_TOO_MANY_REDIRECTS – Foreen May 09 '21 at 13:51
  • 1
    @TadeasekF, could you please do let me know what is the sample url you are hitting? – RavinderSingh13 May 09 '21 at 13:53
  • I tried for example https://example.com, https://www.example.com, http://new.example.com and nothing works. – Foreen May 09 '21 at 13:57
  • 1
    @TadeasekF, ok, could you please do let me know non existing pages like `http://example.com` etc from which file you want to serve? eg--> index.php etc? Also where that file is present inside new folder? Kindly confirm once. – RavinderSingh13 May 09 '21 at 13:59
  • Yes, I want to serve for example: example.com/index.php and example.com/new/index.php as new.example.com – Foreen May 09 '21 at 14:03
  • @TadeasekF, I have edited the rules now, could you please check my updated rules, please make sure to clear your browser cache before testing your URLs. – RavinderSingh13 May 09 '21 at 14:06
  • Thank you, still same. I always rerun incognito mode, is that all right? – Foreen May 09 '21 at 14:12
  • @TadeasekF, ok, I have edited my rules now, yes please better to clear cache and then test your urls. – RavinderSingh13 May 09 '21 at 14:16
  • Same error again. I tried to clear everything from browser and then run incognito. – Foreen May 09 '21 at 14:21
  • @TadeasekF, ok, where is your htaccess file present? It should be residing in same folder where you have new folder. Means folder `new` and htaccess should be placed in same folder, let me know on same. – RavinderSingh13 May 09 '21 at 14:23
  • Yes, I have /.htaccess, /index.php, /new/index.php – Foreen May 09 '21 at 14:26
  • 1
    @TadeasekF, Then these rules should work. could you please place a test line(temporary purposes) `--------` along with my rules and check any rule if you get 500 internal error means its enabled else its not enabled, let me know how it goes, thank you. – RavinderSingh13 May 09 '21 at 14:27
  • Ok, I added ---- to each blank line in .htaccess and I got 500, so it looks enabled. – Foreen May 09 '21 at 14:31
  • @TadeasekF, what url `example.com` gives you? – RavinderSingh13 May 09 '21 at 14:33
  • 1
    @TadeasekF, is it 404 or 500 or 403? – RavinderSingh13 May 09 '21 at 14:41
  • That is not shown - I am getting: This page isn’t working. Page redirected you too many times. Try clearing your cookies. ERR_TOO_MANY_REDIRECTS – Foreen May 09 '21 at 14:53
  • 1
    @TadeasekF, ok any other rules you have in your current htaccess apart from these? OR you want any other htaccess residing in any of the folders? Kindly confirm once. – RavinderSingh13 May 09 '21 at 14:59
  • A do not have anything other than your code in my htaccess file. I have only one htaccess file. – Foreen May 09 '21 at 15:01
  • @TadeasekF, sure, you have created virtual host for sub domain? – RavinderSingh13 May 09 '21 at 15:02
  • No, I am testing it on my domain. – Foreen May 09 '21 at 15:10
  • @TadeasekF, may be you could take a look to link https://stackoverflow.com/a/586160/5866580 for checking how to create sub domain in htaccess, though I am not that sure but give it a shot for reading and see how ti goes. – RavinderSingh13 May 09 '21 at 15:28
  • Ok, thank you. I will try to solve it. As I said, it is wonderfully that https and www is working fine, but when I try subdomain forwarding, it fails. – Foreen May 09 '21 at 15:36
  • @TadeasekF, yeah once you have sub domain these rules should fly then, you could upvote my answer if this has helped you too, cheers. – RavinderSingh13 May 09 '21 at 15:37