Is 'off' equal to '!on' ? in htaccess syntaxe? - .htaccess

Are the next two codes equivalent?
RewriteEngine On
RewriteCond %{HTTPS} off
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI}
And
RewriteEngine On
RewriteCond %{HTTPS} !on
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI}

Yes!
HTTPS
Will contain the text "on" if the connection is using SSL/TLS, or "off" otherwise. (This variable can be safely used regardless of
whether or not mod_ssl is loaded).
http://httpd.apache.org/docs/2.2/mod/mod_rewrite.html#rewritecond
The exclamation mark ! specifies a negative match, so the rule is only applied if HTTPS does not contain "on".

Related

.htaccess : how to bypass all rules when url matches?

I have an .htaccess file:
SetEnv PHP_VER 5_TEST
RewriteEngine On
#RewriteCond %{HTTPS} off
#RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
RewriteCond %{HTTPS} off
RewriteCond %{THE_REQUEST} !/pay|ok|ko|retour/ [NC]
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
RewriteCond %{HTTP_HOST} !^(blog|www|flashgames).sample.com$
RewriteRule ^(.*) http://www.sample.com/$1 [QSA,L,R=301]
ErrorDocument 404 https://www.sample.com/index.php
I would like to add that rule at the begib
when url is : http://www.sample.com/test.php then Returns (DO NOTHING), just bypass all rules.
Any idea ?
You can use a dash - as the substitution for RewriteRule
- (dash)
A dash indicates that no substitution should be performed (the existing path is passed through untouched). This is used when a flag (see below) needs to be applied without changing the path.
With an L|last flag, it will skip all following rules. This means, when you insert a rule at the beginning
RewriteRule ^test\.php$ - [L]
it will just process test.php, as if there were no rules at all.
When you insert this rule somewhere in the middle, Apache will process all rules up to this point and then exit the rule chain.

.htaccess Force SSL (HTTPS) On all pages besides a few

Ok, so i need to forward all traffic through HTTPS besides on ONE specific page. Here is my current .htaccess code:
RewriteEngine On
RewriteCond %{SERVER_PORT} 80
RewriteRule ^(.*)$ https://www.example.com/$1 [R=301,L]
ErrorDocument 404 /404.html
Options -Indexes
So basically i need to forward all traffic through HTTPS BESIDES my /r.php & /l.php & /c.php page, can this be done? I tried doing some research but haven't found too much.
EDIT:
Would this work?
RewriteEngine On
RewriteCond %{HTTPS} off
RewriteCond %{REQUEST_URI} !^/r.php$
RewriteCond %{REQUEST_URI} !^/c.php$
RewriteCond %{REQUEST_URI} !^/l.php$
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
IT IS HIGHLY RECOMMENDED TO USE A SECURE CONNECTION IF IT IS AVAILABLE TO YOU
RewriteEngine On
RewriteCond %{HTTPS} off
#Add slash(es) before special characters to escape them as literals
RewriteCond %{REQUEST_URI} !^\/r\.php$
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
What this does is check if your HTTPS flag is set on the server, if it is not set, and the page is not /r.php then redirects the page to the secure HTTPS equivalant.
EDIT: The /r.php entity needs to have the / and . escaped by having these characters preceeded by backslashes \.
The RewriteCond line "escapes" the /r.php page reference from the Secure connection flag check, it is better to use the proper server flag as detailed here, rather than manual port requests because secure/insecure standard hypertext protocol ports can be ANY port on a server, and it is only convention (and not at all required) that TLS/HTTP ports are 443/80 etc.
You can add a new RewriteRule above your RewriteCond telling apache to process it as is and not applying any more RewriteRules (the L says, stop after matching this rule, (r|c|l) is a regular expression for matching r OR c OR l).
RewriteRule ^/?(r|c|l)\.php$ - [L]
See https://httpd.apache.org/docs/current/mod/mod_rewrite.html#rewriterule for more information.

how can I make rewrite rule to write my domain name to have caps on certain letters?

for example:
mydomain.com/whatever
to show like
MyDomain.com/whatever
can i do with with a rewrite rule in htaccess?
I tried:
RewriteCond %{HTTP_HOST} !^www\.mrhowtosay\.com/([^&\s]+)
RewriteRule ^(.*)$ http://www\.MrHowToSay.com/$1 [R=301,L]
but it loops infinitly
The %{HTTP_HOST} variable is only the hostname submitted through the "Host:" request header. It doesn't contain any URI path info. So the /([^&\s]+) bit will cause the condition to guarantee fail (which negates to always "true"). Try:
RewriteCond %{HTTP_HOST} !^www\.MrHowToSay\.com$
RewriteRule ^(.*)$ http://www.MrHowToSay.com/$1 [R=301,L]

.htaccess RewriteRule : redirects http to https always (works fine), but 1 exclusion is still needed...

Seems like
RewriteCond %{HTTPS} off
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI}
works perfect and redirects all http requests to https pages.
As result,
http://mydomain.com/?p=abc
http://mydomain.com/?q=de
http://mydomain.com/?z=123
http://mydomain.com/
go to https://
https://mydomain.com/?p=abc
https://mydomain.com/?q=de
https://mydomain.com/?z=123
https://mydomain.com/
respectively.
How to keep http://mydomain.com/ stay with no redirection to https?
Criteria: if no parametres (no ?p, no ?q, no ?z etc.), then stay at http.
Thank you
If you want to stay on HTTP when the query string is empty, use solution #1 below.
If you only want to keep HTTP when p, q, or z params are present, comment out the #1 solution and uncomment out the #2 one.
RewriteCond %{HTTPS} off
#1 if query string is not empty
RewriteCond %{QUERY_STRING} !^$
#2 only if p or q or z params are not present
#RewriteCond %{QUERY_STRING} !(^|&)(p|q|z)= [NC]
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI}
Use this code for your conditions:
RewriteCond %{HTTPS} off
RewriteCond %{QUERY_STRING} !^$
RewriteCond %{REQUEST_URI} !^/*$
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]

how do i fix this rewrite URL rule?

basically need to convert
with www or not, example.com/[anycharacter]
into
with www or not, example.com/cgi-bin/new-disk.cgi/dir/smooth/[anycharacter]
additinoally...
i would like to redirect ALL www.example.com into example.com
This should work for you:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^www\.example\.com$ [NC]
RewriteRule ^(.*)$ http://example.com/$1 [R=301,L]
RewriteCond %{REQUEST_FILENAME} !cgi-bin/new-disk.cgi/dir/smooth
RewriteRule ^(.*)$ /cgi-bin/new-disk.cgi/dir/smooth/$1 [L]
For the first two lines, it checks to see if you have www in your URL. If so, bounce it to the non-www version.
Note the exclamation mark (!) on the second last line. This is a not operator and in this test, is checking to see if your currently requested file isn't your final rewriting file, in this case:
cgi-bin/new-disk.cgi/dir/smooth
If that's true, shunt it to the actual rewrite script you have as pointed out in the final line.
The character, $1, references the first capture group as marked by the first set of parentheses on the same line.
For part of your answer, I believe you can use this as an example to base off.. hopefully you use a test domain:
RewriteCond %{HTTP_HOST} ^www\.example\.com [NC]
RewriteRule ^/(.*) http://example.com/$1 [L,R=301]
Might need to add a (.*) and make it optional for the other part.

Resources