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

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.

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.

Getting .htaccess redirects from one domain to another to work for WWW ( both http://www. and https://www.), HTTP and HTTPS

I have a scenario for redirection I'm having trouble wrapping my head around.
The goals are:
old-domain.com/store needs to redirect to new-domain.com/store
There are three validation scripts that should remain on Old-Domain.com
Everything else from old-domain.com to redirect to new-domain.com
These rules need to apply to all scenarios HTTP, HTTPS and WWW (HTTP and HTTPS)
The problem I'm having is that the old-domain.com/store only properly redirects to new-domain.com/store when I remove the WWW redirects. I can't seem to get it to all play together nicely.
Here's what I've got:
Options +FollowSymLinks
## Rewrite rules
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} ^www.old-domain.com$
RewriteRule ^(.*)$ http://old-domain.com/$1 [R=301]
RewriteCond %{HTTP_HOST} ^www.old-domain.com$
RewriteRule ^(.*)$ https://old-domain.com/$1 [R=301]
RewriteCond %{REQUEST_URI} !^/validation-script-1\.html
RewriteCond %{REQUEST_URI} !^/validation-script-2\.html
RewriteCond %{REQUEST_URI} !^/validation-script-3\.html
Redirect 301 /store/ https://new-domain.com/store
RewriteRule ^(.*)$ https://new-domain.com/ [R=301,L]
</IfModule>
We've also tried the following but then the validation scripts stop passing through:
Options +FollowSymLinks
## Rewrite rules
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REQUEST_URI} !^/validation-script-1\.html
RewriteCond %{REQUEST_URI} !^/validation-script-2\.html
RewriteCond %{REQUEST_URI} !^/validation-script-3\.html
# Remove trailing slash from non-filepath urls
RewriteCond %{REQUEST_URI} /(.+)/$
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ http://old-domain.com/%1 [R=301,L]
# Include trailing slash on directory
RewriteCond %{REQUEST_URI} !(.+)/$
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^(.+)$ http://old-domain.com/$1/ [R=301,L]
RewriteRule ^/?store https://new-domain.com/store [R=301,L]
# Remove WWW
RewriteCond %{HTTP_HOST} ^www\.(.*)$ [OR,NC]
RewriteRule ^(.*)$ https://new-domain.com/ [R=301,L]
</IfModule>
The following rewrite rule will redirect all (www.)old-domain.com URLs to https://new-domain.com/ except URLs containing validation-script-(1|2|3).html
RewriteEngine On
RewriteCond %{REQUEST_URI} !^/validation-script-(1|2|3)\.html$
RewriteCond %{HTTP_HOST} ^(www\.)?old-domain\.com$
RewriteRule ^(.*)$ "https\:\/\/new-domain\.com\/$1" [R=301,L]
Check this demo https://htaccess.madewithlove.be?share=f0deda97-dff8-569e-bd6f-411b8a779361
Edit:
to redirect old-domain.com/store or old-domain.com/store/ to new-domain.com/pages/store
RewriteRule ^store/?$ "https\:\/\/new-domain\.com\/pages\/store" [R=301,L]

HTAccess Mod_Rewrite HTTPS WWW Keeping URL

I'm hoping someone here will be able to help me as I'm slowly losing my mind trying to get a rewrite rule working in HTAccess.
All I need is for 3 things to happen.
1) All www. traffic should go to non-www.
2) All non-https traffic should go to https.
3) All URLs should be preserved through these rewrites.
I've tried countless different examples online (and my own bastardised variations on them) without achieving the results I'm after. The site uses Joomla, so there's a fair amount of crap that Joomla includes as standard, but I'm pretty sure none of that is interfering with it.
With the current HTAccess the 'www.' is being dropped and the user is forced to https, but the URL is being lost.
Thanks in advance for your assistance!
My HTAccess file is here:
AddHandler application/x-httpd-php53 .php
ErrorDocument 401 "Authorisation Required"
##
# Joomla Crap START
##
RewriteEngine On
RewriteCond %{QUERY_STRING} base64_encode[^(]*\([^)]*\) [OR]
RewriteCond %{QUERY_STRING} (<|%3C)([^s]*s)+cript.*(>|%3E) [NC,OR]
RewriteCond %{QUERY_STRING} GLOBALS(=|\[|\%[0-9A-Z]{0,2}) [OR]
RewriteCond %{QUERY_STRING} _REQUEST(=|\[|\%[0-9A-Z]{0,2})
RewriteRule .* index.php [F]
RewriteBase /
#RewriteCond %{HTTP_HOST} !^www\.
#RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,L]
RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]
RewriteCond %{REQUEST_URI} !^/index\.php
RewriteCond %{REQUEST_URI} /component/|(/[^.]*|\.(php|html?|feed|pdf|vcf|raw))$ [NC]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule .* index.php [L]
##
# Joomla Crap END
##
# rewrite www.mywebsite.co.uk > mywebsite.co.uk
RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC]
RewriteRule ^(.*)$ https://%1/$1 [R=301,L]
# rewrite http > https
RewriteCond %{HTTPS} off
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} [R,L]
Redirect 301 /~fitspace https://mywebsite.co.uk
RedirectMatch 301 ^/index.php/(.*)$ https://mywebsite.co.uk/$1
Try :
RewriteEngine on
RewriteCond %{HTTP_HOST} ^www\.(.+)$ [OR]
RewriteCond %{HTTPS} off
RewriteRule ^ https://%1%{REQUEST_URI} [L,R]
The rule above will redirect
http://www.example.com/
to
https://example.com/
And
https://www.example.com/
to
https://example.com/

