How to redirect www to root (#)? - vercel

How do I setup an alias so that www.mydomain.com permanently redirects to mydomain.com using Zeit now CLI?
I tried now dns add mydomain.com www CNAME mydomain.com but it does not work.
It seems * CNAME alias.zeit.co is prioritized over www CNAME.

First, you need to add an alias pointing your deployment to your root domain.
now alias my-deployment-url.now.sh example.com
Then you need to add a redirect. Since this is not possible with DNS, you need to have some web server emit a 301/302 redirect. In this case, we'll make a new deployment.
now -e REDIRECT_URL=https://example.com now-examples/redirect
Now that we have this new "redirect" deployment, we'll alias it as www like so:
now alias https://redirect-otytioldup.now.sh www.example.com
See the docs for more info.

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

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

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

apache2 - can I setup a subdomain without creating an A record?

So I have
mydomain.com
and I want
test.mydomain.com
Can I set this up without creating an A record for the subdomain? Either with .htaccess or
You need something in DNS. It can be an A, a CNAME, or you can use a wildcard entry like
*.mydomain.com. IN CNAME mydomain.com.
The above is a wildcard record, which will match any name under mydomain.com. You can also use a record for your specific name.
; an alias name pointing to mydomain.com
test.mydomain.com. IN CNAME mydomain.com.
; or, a regular DNS record
test.mydomain.com. IN A 1.2.3.4
Without a DNS entry, the browser will not know about your Apache instance to begin with. So you cannot solve this with .htaccess or Apache config alone, DNS must route the request to Apache first.

How to map subdomain to a domain with CNAME?

I would like to point CNAME records for www.example.com to sub.example2.com. The hosting for example2.com is a shared hosting (cPanel without Addon domain feature).
So, what I did was to add a subdomain in the cPanel: sub.example2.com
I then added a CNAME record for www (and without www) of example.com to sub.example2.com
After propagation, www.example.com shows the default server page of the hosting instead of showing the sub domain contents. If I access this subdomain directly, it works fine.
Can somebody help me with these, please? Thanks.
If I understood your intent correctly, you have to tell cPanel that you want to serve www.example.com, not the other thing. Either that, or create a third virtual host somewhere (anywhere) that would handle www.example.com by doing a HTTP redirect to sub.example2.com.
Note also that you can't have a CNAME record for example.com without www, because a domain record already has SOA and NS records, and CNAME can't be combined with anything else - it has to be an A record.
Another thing to know about what you're doing:
You can't use a CNAME in the zone apex
(so as in the example.com zone a record "# IN CNAME example2.com")
Theoretically it is, but in reality too many (resolving) nameservers get confused.

help regarding setting up pseudo/fake subdomains on apache

First of all, sorry if I got the term 'pseudo subdomain' wrong.
what I am trying to achieve is this-
When someone registers on my application, they get a new url like..
yourname.myapp.tld
I dont want to use the subdomain system for this. To be frank, I dont know how the subdomains exactly work but it guess it requires a folder per subdomain inside the document root and then the server redirects the requests there.
Can this be achieved by doing something like -
when a visiter types any subdomain, (anything.myapp.tld), he is able to access myapp . In the index.php file i will explode the $_SERVER['HTTP_HOST'] to get the subdomain which i will store in session and will thereafter act as an identifier for that user. Ideally i wouldnt want to create any vhosts or add many lines to the hosts file. Can this be achieved with just one vhost?
Is this possible with mod rewrite or something ?
Yes you can archive this using wildcard that needs to be configured on both, the dns server and http server
On the dns a entry like this (installing dns on ubuntu https://help.ubuntu.com/10.04/serverguide/C/dns.html):
; wildcard subdomains are all directed to this IP
; of course this should be the IP of your web server
*.domain.tld. IN A 1.2.3.4
At apache an entry like this:
<VirtualHost 111.22.33.55>
DocumentRoot /www/subdomain
ServerName www.domain.tld
ServerAlias *.domain.tld
</VirtualHost>
What happens after is that everything.domain.tld will be going to your main folder so you can use the index.php to redirect it to the right place or even an htaccess using mod_rewrite.