Redirect to new domain using https Apache - apache

I've got this domain:
www.example.com
And I want to redirect it to
https://www.example2.com
This is what I have in my .htaccess
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{HTTP_HOST} ^example\.com$ [OR]
RewriteCond %{HTTP_HOST} ^www\.example\.com$
RewriteRule (.*)$ https://www.example2.com/$1 [R=301,L]
#Joomla Rules
RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]
RewriteCond %{REQUEST_URI} !^/index\.php
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule .* index.php [L]
</IfModule>
If I try from www.example.com the rules are applied it and redirects me, but when I try from example.com it shows me an insecurity error (NET::ERR_CERT_AUTHORITY_INVALID). How can I make it work both ways?
P.S. both domains point to the same IP

Check the below code for redirect one domain to another domain :
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{HTTP_HOST} ^example\.com$ [OR]
RewriteCond %{HTTP_HOST} ^www\.example\.com$
RewriteRule (.*)$ http://www.example2.com/$1 [R=301,L]
</IfModule>
OR
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{HTTP_HOST} ^(www\.)?example.com [NC]
RewriteRule ^(.*)$ http://www.example2.com/$1 [R=301,NC,L]
</IfModule>
I hope it's helpful to you.

Turns out I cannot just redirect to an SSL domain if the redirected domain isn't SSL as well, this post gave me the answer.
Nginx HTTPS 301 redirect to another TLD (hosted on same server) without showing SSL warning
Thank you!

Related

Redirects aren't working, is there a conflict with the rules or order of them?

I need to redirect any incoming traffic that is not www or https, to www and https.
The one issue is trying to add one more rule that would handle all traffic that used to be at rootdomain.com/blog/rest-of-url-title and send that to the subdomain https://blog.rootdomain.com/rest-of-url-title
This is what I have in my root domain htaccess
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^/blog/(.*)$ https://blog.rootdomain.com/$1 [R=302,L]
RewriteCond %{HTTPS} off [OR]
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteCond %{HTTP_HOST} ^(?:www\.)?(.+)$ [NC]
RewriteRule ^ https://www.%1%{REQUEST_URI} [L,NE,R=302]
</IfModule>
Then within the blog subdomain directory htaccess I have this (to redirect any non https to https and the rest is Wordpress based)
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteCond %{HTTPS} off
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
When trying to hit any url that is rootdomain.com/blog/rest-of-url-title it redirects to
https://www.rootdomain.com/index.php
Instead of redirecting to the blog subdomain, it just goes to www and since that url title doesn't exist for www i just get 404.
I had to go with mod_alias redirectmatch to get it working how i wanted
RedirectMatch 302 ^/blog/(.*)$ https://blog.rootdomain.com/$1
It's odd because the mod_rewrite line in my original htaccess in root had this:
RewriteRule ^blog/(.*) https://blog.rootdomain.com/$1 [R=302,L]
Which did not work on this particular server, but worked fine on any other servers I tested on. Maybe because of a subdomain/directory mapping issue?
Regardless, I know it's not good practice to mix mod_rewrite and mod_alias, this final worked for me:
RedirectMatch 302 ^/blog/(.*)$ https://blog.skodaminotti.com/$1
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteCond %{HTTPS} off [OR]
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteCond %{HTTP_HOST} ^(?:www\.)?(.+)$ [NC]
RewriteRule ^ https://www.%1%{REQUEST_URI} [L,NE,R=302]
</IfModule>
Would still love to hear if anyone knows why the rewriterule wouldn't work.

Stacking 301 redirects? .net to .com, www, https

I have been struggling getting this chain to work:
I want to point example.net to example.com and point everything to https://www.example.com.
So, we have: example.net, www.example.net, example.com, and www.example.com that all need to land on https://www.example.com.
I can get everything but https to work or https for the .com site only.
Any way to set this up?
First to redirect example.net to example.com , put the following code in example.net main root .htaccess file :
RewriteEngine On
RewriteRule (.*) https://www.example.com/$1 [R=301,NE,L]
Then in example.com main root .htaccess file put the following code :
RewriteEngine On
RewriteCond %{HTTPS} off [OR]
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule ^(.*)$ https://www.example.com/$1 [L,R=301]
Note: clear your browser cache and test it .
I was finally able to get to this work with:
<IfModule mod_rewrite.c>
Options +FollowSymLinks
RewriteEngine On
RewriteCond %{HTTP:X-Forwarded-Proto} !https [NC,OR]
RewriteCond %{HTTP_HOST} ^example.net [NC,OR]
RewriteCond %{HTTP_HOST} ^www.example.net [NC]
RewriteRule ^(.*)$ https://www.example.com/$1 [R=301,L]
#WordPress code
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>

.htaccess non-www to www and http to https redirects

