SSL https error on 404 pages, but other pages are working fine with redirection enabled - apache

My drupal site has auto redirection to remove www prefix from URLs and add https as seen from these links, they goto a 404 page tat is just a contact page:
http://www.phonerepair.us/some_random_url-- redirection works
http://phonerepair.us/some_random_url -- redirection works
https://www.phonerepair.us/some_random_url -- redirection FAILS
https://phonerepair.us/some_random_url -- This should be END RESULT
My apache .conf setting and .htaccess for this domain is:
What could be off in my redirection rules to cause the https://www to throw an SSL error like this:

Deleted the ServerAlias and then duplicated the entire virtualhost in my .conf file and just added www to the ServerName. Now there should be 2 virtualhost settings. One for the non-www domain and another for the www one. Then do a service apache2 restart
Delete the ssl-le.conf file for your domain.
Then I regenerated my SSL certs using certbot --apache and select both of these domains and generate.
Now my www and non-www works. Below is how my conf files look. A better approach is to use the * wildcard config, but I wasn't sure how to get that working with certbot --apache command

Related

Apache redirecting example.com to ec2 public DNS

Setup
Route 53 Domain example.com has A record to id.cloudfront.net and AAAA record to random-id.cloudfront.net.
Cloudfront has origin domain as EC2 public DNS ec.xxxxxx.amazonaws.com and alternate domain as example.com. Also, it has a AWS certificate for SSL/TLS (for now it accepts both HTTP & HTTPS requests)
EC2 is an Ubuntu-20.04 AMI with apache2, php7.4 and phpmyadmin, all installed using apt-get.
Issue is whenever I put in example.com/* the site is automatically redirected to ec.xxxxxx.amazonaws.com/*.
Example:
example.com/phpmyadmin gets redirected to ec.xxxxxx.amazonaws.com/phpmyadmin/.
I tried to setup wordpress and example.com redirects to http://ec2-xxxxxx.amazonaws.com/wp-admin/setup-config.php`
These are the things I have tried.
Added ServerName example.com in /etc/apache2/apache2.conf
This command sudo ufw allow in "Apache Full"
I do not have .htaccess in my /var/www/html folder.
Is it a Cloudfront or an Apache issue? This is my first rodeo with AWS and Apache and trying to learn how it works.
Solutions that seem out of scope:
https://serverfault.com/questions/685654/apache-server-config-redirect-from-ip-to-domain-name-ec2
Or anything that includes creating .htaccess at the root directory as I feel that would create an endless loop.
I can provide more information, if required.
Update:
Removed A and AAAA record from route53. Doesn't work.
Also, added ServerName example.com to /etc/apache2/sites-enabled/000-default.conf
Also, visiting random-id.cloudfront.net does the same redirection.
However, visiting elastic-IP does not do any redirection.
If I correctly understand your setup, probably you need CNAME instead of A record in Route53
Traced the issue to HTTP_HOST being the ec2 public DNS received by the server. So, if I visited example.com/index.php and the code were supposed to redirect to example.com/index2.php it would instead redirect to ec.xxxxxx.amazonaws.com/index2.php because HTTP_HOST = ec.xxxxxx.amazonaws.com.
Fixed by updating CloudFront Distribution's behavior to forward Host value.
So, if I visit example.com the host value would be example.com
And if I visit id.cloudfront.net then host value would be id.cloudfront.net.
https://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html#sec14.23

Using PointDNS on Heroku to manage SSL and non-www to www redirects

I've been using Heroku to host and manage a website of mine that requires an SSL certificate. For the last year and a half I've been using ZerigoDNS to manage my domain and SSL redirecting (although, redirecting from non-https, non-www to https and www does not work).
I just got a notice from Heroku that they will be shutting down ZerigoDNS soon and they recommend switching to PointDNS. I couldn't find any information in the PointDNS documentation about managing SSL redirects.
Here's what I'm looking to do:
non-www non-https redirects to https and www
non-https redirects to https and www
https but non-www redirects to https and www
The reason everything needs to be forwarded to https and www is that's what the ssl cert was created for (the www is required. I don't have a wildcard certificate).
Does anyone familiar with PointDNS know how to to set up the records and redirects properly?
Thanks in advance!
Regarding non-www to www redirects, I just got that to work as follows:
First, you must add both www.your-domain.com and your-domain.com as custom domains.
heroku domains:add andrewpuglionesi.com
heroku domains:add www.andrewpuglionesi.com
I have a CNAME record with name www.my-domain.com.. The text in its data field is my-heroku-domain.herokuapp.com. I also have an alias record with the name my-domain.com., and I made its data field www.my-domain.com. In other words, the alias just points to the CNAME.
I don't know if this is well-advised, but it seems to work (and without sticking www in the user's URL, at that). It simply solves the problem where you can't access pages other than the index without having www there). I like how Heroku and PointDNS' documentation both say you can easily do redirects and then don't actually explain how to... I guess this is one way.
I've previously been able to force https protocol through the backend of a Django site. I can't remember the exact method I used, but you might be able to do it without the DNS stuff.
First, add both www.your-domain.com and your-domain.com as custom domains under settings or run below commands
heroku domains:add domainname.com
heroku domains:add www.domainname.com
For redirection, you need to do it via server, eg. if you are using apache you can achieve redirections via .htacess file.
Also, you can only redirect non-www to www with pointDNS redirect option

Apache Redirect domain.com/dir/slug to sub.domain.com/dir/slug silently without .htaccess

I'm having the darndest time trying to figure this out.
I have a site on a legacy system which will not permit me to alter the .htaccess file at domain.com. I have moved part of this site to a WordPress install located at sub.domain.com. I have to make the URL domain.com/dir/ redirect silently to sub.domain.com/dir/. How can I go about this? I can edit the Apache config files for both domain.com and sub.domain.com, and the .htaccess for sub.domain.com, but not the .htaccess for domain.com
Thanks!
Just add the redirect into the apache config files. In the virtualhost for domain.com:
Redirect 301 / http://sub.domain.com/
If by "silently" (not sure what that's supposed to mean since redirects are involve the browser sending a new request), I'm guessing you want to reverse proxy? Then you'd need to make sure mod_proxy is loaded, then do:
ProxyPass / http://sub.domain.com/

Apache 2.2 disable unconfigured subdomains

There is a webserver which has example.com, www.example.com, my.example.com, shop.example.com, static.example.com. All sites except www.example.com is on https. My problem is when I type randomstring.example.com -> shop.example.com gets served. But I want nothing to be served. For example mail.google.com is a valid subdomain. if you type johndoe.google.com you see "This webpage is not available". How to achieve this via Vhost/.htaccess file.
It's not the part of apache. You should disallow DNS resolving for subdomains like *.example.com.
or
If you don't have access to the DNS server - you can create a virtualhost with blank index page and put this virtualhost directive on top of the all virtualhosts. It shouldn't be same as google's behavior (they are using 1st way), but at least you can protect shop.example.com visiting from randomstring.example.com

VirtualHosts fallback to rewriting URL

I'm setting up an Apache web server, and I have a bunch of VirtualHost entries in my httpd.conf, which all work fine. All subdomain requests on my domain (eg. test.mydomain.com, test2.mydomain.com) are sent to the same IP address (same server). I'd like to rewrite any unrecognized subdomains to simply www.mydomain.com or just mydomain.com if there isn't a VirtualHost entry for them. Is this achievable using Apache?
Thanks.
You could set a Redirect rule in your default VirtualHost (ie, the one that's used when accessing the server via a host without a VirtualHost):
Redirect permanent / http://example.com/
If that doesn't suit your needs (like if you want to still be able to access some files on the fallback DocumentRoot), you could set a PHP script as your 404 handler:
httpd.conf
ErrorDocument 404 /404.php
404.php
<?php
header($_SERVER["SERVER_PROTOCOL"] . " 301 Moved Permanently");
header("Location: http://example.com");