url rewriting to redirect www to non www on https

I just changed my website http to https. I'll already redirected http to https version. I also want to redirect www version to non www.
So far couldn't succeed it.
http://www.domain.com => https://domain.com
https://www.domain.com => https://domain.com
Here is the .htaccess file;
<ifModule mod_rewrite.c>
RewriteEngine On
redirect 301 /makale /sozluk
RewriteCond %{REQUEST_FILENAME} !-s
RewriteRule ^(.*)\.[\d]+\.(css|js)$ $1.$2 [L]
RewriteCond %{HTTPS ^www.sporapp.com$ [NC]
RewriteRule ^(.*)$ https://sporapp.com/$1 [R=301,L]
RewriteCond %{HTTPS} !=on
# This checks to make sure the connection is not already HTTPS
RewriteRule ^/?(.*) https://%{SERVER_NAME}/$1 [R,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ index.php/$1 [NC,QSA,L]
</ifModule>
Thank you.
You should remove this bit:
RewriteCond %{HTTPS ^www.sporapp.com$ [NC]
RewriteRule ^(.*)$ https://sporapp.com/$1 [R=301,L]
RewriteCond %{HTTPS} !=on
# This checks to make sure the connection is not already HTTPS
RewriteRule ^/?(.*) https://%{SERVER_NAME}/$1 [R,L]
And add this just under the RewriteEngine On:
RewriteCond %{HTTPS} off [OR]
RewriteCond %{HTTP_HOST} ^www\.sporapp\.com$ [NC]
RewriteRule ^(.*)$ https://sporapp.com/$1 [R=301,L]
You need to have the redirecting happening before you rewrite, otherwise the request gets changed before it gets redirected.
Also, you have mod_alias directives which will interfere with the rewrite to /index.php:
redirect 301 /makale /sozluk
Should be:
RewriteRule ^makale/(.*)$ /sozluk/$1 [L,R=301]

How to redirect specific links to https:// everything else to http:// using .htaccess?

I am trying to use htaccess to automatically direct all requests to certain pages to https and everything else to http://
Here is the code I've got the code to force ssl working below
RewriteEngine on
RewriteBase /
RewriteCond %{SERVER_PORT} !^443$
RewriteRule ^va-homebuyers-guide https://domain.com/va-homebuyers-guide/ [R=301,L]
RewriteCond %{SERVER_PORT} !^443$
RewriteRule ^va-purchase-request https://domain.com/va-purchase-request/ [R=301,L]
RewriteCond %{SERVER_PORT} !^443$
RewriteRule ^va-streamline-refinance https://domain.com/va-streamline-refinance/ [R=301,L]
This is working as expected and redirects to the ssl versions of those pages. I then tried adding the following to redirect everything else to http:
RewriteCond %{SERVER_PORT} !^80
RewriteCond %{REQUEST_URI} !^va-homebuyers-guide$
RewriteCond %{REQUEST_URI} !^va-purchase-request$
RewriteCond %{REQUEST_URI} !^va-streamline-refinance$
RewriteRule ^(.*)$ http://domain.com/$1 [R,L]
This code results in a redirect loop when going to /va-purchase-request /va-homebuyers-guide and /va-streamline-refinance and does not redirect the other pages at all.
I'm totally stuck with this and any help would be massively appreciated!
Edit:
I also have this code in the .htaccess file added by wordpress... Could it be interfering with the other redirects?
# 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
Replace your .htaccess code with this:
Options +FollowSymLinks -MultiViews
# Turn mod_rewrite on
RewriteEngine On
RewriteBase /
RewriteCond %{HTTPS} off
RewriteRule ^va-(homebuyers-guide|purchase-request|streamline-refinance)(/.*|)$ https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L,NC]
RewriteCond %{HTTPS} on
RewriteRule (?!^va-(homebuyers-guide|purchase-request|streamline-refinance)(/.*|)$)^.*$ http://%{HTTP_HOST}%{REQUEST_URI} [R=301,L,NC]