For some reason my redirects are not working. I just want non-www to go to www and non-https to go to https. Here's what I've got. It's from this post.
RewriteCond %{HTTP_HOST} ^example\.com$ [NC]
RewriteRule (.*) https://www.example.com/$1 [R=301,L]
RewriteCond %{HTTP_HOST} ^www\.example\.com$ [NC]
RewriteCond %{HTTP:X-Forwarded-Proto} !=https
RewriteRule (.*) https://www.example.com/$1 [R=301,L]
EDIT: Here is my whole file.
# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteCond %{HTTPS} Off
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
RewriteCond %{HTTP_HOST} blog.example.com
RewriteRule ^(.*)$ https://www.example.com/blog/$1 [R=permanent,L]
RewriteCond %{HTTP_HOST} ^example.com/blog/$ [NC]
RewriteRule (.*) https://www.exmaple.com/blog/$1 [R=301,L]
RewriteCond %{HTTP_HOST} ^www.example.com/blog/$ [NC]
RewriteCond %{HTTP:X-Forwarded-Proto} !=https
RewriteRule (.*) https://www.example.com/blog/$1 [R=301,L]
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
# END WordPress
I just want non-www to go to www and non-https to go to https.
What you had initially was close, but it depends on how the SSL cert is managed. If the SSL is managed by a front-end proxy (eg. Cloudflare flexible-SSL/FREE option) then it maybe OK. However, if the SSL cert is installed directly on your server then probably not.
Try something like:
# Non-www to www
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule (.*) https://www.%{HTTP_HOST}/$1 [R,L]
# Non-SSL to SSL
RewriteCond %{HTTPS} off
RewriteRule (.*) https://%{HTTP_HOST}/$1 [R,L]
This assumes you only have example.com and www.example.com, and no other subdomains. However, in your edited code you make reference to a blog subdomain - this will be OK, provided you include the blog redirect first.
This is currently a temporary (302) redirect. Change the R to R=301 (permanent) only when you are sure it's working OK.
Make sure you browser catch is clear before testing.
Summary
The complete code would look something like:
# Redirect blog subdomain to main domain (required?)
RewriteCond %{HTTP_HOST} ^(www\.)?blog\.example\.com
RewriteRule (.*) https://www.example.com/blog/$1 [R=301,L]
# Non-www to www
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule (.*) https://www.%{HTTP_HOST}/$1 [R=301,L]
# Non-SSL to SSL
RewriteCond %{HTTPS} off
RewriteRule (.*) https://%{HTTP_HOST}/$1 [R=301,L]
# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
# END WordPress
It is best not to edit between the # BEGIN WordPress blocks to avoid your custom code being overwritten by WordPress.
Only include the RewriteBase directive once in your code (only the last instance will do anything). Currently, the RewriteBase directive is not actually doing anything anyway, since you have no relative path substitutions.

Redirect all traffic to bare domain SSL using htaccess

I'm trying to redirect all traffic through HTTPS and here's my htaccess file:
<IfModule mod_rewrite.c>
Options +FollowSymLinks -Indexes
RewriteEngine on
RewriteCond %{HTTPS} off
RewriteCond %{HTTP_HOST} ^www\.(.+)$ [NC]
RewriteRule ^ https://%1%{REQUEST_URI} [R=301,L]
RewriteBase /
RedirectMatch 301 /index.php/(.*)$ /$1
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?/$1 [L]
</IfModule>
<IfModule !mod_rewrite.c>
ErrorDocument 404 index.php
</IfModule>
Currently here's what's going on,
If the user types in www.domain.com it redirects to: https://domain.com which is good. I would like help in configuring the file so that if the user also types in domain.com they get redirected to https://domain.com
It would be as simple as updating your pattern:
RewriteEngine on
RewriteCond %{HTTPS} off
RewriteCond %{HTTP_HOST} ^(?:www\.)?(.+)$ [NC]
RewriteRule ^ https://%1%{REQUEST_URI} [R=301,L]

mod_rewrite to ssl not working for example.com but does for example.com/index.html

NOTE: config and error updated to be clearer.
I have the following .htaccess for my site running apach 2.4 on Ubuntu:
<IfModule mod_rewrite.c>
RewriteEngine on
# Redirect to HTTPS
RewriteCond %{HTTPS} off
RewriteCond %{HTTP:X-Forwarded-Proto} !https
RewriteCond %{HTTP_HOST} ^(www\.)example1.com [OR]
RewriteCond %{HTTP_HOST} ^(www\.)example2.com
RewriteRule ^(.*)$ https://www.example2.com/$1 [L,R=301]
</IfModule>
If I go to http://example2.com, http://www.example2.com orhttp://www.example1.comit redirects tohttps`
if I go to http://example1.com it stays as http.
Is there something I am missing so that it will redirect with just the base / url?
The problem is these two rules:
RewriteCond %{HTTP_HOST} ^(www\.)example1.com [OR]
RewriteCond %{HTTP_HOST} ^(www\.)example2.com
You need to add ? to make the www. optional:
RewriteCond %{HTTP_HOST} ^(www\.)?example1.com [OR]
RewriteCond %{HTTP_HOST} ^(www\.)?example2.com