Trace 301 Redirect Rule and move all request to www subdomain - apache

I have a domain example.com added to the DigitalOcean (Ubuntu 16.04) with Apache2.4.18 and have Wordpress installed on it. The site is running properly.
The problem is, that all the request www.example.com redirects to example.com. I need example.com to redirect to www.example.com.
I see there are 2 ways to do it. 1. .htaccess and 2. Apache config file, currently using 000-default.conf file. I installed letsencrypt ssl, which added the following RewriteRule to 000-default.conf
RewriteEngine on
RewriteCond %{SERVER_NAME} =www.example.com [OR]
RewriteCond %{SERVER_NAME} =example.com
RewriteRule ^ https://%{SERVER_NAME}%{REQUEST_URI} [END,NE,R=permanent]
based on the research online, I did modify the 000-default.conf to
RewriteRule ^ https://www.%{SERVER_NAME}%{REQUEST_URI} [END,NE,R=permanent]
and also tried the following code in .htaccess file.
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule ^(.*)$ https://www.%{HTTP_HOST}/$1 [R=301,L]
Upon doing either of these, I get error of multiple redirection loop. Which basically redirecting example.com to www.example.com and www.example.com to example.com and so on. I tested the same on Redirectcheck.com.
The main issue is I can't seem to find out the first 301 Redirect which is all the request being redirected to exmple.com. If I can disable or overwrite the main 301 Redirect rule then I can achieve all the request to be redirected to www.example.com.
I did my research but I'm hitting my head hard here, any help would be really appreciated. Thank You!

So, I finally traced it down, all the configuration were correct, the only issue was in the WordPress -> Admin Panel -> Setting -> General. I have placed the site_url and main_url as example.com and when I updated it to www.example.com and tested again, It did fix it.

Related

Redirect all http requests (including subdomains) to https

I have already tried different solutions from other threads but somehow none is working. So I have a domain called my-domain.com and I want that all http requests to http://my-domain.com or http:///*.my-domain.com are redirected to the corresponding https page. In the folder /etc/apache2/sites-enabled/ I have a configuration file which looks like this:
<VirtualHost *:80>
ServerName my-domain.com
ServerAlias *.my-domain.com
RewriteEngine On
RewriteCond %{HTTP_HOST} ^(.+)\.my-domain\.com$
RewriteRule ^(.*)$ https://%1.my-domain.com/$1 [R=302,L]
</VirtualHost>
But still there are no redirects. All requests are failing because of a timeout. Is there something I am missing?
In your RewriteRule, the %1 is not doing what you think it will be doing.
# bad
RewriteRule ^(.*)$ https://%1.my-domain.com/$1 [R=302,L]
Try this:
# good
RewriteEngine On
RewriteCond %{HTTPS} off
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
What we are doing here is taking ANY request that is NOT HTTPS, and rewriting it. We don't care about looking at the %{HTTP_HOST}, nor the %{REQUEST_URI}, because we will just use them blindly.
This is a "blind forward" to HTTPS. I don't care what you asked for, but since you did it over HTTP, you will have to do it over HTTPS.
At that point, it becomes a different rule to redirect further.
Additionally, 302 is a temporary redirect. You may wish to consider a 301, for a permanent redirect across ports.

Force to SSL maintaining the URL

I don't have much experience with VHOST and SSL, I tried lot of possibilities but none of them is working.
I have a website with 2 folders:
www.example.com/userpage
www.example.com/adminpage
If I go to www.example.com, my loginsystem automatically redirect to www.example.com/userpage.
If I want to go to the admin section, I have to write manually www.example.com/adminpage.
Now I switched to SSL, and everything is working if I type https://....
But I cannot understand how to force the redirect from http to https.
I wrote this in my apache vhost file:
<VirtualHost *:80>
DocumentRoot "C:/xampp/htdocs"
ServerName example.com
RewriteEngine on
RewriteCond %{HTTP_HOST} ^example.com
RewriteCond ${HTTPS} !=on
RewriteRule (.*) https://example.com/$1 [R=301,L]
</VirtualHost>
But it's not working.
How can I manage it?
If I write www.example.com/adminpage it have to redirect me to https://www.example.com/adminpage.
Actually it should work for every subfolder, if for example I send an email to a user saying "hey user, please check your account www.example.com/user/check_account.php?userid=14125114
it have to then redirect it to automatically:
https://www.example.com/user/check_account.php?userid=14125114
so it should work for every page and every subfolder.
Thank you for your suggestions
Should be quite simple. Replace this block:
RewriteEngine on
RewriteCond %{HTTP_HOST} ^example.com
RewriteCond ${HTTPS} !=on
RewriteRule (.*) https://example.com/$1 [R=301,L]
With this:
Redirect permanent "/" "https://example.com/"

.htaccess redirect to example.com except for directory redirect to example.com/example

Variations on this question have been asked, and so I apologize in advance -- I simply cannot get this to work. I know the devil is in the details with the .htaccess, so I'm sure I'm missing something small.
Here's what have:
I have a page: example.com/ which redirects all traffic to its https equivalent. I'm building the next version of the same site inside of the directory example.com/example.com/ I'm trying to write a redirect that will send all traffic to its https equivalent except for the directory that I'm working in, which should be left alone. My problem is, whenever I try to access example.com/example.com/ I get redirected to the 404 of example.com (which makes sense). I just can't get the redirect to not loop indefinitely.
I currently have a redirect from http to https for example.com as follows:
RewriteEngine On
RewriteCond %{SERVER_PORT} 80
RewriteRule ^(.*)$ https://example.com/$1 [R,L]
This works just fine, and only causes troubles when I try to access the subfolder.
tl;dr
Here's what I need:
http://example.com/ REDIRECTS TO https://example.com/
http://example.com/[anything else] REDIRECTS TO https://example.com/[anything else]
https://example.com/example.com/ SHOULD NOT REDIRECT
Try:
RewriteEngine On
RewriteCond %{SERVER_PORT} 80
RewriteCond %{REQUEST_URI} !example.com
RewriteRule ^(.*)$ https://example.com/$1 [L]

