redirecting from www to non-www urls with .htaccess - apache

I am redirecting an application with following code on my .htaccess file, the page is supposed to do the followings:
replace .php extension with .html
redirect from http to https
redirect from www to non-www urls
The extension .html is working fine and it is redirecting from http to https but the issue is to redirect from www to non-www, it is working properly on main url but when there is reference to a file then it is not working.
Say when i write www.ntestechnologies.com i get my desire url that is https://ntestechnologies.com but when i write www.ntestechnologies.com/index.html i get this https://www.ntestechnologies.com/index.html i don't need the www in this url as well please guide me, here is the code on htaccess file:
RewriteEngine On
RewriteCond %{HTTPS} off
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI}
RewriteEngine on
RewriteCond %{HTTP_HOST} ^www\.ntestechnologies\.com$
RewriteRule ^/?$ "https\:\/\/ntestechnologies\.com\/$1" [R=301,L]
RewriteRule ^(.*)\.html$ $1.php [nc]

You need only one RewriteEngine On.
You cannot use HTTP_HOST or REQUEST_URI in a RewriteRule. If you need to capture these values, you must do so in a RewriteCond
RewriteCond %{HTTPS} off
RewriteCond %{HTTP_HOST} ^(?:www\.)(.+)
RewriteRule .* https://%1/$0 [R,L]
RewriteCond %{HTTP_HOST} ^www\.(.+)
RewriteRule .* https://%1/$0 [R,L]
This removes the leading www, if present. At the same time, it redirects to HTTPS.

RewriteEngine On
# Redirects from HTTP to HTTPS. We use %{SERVER_PORT} as it's more reliable than %{HTTPS}
RewriteCond %{SERVER_PORT} !^443$
RewriteRule .* https://%{HTTP_HOST}%{REQUEST_URI} [R,L]
# Redirects www.example.com/... to example.com/...
RewriteCond %{HTTP_HOST} ^www\.(.+)
RewriteRule .* https://%1%{REQUEST_URI} [R,L]
RewriteRule ^(.*)\.html$ $1.php [nc]

Related

Using multiple rewrite rules .htaccess

I made the switch of my website from http to https. Now, I want to 301 redirect all http content to https.
I want to use this rule:
RewriteEngine On
RewriteCond %{HTTPS} off
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
The issue is I also have another rule already in place which redirects all non-www pages to www ones.
RewriteEngine On
RewriteCond %{HTTP_HOST} ^example.com [NC]
RewriteRule ^(.*) http://www.example.com/$1 [last,redirect=301]
How can I combine it so whatever link users write (http non-www ; http www ; https non-www) all redirect to https://www.example.com
Cheers!
You can use this single rule to add www and http->https in single rule:
RewriteEngine On
RewriteCond %{HTTP_HOST} !^www\. [NC,OR]
RewriteCond %{HTTPS} off
RewriteCond %{HTTP_HOST} ^(?:www\.)?(.+)$ [NC]
RewriteRule ^ https://www.%1%{REQUEST_URI} [R=301,L,NE]
This rule should be kept just below RewriteEngine line and make sure to clear your browser cache when testing this change.

Redirect subdomain to folder infinitely redirects due to www rule

I'm trying to add a single subdomain to an existing website. We've added the appropriate 'A' record. We want staging.example.com to display the contents from example.com/test/ (and remain staging.example.com).
The .htaccess file (written by someone else) redirects all non-https to https, and all non-www to www:
RewriteEngine On
RewriteBase /
#Rewrite http to https
RewriteCond %{HTTPS} off
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} [L]
#Rewrite non-www to www
RewriteCond %{HTTP_HOST} !^www\.(.+)$ [NC]
RewriteRule ^(.*)$ http://www\.%{HTTP_HOST}/$1 [R=301,L]
I tried to add a line like the following, just before the last RewriteRule:
RewriteCond %{HTTP_HOST} !^staging\.(.+)$ [NC]
However, it always gets in a redirect loop to www.staging.example.com ...
Suggestions much appreciated.
This is because you are directing BACK to HTTP with your WWW redirect. Change it to https:
EDIT
I would also invert your statments .. This is EXACTLY what I use -- And I can verify it works for me:
RewriteEngine On
# to www
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule ^ https://www.%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
# to https
RewriteCond %{HTTP:X-Forwarded-Proto} !https
RewriteCond %{HTTPS} off
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]

