Domain name not redirecting - .htaccess

My website can be accessed via https://example.com but can not be found using either https://www.example.com or www.example.com, if I use either of these it just throws up a "site cannot be reached" page.
Is this something I can change in the .htaccess file or is it the DNS of the domain?
I have tried adding the following code to the .htaccess file, but it makes no difference
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC]
RewriteRule ^(.*)$ https://%1/$1 [R=301,L]

...or is it the dns of the domain?
Yes, this is "most probably" a DNS issue. The request is not even reaching your server by the sounds of it. .htaccess is consequently never processed.
You'll require a CNAME record that points the www subdomain to the domain apex (example.com) - so that both www.example.com and example.com point to the same place.
However, you also need to make sure that your server is accepting requests to both www.example.com and example.com - this doesn't happen by default. Although if you have configured your account via some hosting control panel then this has probably already been done for you. Otherwise, you need a ServerAlias record defined in the relevant <VirtualHost> container. For example:
ServerName example.com
ServerAlias www.example.com

Related

How to redirect all traffic via .htaccess to https

I have wasted endless hours trying to redirect all traffic (domain.com,www.domain.com, chat.domain.com) to https. I want to keep the domain name generic because there are other domain names parked on the site.
The best rules I could come up with so far, after lots of googling, trial and error, is this:
RewriteEngine on
RewriteCond %{HTTPS} off
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
Unfortunately, these rules do NOT work for URLs belong to the subdomain.
Go through the following checklist:
The SSL certificate is valid.
The rule is defined somewhere all subdomains can actually visit 9and will have to visit).
If the rules are inside an htaccess file, the AllowOverride directive is set to All in server configuration.

Redirect subdomain into a folder of another domain

I'm trying to redirect one subdomain (a wordpress.com domain) into a folder of another domain. Example:
blog.domain.com to anotherdomain.com/blog
I know that is possible by using .htacces files, but i dont have a hosting service on my first domain.
Is it possible to do it by using DNS?
I have tried by creating some A and CNAME registrations but I cant find a way to do it: Can I have a ip for a specific folder of my second domain?
Thanks :)
I attempted to do a comment but it's too much. You can do this if you own both domains. If you don't have hosting on the first domain but you do on the other domain, then you can add the main domain as an ServerAlias on the other server.
Either through your control panel or your vhost config do the following.
Add an A record for blog.domain.com in DNS pointing to the IP address of the other domain with the hosting account.
In the configuration of the web server with the hosting add a ServerAlias of blog.domain.com to the anotherdomain.com vhost config.
in the root of the anotherdomain.com put this code inside your .htaccess file.
If you want it to actually redirect then you can do this
RewriteEngine on
RewriteCond %{HTTP_HOST} ^blog\.domain\.com [NC]
RewriteRule ^(.*)$ http://anotherdomain.com/blog/$1 [R=301,L]
If you don't want it to redirect and keep blog.domain.com in the address bar then, you can do this.
RewriteEngine on
RewriteCond %{HTTP_HOST} ^blog\.domain\.com [NC]
RewriteRule ^(.*)$ /blog/$1 [L]

.htaccess and A record subdomains

I have a wordpress hosting account. I also have a standard web hosting account with the same host. My main website, domain.com is hosted on the Wordpress platform, BUT now I want to add a subdomain for sub.domain.com.
I can't host the script on sub.domain.com as part of the wordpress hosting, so I have been told to create it on my standard web hosting account and then use an A record in the DNS for domain.com to point to the IP of sub.domain.com.
Now, all this works if I visit http://sub.domain.com. However, www.sub.domain.com doesn't work.
What I want to know is, can I edit the .htaccess for domain.com to redirect anyone who visits www.sub.domain.com to http://sub.domain.com?
You can use this:
RewriteCond %{HTTP_HOST} ^www\.sub\.domain\.com [NC]
RewriteRule ^(.*) http://sub.domain.com/$1 [L,R=301]
You can't use .htaccess to redirect traffic from www.sub.domain.com to sub.domain.com, because that traffic won't ever get to your server unless your DNS is set up correctly. In other words, if there's no DNS record for www.sub.domain.com, traffic looking for that address will never hit your server, so what is in your .htaccess would be irrelevant.
Keep in mind that you don't have to have www.sub.domain.com - almost nobody will visit that unless you share links using that domain yourself.
That said, if you really want to do this:
Create a sub-sub domain by creating an A record for www.sub.domain.com
Edit the .htaccess file for that sub-sub domain only, adding this rule:
RewriteRule ^(.*) http://sub.domain.com/$1 [L,R=301]

htaccess rewrite secondary domain to subdomain

I have a main domain "www.main.com" with subdomain "subdomain.main.com"
the subdomain is located in the folder /htdocs/subdomain/ and the main domain is a symbolic link to the /htdocs/www/ (which also serves www.main.com)
Now I have registered a new domain name, "newname.com" which I have connected to my hosting account. Now I am trying to determine the contents of /htdocs/.htaccess for rewriting incoming requests to subdomain.main.com.
To be clear, I do not want to redirect. I want www.newname.com/anything to be rewritten to subdomain.main.com/anything, while the user keeps on navigating newname.com.
I fear there is a catch with circular rewrites?
What should my .htaccess look like?
My document root is /htdocs and the subdomain and the new domain are on the same ip address.
First of all, make sure your DNS is properly setup i.e. ping www.newname.com goes to the same server (= same IP) than ping subdomain.main.com.
Then your vhost should look like:
<VirtualHost *>
ServerAdmin webmaster#papdevis.fr
DocumentRoot "/home/subdomain"
ServerName subdomain.main.com
ServerAlias www.newname.com
</VirtualHost>
Then everything should work transparently. It's all about vhosts. I don't know with htaccess, sorry.
It was in fact easy, i created a symbolic link to my new domain (primary.com/secondary) which links to the folder which also serves secondary.primary.com and I rewrite all queries to www.secondary.com to that symbolic link. Totally untransparent, just how I wanted it. It was just a bit fighting with drupal before it let me take control of a tiny part of my main domain :)
The main .htaccess is
RewriteCond %{HTTP_HOST} ^www.secondary.com [NC]
RewriteCond $1 !^secondary/
RewriteRule ^(.*)$ /secondary/$1
and i redirect all secondary.com to www.secondary.com in my rewrite base with this .htaccess
RewriteEngine On
RewriteCond %{HTTP_HOST} secondary.com$ [NC]
RewriteRule ^(.*)$ http://www.secondary.com/$1 [R=301,L]

Webfaction subdomain forwarding

I have shared hosting on webfaction and I want to have www.mydomain.com forward to mydomain.com, in the same way that www.stackoverflow.com redirects to stackoverflow.com. In the webfaction control panel I set up a CNAME record linking the www.mydomain.com subdomain to mydomain.com, but this doesn't seem to be working. Maybe what's messing it up is that mydomain.com is a virtual host and doesn't have an A record to an IP address?
Anyway, can someone help me figure out the right way to do this, either in the webfaction control panel or directly in the httpd.conf file? Thanks in advance.
The solution I use is to create a Static/CGI/PHP application (which I call redirect), and place an .htaccess file at its root. I point all the domains and subdomains that need redirecting to this application, and I then populate the .htaccess file with all the necessary redirection directives for my server, like so:
RewriteEngine on
RewriteCond %{HTTP_HOST} www.example.com
RewriteRule ^(.*) http://example.com/$1 [R=301,L]
RewriteCond %{HTTP_HOST} www.example1.com
RewriteRule ^(.*) http://example1.com/$1 [R=301,L]
It works well, and because it is instantaneous, it's easy to debug.

Resources