Redirect all requests to HTTPS AND www to non-www

I'm fixing my Apache (2.4.12) config files on a server that serves three different domain names. I have a different config file for each site. I cannot for the life of me figure out how to accomplish both of the following:
Redirect all http requests to https, keeping the entire rest of the request (subdomain/host AND document path) exactly the same
Redirect all www requests to non-www
I've read that this can be done in one step if I have only one *:80 VirtualHost and put the rewrite rules there (the remainder of my subdomains are all *:443 VirtualHosts with the exception of www), but I can't figure out how to do it. These answers on SO did not work:
The accepted answer in this question is not correct (only does the https redirect)
This answer does not work for me--only the https redirect works.
This question doesn't deal with a wildcard subdomain and is thus inapplicable.
This question is also inapplicable because it doesn't deal with subdomains.
EDIT: This is the code I reference in the comments for mike.k's answer below.
<VirtualHost *:80>
RewriteEngine On
RewriteCond %{HTTP_HOST} www.example.com
RewriteRule ^(.*)$ https://example.com/$1 [R=permanent,L]
RewriteCond %{HTTPS} off
RewriteRule (.*) https://example.com%{REQUEST_URI}
</VirtualHost>
This is from my production system and works.
THE_HOSTNAME is for instance server, and then THE_FQHN is server.domain.edu, which helps for SSL certificates if you don't want to support wildcards and multiple domain names.
# redirect to FQHN
RewriteEngine on
RewriteCond %{HTTP_HOST} THE_HOSTNAME$
RewriteRule ^(.*)$ https://THE_FQHN/ $1 [R=permanent,L]
# redirect to HTTPS
RewriteCond %{HTTPS} off
RewriteRule (.*) https://THE_FQHN%{REQUEST_URI}
In your case www.domain.com would be where THE_HOSTNAME is, and THE_FQHN would be domain.com, just flipped around

Redirect wildcard subdomains to subdirectory, without changing URL in address bar

I've read a lot of questions and answers about this on here but none that seem to solve my specific problem.
I want to redirect any subdomain to the subdirectory to match.
So: x.domain.com would go to domain.com/x, and y.domain.com would go to domain.com/y - But I want to do this without the URL in the address bar changing.
Here's what I have so far:
Options +FollowSymLinks
RewriteEngine on
RewriteBase /
RewriteCond %{HTTP_HOST} !^(www)\. [NC]
RewriteCond %{HTTP_HOST} ^(.*)\.domain\.com [NC]
RewriteRule ^ /%1 [P,L]
But this takes me to a website redirect loop, with an incorrect address in the URL bar where the subdomain still exists.
For example, x.domain.com takes me to x.domain.com/x and I get a redirect loop error.
I'd be grateful if anyone can point me in the right direction! Nothing I change seems to work...
First of all, make sure that the vhost in the apache configuration is properly configured and all subdomains of domain.com are in the same host configuration (wildcard):
<VirtualHost *:80>
ServerName domain.com
ServerAlias *.domain.com
...
You can get the redirect working with the following htaccess configuration:
RewriteEngine on
RewriteCond %{HTTP_HOST} ^(.*)\.domain\.com
RewriteRule ^(.*)$ http://domain.com/%1/$1 [L,NC,QSA]
Now, if you open asd.domain.com it should redirect you to domain.com/asd.
You will still have the problem, that the redirect is visible in the URL address bar. In order to prevent this, enable mod_proxy (and load the submodules) on your server and exchange the "L" flag with the "P" flag:
RewriteEngine on
RewriteCond %{HTTP_HOST} ^(.*)\.domain\.com
RewriteRule ^(.*)$ http://domain.com/%1/$1 [P,NC,QSA]
If this doesn't work, viewing the vhost configuration and the content of error.log on subdomain calling will be helpful!
References:
.htaccess rewrite subdomain to directory
http://httpd.apache.org/docs/2.2/mod/mod_proxy.html
http://httpd.apache.org/docs/2.2/rewrite/flags.html#flag_p
This can be achieved in .htaccess without mod_proxy provided your server is configured to allow wildcard subdomains. (I achieved that in JustHost by creating a subomain manually named *). Add this to your .htaccess file:
RewriteEngine On
RewriteCond %{HTTP_HOST} !^www\.website\.com$
RewriteCond %{HTTP_HOST} ^(\w+)\.website\.com$
RewriteCond %{REQUEST_URI}:%1 !^/([^/]+)/([^:]*):\1
RewriteRule ^(.*)$ /%1/$1 [QSA]
I named the subdirectories under $_SERVER['DOCUMENT_ROOT'] match with my subdomains like so:
/
var/
www/
html/
.htaccess
subdomain1.domain.com/
subdomain2.domain.com/
subdomain3.domain.com/
Where /var/www/html stand as 'DOCUMENT_ROOT'. Then put following code in the .htaccess file:
RewriteEngine on
RewriteCond %{REQUEST_URI} !^/%{HTTP_HOST}/
RewriteRule (.*) /%{HTTP_HOST}/$1 [L]
It works as redirect wildcard subdomains to subdirectories, without changing URL in address bar.
Beside of vhost, you may also put the subdirectories outside root and access it using alias as described here. Then put the same .htaccess code in that location.