Redirects in Apache - apache

Can anyone help.
Im need to write a redirect rule that :
redirects all HTTPS traffic back to HTTP unless it has the URL /administrator within it.
RewriteEngine on
RewriteCond %{REQUEST_URI} !^administrator/
RewriteRule .? http://www.site2.com%{REQUEST_URI} [R=301,L]
Thanks,

You need to include a condition that checks if it's HTTPS:
RewriteEngine on
RewriteCond %{HTTPS} on
RewriteCond %{REQUEST_URI} !^administrator/
RewriteRule .? http://www.site2.com%{REQUEST_URI} [R=301,L]

Related

www and non-www http requests to https://www with full path

I created redirect from non-www and www http request to https and www request that was quite easy. I used following rules in .htaccess
RewriteEngine On
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule ^(.*)$ https://www.%{HTTP_HOST}%{REQUEST_URI}$1 [R=301,L]
RewriteCond %{HTTPS} off
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI}$1
But now the problem is when someone enters old non-secure url like domain and project-about.html at the end .htaccess redirect to https but adds request at the end again so the request after the domain look like:
/project-about.htmlproject-about.html
How can I fix it? Thanks
Change it to this:
RewriteEngine On
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule ^ https://www.%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
RewriteCond %{HTTPS} off
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
Use this:
RewriteEngine On
RewriteCond %{HTTP_HOST} !^www\. [NC,OR]
RewriteCond %{HTTPS} !=on
RewriteRule ^ https://www.example.com%{REQUEST_URI} [R=301,L,NE]
This method removes the need for an extra RewriteRule also, helping to speed up your website.
Make sure you clear your cache before testing this.

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]

redirecting from www to non-www urls with .htaccess

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]

Stop .htaccess redirecting everything to

I have setup my .htaccess to redirect requests from http ://subdomain.domain.com.tld, http ://www.subdomain.domain.com.tld and http ://domain.com.tld/folder (folder that contains contents of subdomain) to https ://subdomain.domain.com.tld so my single domain SSL certificate setup for subdomain.domain.com.tld is used at all times.
My problem is http ://domain.com.tld also redirects to https ://subdomain.domain.com.tld. How do I stop this?
RewriteOptions inherit
Options +FollowSymlinks
RewriteEngine on
RewriteCond %{HTTP_HOST} .
RewriteCond %{HTTP_HOST} !^subdomain.domain\.com.tld
RewriteRule ^(.*)$ https://subdomain.domain.com.tld/$1 [R=301,L]
RewriteCond %{HTTP_HOST} ^.*$
RewriteRule ^folder$ "https\:\/\/subdomain\.domain\.com\.tld\/" [R=301,L]
Thank you.
These rules should replace everything after RewriteEngine on:
RewriteCond %{HTTP_HOST} ^(www\.)?subdomain\.domain\.com\.tld$ [NC]
RewriteRule ^(.*)$ https://subdomain.domain.com.tld$1 [R=301,L]
RewriteCond %{HTTP_HOST} ^domain\.com\.tld$ [NC]
RewriteRule ^folder/?(.*)$ https://subdomain.domain.com.tld$1 [R=301,L]
The first set of rules matches http://subdomain.domain.com.tld and http://www.subdomain.domain.com.tld
The second set matches http://domain.com.tld/folder, but does not match http://domain.com.tld without the "folder" part.
Edit: Corrected typo as noted in comments and escaped dots in RewriteConds.