Redirect all from http to https except contact page

How do I redirect all pages on a site from https to http except the /contact page which I always want to be redirected to the https domain?
I can redirect all from http to https as follows:
RewriteEngine On
RewriteCond %{HTTPS} on
RewriteRule (.*) http://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
However, I'm having trouble adding the exception for the contact page which I want to always redirect from http to https
update
I've added the rules as suggested but /contact is now redirecting to /index.php?q=contact.
I'm using ModX which has the rules:
# The Friendly URLs part
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?q=$1 [L,QSA]
Why is this conflicting with the new rule to redirect /contact (http) to /contact (https)?
Thanks :)
Use this:
RewriteEngine On
RewriteCond %{HTTPS} =off
RewriteCond %{REQUEST_URI} !^\/contact\/
RewriteRule (.*) https://%{HTTP_HOST}/$1 [L,R=301]
RewriteCond %{HTTPS} =on
RewriteCond %{REQUEST_URI} \/contact\/
RewriteRule (.*) http://%{HTTP_HOST}/$1 [L,R=301]
You can try this rules:
RewriteEngine On
RewriteBase /
RewriteCond %{HTTPS} on
RewriteCond %{REQUEST_URI} !^/contact$
RewriteRule (.*) http://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
RewriteCond %{HTTPS} off
RewriteRule ^contact$ https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
You can test your .htaccess file using this online tool (note that it doesn't includes all Apache functionalities and the result might slightly differ from your production server but works well for simple rules).
Try this:-
RewriteEngine On
RewriteBase /
//Turn https on for /page/contact
RewriteCond %{HTTPS} off
RewriteCond %{REQUEST_URI} ^/page/contact
RewriteRule ^(.*)$ https://%{HTTP_HOST}/$1 [R=301,L]
//Turn https off everthing but /page/contact
RewriteCond %{HTTPS} on
RewriteCond %{REQUEST_URI} !^/page/contact
RewriteRule ^(.*)$ http://%{HTTP_HOST}/$1 [R=301,L]
The first rule set will make sure the page /page/contact will redirected back to https if they are accessed via http. The second rule set will redirect all page to http that are accessed by https except /page/contact.

unwanted loop in .htaccess

I want to use mod_rewrite to redirect all Urls in http to https except with a single directory.
In other words:
Any request for http content will redirected to the content in https
Any request for content in the blog directory (URI starting with /blog/) will be redirected to its http equivalent.
I have implemented the following .htaccess:
RewriteEngine On
RewriteCond %{HTTPS} on
RewriteCond %{REQUEST_URI} ^/blog/
RewriteRule ^(.*)$ http://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
RewriteCond %{HTTPS} off
RewriteCond %{REQUEST_URI} !^/blog
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
Whenever I call a page with a request URI starting in /blog/ I get a redirect loop. I don't really understand why. What am I doing wrong?
Try this.
RewriteEngine On
RewriteCond %{HTTPS} off
RewriteCond %{REQUEST_URI} !blog [NC]
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]

htaccess help, need to force www, https, and remove index.php

I have a rewrite in my htaccess file that removes index.php from the url
RewriteEngine on
RewriteCond $1 !^(images|media|system|themes|_css|_js|favicon\.ico|robots\.txt|cert\.html|index\.php) [NC]
RewriteRule ^(.*)$ /index.php/$1 [L]
In addition to this, I want to force the www and https for any request that does not have either.
So ultimately all urls should look like this: https://www.example.com/whatever/something/; and for SEO purposes, if a url misses the mark, it should 301 redirect to it's correct version, for example:
http://example.com/about/
301 redirect to
https://www.example.com/about/
Would love some help accomplishing that, thanks!
Force WWW:
RewriteCond %{HTTP_HOST} !^www\.example\.com [NC]
RewriteRule ^(.*)$ https://www.example.com/$1 [R=301,L] # include 's' here to force ssl in addition to www
Force SSL:
RewriteCond %{SERVER_PORT} 80
RewriteRule ^(.*)$ https://www.example.com/$1 [R=301,L]
Remove "index.php":
RewriteCond %{THE_REQUEST} /index.php HTTP
RewriteRule (.*)index.php$ /$1 [R=301,L]