force non https version of website - apache

After installing ssl and making all pages https by using htaccess I realised that my website actually cannot run with https because it has a lot of embeded code with http protocol which cannot be changed to https or removed, but simply removing the rewrite rule which forces https does not work because google has indexed all pages with https, so now most part of the google search traffic goes to https version of website and they see mixed content error.
I tried to implement examples that I found in forums but when I apply this code
RewriteCond %{HTTPS} on [NC]
RewriteRule ^(.*)$ http://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
the browser always redirects from https://example.com/anypage to http://example.com/index.php
Here is the full code from .htaccess file
<IfModule mod_rewrite.c>
<IfModule mod_negotiation.c>
Options -MultiViews
</IfModule>
RewriteEngine On
# Redirect Trailing Slashes If Not A Folder...
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)/$ /$1 [L,R=301]
# Handle Front Controller...
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [L]
# Handle Authorization Header
RewriteCond %{HTTP:Authorization} .
RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]
#Force non-www:
RewriteCond %{HTTP_HOST} ^www\.example\.com[NC]
RewriteRule ^(.*)$ http://example.com/$1 [L,R=301]
RewriteCond %{HTTPS} on [NC]
RewriteRule ^(.*)$ http://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
</IfModule>

Related

My fowards do not seem to work in .htaccess

I have an issue with an apache .htaccess redirection on a laravel site
My forwards do not seem to work
here is what I want:
Foward www to non-www www.ReplaceMe.com to ReplaceMe.com
Foward HTTP to https http://ReplaceMe.com to https://ReplaceMe.com
Foward non-trailing / to add trailing / https://ReplaceMe.com to https://ReplaceMe.com/
so the result would be.
https://www.ReplaceMe.com/about-us TO https://ReplaceMe.com/about-us/
But it does not seem to be working.
Below Code i am using now:
<IfModule mod_rewrite.c>
<IfModule mod_negotiation.c>
Options -MultiViews -Indexes
</IfModule>
RewriteEngine On
# Handle Authorization Header
RewriteCond %{HTTP:Authorization} .
RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]
# Redirect Trailing Slashes If Not A Folder...
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} (.+)/$
RewriteRule ^ %1 [L,R=301]
# Send Requests To Front Controller...
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [L]
# www to non-www
RewriteCond %{HTTP_HOST} ^www.ReplaceMe\.com$ [NC]
RewriteRule ^(.*)$ https://ReplaceMe.com/$1 [R=301,L]
# http to https
RewriteCond %{HTTPS} !=on
RewriteRule ^/?(.*) https://%{SERVER_NAME}/$1 [R=301,L]
</IfModule>
As far as I know, this should work. Can anyone spot my Issue?

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.

Debug .htaccess file I inherited

I inherited the following .htaccess file from the previous web developer. I am totally baffled by some of the rules here and getting endless loops on things.
<IfModule mod_security.c>
SecRuleRemoveByID 000014
</IfModule>
RewriteEngine On
RewriteBase /
RewriteCond %{SERVER_PORT} 80
RewriteRule ^(.*)$ https://www.example.com/$1 [R,L]
RewriteCond %{HTTP_HOST} ^example.com [NC]
RewriteRule ^(.*)$ http://www.example.com/$1 [L,R=301,NC]
# STRIP .HTML FROM DISPLAY
RewriteCond %{REQUEST_URI} !^/(dashboard|editor/.*)$
RewriteCond %{THE_REQUEST} ^(.+)\.html([#?][^\ ]*)?\ HTTP/
RewriteRule ^(.+)\.html$ /$1 [R=301,L]
RewriteCond %{REQUEST_URI} !^/(dashboard|editor/.*)$
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^/]+)/$ $1.html
RewriteRule ^([^/]+)/([^/]+)/$ /$1/$2.html
RewriteCond %{REQUEST_URI} !^/(dashboard|editor/.*)$
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !(\.[a-zA-Z0-9]{1,5}|/)$
RewriteRule (.*)$ /$1/ [R=301,L]
What I'm trying to accomplish is the following:
Append www. to all incoming URLs
Redirect all http requests to https
Display all incoming .html URLs without the .html
We have an angular app in /dashboard and /editor and we need to make sure that we exclude those directories from any rewrite rules.
PLEASE HELP!
turn on debugging via
LogLevel alert rewrite:trace6
(on apache2.4 and up)
and trace it. see
How to debug Apache mod_rewrite
I can imagine several bad news cases, including where apache is behind some sort of reverse proxy, talking to it via port 80 so the http->https rule gets re-invoked forever.

Htaccess URLs redirects are working for http not all https

