0

So this is my conf:

<VirtualHost *:80>
  <Directory /var/www/html/webserver/public>
      Allow From All
      AllowOverride All
      Options -Indexes
  </Directory>

  DocumentRoot /var/www/html/webserver/public
  ServerName ./install.sh
</VirtualHost>

<VirtualHost *:80>
  <Directory /var/www/html/webserver/public>
      Allow From All
      AllowOverride All
      Options -Indexes
  </Directory>

  DocumentRoot /var/www/html/webserver/public
  ServerName test.com
  ServerAlias www.test.com
  Redirect permanent / https://test.com/

  RewriteEngine on
  #RewriteBase /
  RewriteCond %{SERVER_NAME} =www.test.com [OR]
  RewriteCond %{SERVER_NAME} =test.com
  RewriteRule ^ https://%{SERVER_NAME}%{REQUEST_URI} [END,NE,R=permanent]
</VirtualHost>
<IfModule mod_ssl.c>
    <VirtualHost *:443>
      <Directory /var/www/html/webserver/public>
          Allow From All
          AllowOverride All
          Options -Indexes
      </Directory>
    
      DocumentRoot /var/www/html/webserver/public
      ServerName test.com
      ServerAlias www.test.com
      SSLCertificateFile /etc/letsencrypt/live/test.com/fullchain.pem
      SSLCertificateKeyFile /etc/letsencrypt/live/test.com/privkey.pem
      Include /etc/letsencrypt/options-ssl-apache.conf
    </VirtualHost>
</IfModule>

This works perfectly for http -> https redirection. For example test.com or http://test.com redirects to https://test.com. www.test.com does not work (I don't know why?), I'll get a 404 page. Next big thing, lets assume the ip of my webserver is 12.23.45.67 - how do I have to change my config file, so this ip also redirects to https://test.com?

new .conf file based on @Don't Panic post:

<VirtualHost *:80>
  <Directory /var/www/html/webserver/public>
      Allow From All
      AllowOverride All
      Options -Indexes
  </Directory>

  DocumentRoot /var/www/html/webserver/public
  ServerName test.com
  ServerAlias www.test.com

RewriteEngine on
#RewriteBase /
RewriteCond %{SERVER_NAME} =www.test.com [OR]
RewriteCond %{SERVER_NAME} =test.com
RewriteRule ^ https://%{SERVER_NAME}%{REQUEST_URI} [END,NE,R=permanent]
</VirtualHost>

<VirtualHost *:80>

ServerName XX.XX.XX.XX
ServerAlias www.test.com
DocumentRoot /var/www/html/webserver/public


RewriteEngine on
RewriteCond %{SERVER_NAME} =test.com [OR]
RewriteCond %{SERVER_NAME} =www.test.com
RewriteRule ^ https://%{SERVER_NAME}%{REQUEST_URI} [END,NE,R=permanent]

</VirtualHost>
johndoe123
  • 11
  • 2
  • Looks like maybe some copy-paste errors here? 1) There is no https config. 2) `ServerName ./install.sh` is invalid, I think; 3) `ServerAlias www.test.com` has no point if it is exactly the same as the `ServerName`? – Don't Panic Oct 18 '21 at 14:39
  • @Don'tPanic Thanks! Yeah, it should be `ServerName test.com` and `ServerAlias www.test.com`. Nevertheless, problem is still there. I updated my first post with the https (ssl) config. – johndoe123 Oct 19 '21 at 06:47
  • 1) The first vhost block (with the invalid `ServerName` I pointed out yesterday) should be removed, AFAICT. 2) The `RewriteCond` stuff you have at the end of the http vhost already does the same thing that the `Redirect permanent` line does - don't include them both, remove the `Redirect permanent`. 3) Have you restarted Apache, and cleared browser cache since fixing that `ServerAlias`? "*www.test.com does not work*" - http, or https? – Don't Panic Oct 19 '21 at 07:29
  • Regarding the separate redirect IP question - there are many duplicates here on SO (eg https://stackoverflow.com/a/67920979). But if Apache does not receive a hostname with a request (eg a request for the IP) it will match the first vhost it finds. If that is your http vhost above, it will redirect already, nothing else to do. – Don't Panic Oct 19 '21 at 07:48
  • @Don'tPanic 1) i removed the first vhost block, problem still the same., no redirection from ip adress XX.XX.XX.XX to domain name https://test.com 2) i removed `Redirect permament`, problem still the same 3) i always restart apache after editing the .conf file, also cleared the cache and i am using different browsers 4) both, http and https I updated my start post with a new .conf based on your last post. All in all, redirection from test.com or http://test.com to https://test.com works like a charm, redirecton from XX.XX.XX.XX or www.test.com runs into 404 – johndoe123 Oct 20 '21 at 06:34
  • Also i updated `ServerName XX.XX.XX.XX` in the .conf file: `XX.XX.XX.XX` runs into 404, but `https://XX.XX.XX.XX` redirects to `https://test.com`. So right now, whats not working: Also redirect from `http://XX.XX.XX.XX` and from `www.test.com` – johndoe123 Oct 20 '21 at 06:48
  • 404 means the vhost is working, but it can't find the requested page, eg if you are just requesting `/` and get a 404 it means either you have no `DirectoryIndex` specified and directory listing is disabled (with `Options -Indexes`), or simply none of the specified `DirectoryIndex` files exist (no index.html, etc). I'll say again, you do not need a `ServerName` for an IP address. And you should not have the same server name duplicated between vhosts for the same protocol (as you have for www.test.com). 1 vhost for http is enough. It looks to me like you have another vhost somewhere. – Don't Panic Oct 20 '21 at 07:34
  • @Don'tPanic See my newest post. There is a working example for nearly every redirecting (thanks to you!) but not for `www.test.com`. `http://test.com` is working, also `test.com` or the ip address `XX.XX.XX.XX` and `https://XX.XX.XX.XX` But NOT `www.test.com` -> "This site can’t be reached" – johndoe123 Oct 20 '21 at 13:09

1 Answers1

0

So my final solution looks like this:

<VirtualHost *:80>
  <Directory /var/www/html/webserver/public>
      Allow From All
      AllowOverride All
      Options -Indexes
  </Directory>

  DocumentRoot /var/www/html/webserver/public
  Redirect / https://test.com
</VirtualHost>

This is working for nearly every redirection, but it's not working for www.test.com - Is this a good solution? May not. But it's kind of a working one.

johndoe123
  • 11
  • 2
  • This will only redirect `/`. A request for `/some-page`, or anything other than the site root, will not redirect. Your previous `RewriteRule ` will redirect any and all requests, that's the better solution. – Don't Panic Oct 20 '21 at 13:23