newbie on htaccess and redirects, I hope someone can help me with this. At the moment all the http urls are redirecting correctly. E.g. if you go to otherdomain.com it will take you to the maindomian.com.
This is currently not working for all of the https links.
For example:
http - both www and non www otherdomain.com/blog will redirect to https://www.mainwebsite.com/blog
Which is what I need, however as soon as I get to the https it does the following:
https - both www and non www otherdomain.com/blog will remain on the https otherdomain.com/blog domain.
Which is wrong because it needs to go to the main website's blog page. But if I have manually added a redirect on a page all the http and https urls will work:
E.g.
both www and non www
http://www.otherdomain.com/newpage will redirect to https://www.mainwebsite.com/welcome
https://www.otherdomain.com/newpage will redirect to https://www.mainwebsite.com/welcome
It's Drupal 7, here is the code that I currently have:
Redirect /newpage https://www.mainwebsite.com/welcome
RewriteEngine on
RewriteBase /
# Force HTTPS
RewriteCond %{HTTPS} off
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
# Remove trailing slash
RewriteRule ^(.*)/$ https://%{HTTP_HOST}/$1 [R=301,L]
RewriteCond %{HTTP_HOST} ^mainwebsite.com$ [NC]
RewriteRule ^ https://www.%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
# Set "protossl" to "s" if we were accessed via https://. This is used later
# if you enable "www." stripping or enforcement, in order to ensure that
# you don't bounce between http and https.
RewriteRule ^ - [E=protossl]
RewriteCond %{HTTPS} on
RewriteRule ^ - [E=protossl:s]
# Make sure Authorization HTTP header is available to PHP
# even when running as CGI or FastCGI.
RewriteRule ^ - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]
# Pass all requests not referring directly to files in the filesystem to
# index.php. Clean URLs are handled in drupal_environment_initialize().
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !=/favicon.ico
RewriteRule ^ index.php [L]
Any idea how I can get any url from the https otherdomain.com to go to the mainwebsite.com? Any help would be greatly appreciated.
RewriteEngine on
RewriteCond %{HTTP_HOST} ^othersite.com [NC,OR]
RewriteCond %{HTTP_HOST} ^www.othersite.com [NC]
RewriteRule ^(.*)$ https://www.mainwebsite.com/$1 [L,R=301,NC]
That will redirect all requests, not just /blog
if you want just /blog to redirect then I would adjust the RewriteRule a little
RewriteRule ^blog/?$ http://www.newsite.com/blog [L,R=301,NC]
Práctica recomendada: redirección 301 de HTTP to HTTPS (dominio estándar)
en mi caso para mi este es el mejor
in my case for me this is the best
RewriteEngine On
RewriteCond %{HTTPS} off
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
RewriteCond %{HTTP_HOST} ^www\.(.+)$ [NC]
RewriteRule ^ https://%1%{REQUEST_URI} [R=301,L]
RewriteCond %{HTTP_HOST} ^108\.161\.161\.46
RewriteRule (.*) https://ayuda.ml/$1 [R=301,L]
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ 404.php?%{QUERY_STRING} [NE,L]
RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization},L]

.htaccess ensure www redirect incorrect

I am trying to ensure that urls always have www in front of them for canonicalization reasons.
Unfortunately when I put the following url in:
http://website.net/handbags/1/12/this-is-some-text
It redirects me here:
http://www.website.net/?controller=handbags&path=1/12/this-is-some-text
I would like to add it works fine when using:
http://www.website.net/handbags/1/12/this-is-some-text
I want it to redirect me here...
http://www.website.net/handbags/1/12/this-is-some-text
I'm not sure what could be causing this error. Here is my .htaccess file:
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
# CORE REDIRECT
RewriteRule ^([a-zA-Z]*)/?(.*)?$ index.php?controller=$1&path=$2 [NC,L]
# ENSURE WWW
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteCond %{HTTP_HOST} ^([^.]+\.[a-z]{2,6})$ [NC]
RewriteRule ^(.*)$ http://www.%1/$1 [R=301,L]
</IfModule>
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteBase /
# ENSURE WWW
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteCond %{HTTP_HOST} ^([^.]+\.[a-z]{7})$ [NC]
RewriteRule ^(.*)$ http://www.%1/$1 [R=301,L]
# CORE REDIRECT
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([a-zA-Z]*)/?(.*)?$ index.php?controller=$1&path=$2 [NC,L]
</IfModule>
You can do what I've done above; however, it's just going to redirect to http://www.website.net/handbags/1/12/this-is-some-text and then it's immediately going to hit the "CORE REDIRECT" rule and send you to http://www.website.net/?controller=handbags&path=1/12/this-is-some-text.
Is there a reason you don't want it to redirect to "?controller=handbags" when they go to www?
UPDATE
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteBase /
# ENSURE WWW
Rewritecond %{HTTP_HOST} !^www\.website\.net
RewriteRule ^(.*)$ http://www.website.net/$1 [R=301,L]
# CORE REDIRECT
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([a-zA-Z]*)/?(.*)?$ index.php?controller=$1&path=$2 [NC,L]
</IfModule>
Whenever your # CORE REDIRECT matches, the other rules are not even checked, as the core redirect rule has the Lflag set. That ones tells Apache to stop trying other rules. Remove that flag or change the rule. ;)