How can write this pseudocode in .htaccess:
if (ip == "127.0.0.1") or (dns = "localhost") {
// all to http://
} else {
// all to https://
}
Shortly, need http for local and https for remote.
Thanks.
You can use this :
RewriteEngine on
#if http host =127.0.0.1 or localhost
RewriteCond %{HTTP_HOST} ^127\.0\.0\.1$ [OR]
RewriteCond %{HTTP_HOST} ^localhost$
#then do nothing
RewriteRule ^ - [L]
#else enforce https
RewriteCond %{HTTPS} off
RewriteRule .* https://%{HTTP_HOST}%{REQUEST_URI} [NE,L,R=301]
Related
I have sample website name is example.com/customer/?loc=dashboard, this website is used PHP coding. I need to change last name ?loc=dashboard to dashboard. That means I want the result website name is mydomain.com/customer/dashboard
I have added a file called .htaccess in my root folder, and add something like this, but it cannot work:
ReWriteEngine On
RewriteRule ^/dashboard /?loc=dashboard [L]
I have refer this website https://mediatemple.net/community/products/dv/204643270/.htaccess-rewrite-rules to do. But cannot work. Hope someone can guide me on how to solve this problem. Thanks.
Updated - 1
Using #RavinderSingh13 method result:
Before ?loc=dashboard result:
Updated 12/11/2021 09:19:00
Rewrite rules
RewriteEngine On
RewriteCond %{HTTP_HOST} ^example\.com\.my [OR,NC]
RewriteCond %{SERVER_PORT} 80
RewriteRule ^(.*)$ https://www.example.com.my/$1 [R,L]
RewriteCond %{HTTP_HOST} ^example\.com\.my [OR,NC]
RewriteCond %{SERVER_PORT} 80
RewriteRule ^(.*)$ https://www.mydomain.com.my/$1 [R,L]
##External redirect to /customer/dashboard url rules here.
RewriteCond %{THE_REQUEST} \s/customer/?\?loc=(\S+)\s [NC]
RewriteRule ^ /customer/%1? [R=301,L]
##Internal rewrite rules to get it served by index.php file.
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^customer/(.*)/?$ index.php?parameter=$1 [QSA,NC,L]
Update - 2
everyoneknows.come.my/.htaccess
everyoneknows.come.my/index.php
everyoneknows.come.my/customer
everyoneknows.come.my/customer/index.php
everyoneknows.come.my/customer/account/dashboard.php
So that, in my php code, I use ?loc= replace account, then become everyoneknows.come.my/customer/?loc=dashboard
Update -3
I am using this method to get ?loc:
if (isset($_GET['loc'])) {
if (file_exists('account/' . $_GET['loc'] . '.php')) {
// if ($module_user_permission['view'] == 1) {
include_once 'account/' . $_GET['loc'] . '.php';
// } else {
// if ($_GET['loc'] != 'home') {
// include_once 'app/access_denied.php';
// } else {
// include_once 'account/dashboard.php';
// }
// }
} else {
include_once 'account/dashboard.php';
}
} else {
include_once 'account/dashboard' . (isset($_GET['system']) ? "_" . $_GET['system'] : "") . '.php';
}
With your shown attempts, samples; please try following htaccess rules file. Please make sure to clear your browser cache before testing your URLs.
Make sure your htaccess rules file is present alongside with customer folder(not inside it). In 2nd rewrite rule I have added parameter to get it pass to index.php file you can set it as per your requirement.
RewriteEngine ON
##Applying www by external redirect rules here...
RewriteCond %{HTTP_HOST} ^example\.com\.my [NC]
RewriteCond HTTPS off
RewriteRule ^(.*)/?$ https://www.example.com.my/$1 [R,L]
##External redirect to /customer/dashboard url rules here.
RewriteCond %{THE_REQUEST} \s/customer/?\?loc=(\S+)\s [NC]
RewriteRule ^ /customer/%1? [R=301,L]
##Internal rewrite rules to get it served by dashboard.php file.
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^customer/(.*)/?$ customer/account/dashboard.php?parameter=$1 [QSA,NC,L]
So, am helping a guy to redirect all HTTP requests to HTTPS but keep on getting loop error when adding any of these in .htaccess.
ReWriteCond %{SERVER_PORT} 80
Or
RewriteCond %{HTTP:X-Forwarded-Proto} !https
Or
RewriteCond %{HTTPS} off
Or
RewriteCond %{HTTPS} !on
Or
RewriteCond %{HTTP:X-Forwarded-SSL} =off
This is a shared hosting (if that even matters). This is the phpinfo file and am not too sure how I can use this via .htaccess like what this guy suggested
_SERVER["https_proxy"]
http://www.supawhip.com.au/phpinfo.php
In .htaccess try this:
<IfModule mod_rewrite.c>
RewriteEngine On
# This will enable the Rewrite capabilities
RewriteCond %{HTTPS} !=on
# This checks to make sure the connection is not already HTTPS
RewriteRule ^/?(.*) https://%{SERVER_NAME}/$1 [R=301,L]
# This rule will redirect users from their original location, to the same location but using HTTPS.
# i.e. http://www.example.com/foo/ to https://www.example.com/foo/
# The leading slash is made optional so that this will work either in httpd.conf
# or .htaccess context
</IfModule>
also in /config.php
// HTTP
define('HTTP_SERVER', 'https://www.supawhip.com.au/');
// HTTPS
define('HTTPS_SERVER', 'https://www.supawhip.com.au/');
and in /admin/config.php
// HTTP
define('HTTP_SERVER', 'https://www.supawhip.com.au/admin/');
define('HTTP_CATALOG', 'https://www.supawhip.com.au/');
// HTTPS
define('HTTPS_SERVER', 'https://www.supawhip.com.au/admin/');
define('HTTPS_CATALOG', 'https://www.supawhip.com.au/');
I'm trying to create an .htaccess file that does the following:
1 Allows the EC2 health check URL to be hit without redirecting to https
2 Redirects all non-https traffic to https
3 Redirect calls to / to /auth/app/public/app (using https)
Items 2 and 3 work fine, but quickly the healthcheck fails and the server no longer responds. Here's the content of my .htaccess file:
RewriteEngine On
#one attempt that didn't work
#RewriteCond %{HTTP_HOST} !^my\.domain\.com$ [NC]
#RewriteCond %{REQUEST_URI} !^/healthcheck.html$
#RewriteRule ^ http://my.domain.com/$1 [R=301,L]
#another attempt that didn't work
#RewriteCond %{HTTP_HOST} !$ [NC]
#RewriteCond %{REQUEST_URI} !^/healthcheck.html$
#RewriteRule ^(.*)$ /$1 [L,QSA]
#this works
RewriteRule ^$ https://my.domain.com/auth/app/public/app/ [L]
#this works
RewriteCond %{HTTP:X-Forwarded-Proto} !https
RewriteRule ^ https://my.domain.com%{REQUEST_URI} [L]
The first two commented-out attempts are from examples I found at https://serverfault.com/questions/470015/how-should-i-configure-my-elb-health-check-when-using-namevirtualhosts-and-redir
and https://serverfault.com/questions/470015/how-should-i-configure-my-elb-health-check-when-using-namevirtualhosts-and-redir/597541#597541
Please let me know if you have any suggestions how I can get this working.
Thank you for the reply. Before I read that, I found another post that provided a solution that is working for me. I simply added another condition to not apply the https redirect rule if the url was that of my health check page. Here is the working version of my .htaccess file:
RewriteEngine On
RewriteRule ^$ https://my.domain.com/auth/app/public/app/ [L]
RewriteCond %{HTTP:X-Forwarded-Proto} !https
RewriteCond %{REQUEST_URI} !^/healthcheck\.html$
RewriteRule ^ https://my.domain.com%{REQUEST_URI} [L]
I am trying to force https via htaccess and am getting a "too many redirects" error. Below is what I am using to try to accomplish this.
RewriteEngine On
RewriteCond %{HTTPS} !=on
RewriteRule ^.*$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L]
Any ideas on how I can accomplish this or why this doesn't seem to be working?
Edit: I followed the answers from here Force https://www. for Codeigniter in htaccess with mod_rewrite which seems to be causing the redirect loop for me. When I remove this part:
RewriteCond %{HTTPS} !=on
RewriteRule ^.*$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
The redirect loop goes away; however it isnt redirecting to https.
I can not say for sure if this is related to a (or some) hosting provider/s, but none of the solutions worked for me. Coz of restriction of hosting provider, I could not get the apache version except it was 2.x.
This is what did, maybe helpful to someone:
RewriteEngine On
RewriteCond %{ENV:HTTPS} !on
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
As mentioned by RiggsFolly, there is an issue with the statement, but if that doesnt work, you could try force it in the head tag:
$use_sts = true;
// iis sets HTTPS to 'off' for non-SSL requests
if ($use_sts && isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] != 'off') {
header('Strict-Transport-Security: max-age=31536000');
} elseif ($use_sts) {
header('Location: https://'.$_SERVER['HTTP_HOST'].$_SERVER['REQUEST_URI'], true, 301);
// we are in cleartext at the moment, prevent further execution and output
die();
}``
Is it possible to do this on .htaccess
if(filename.php = "order.php"){
redirect to https
}else{
redirect to http
}
I need it to do something like this, user might want to put https on my index.php and its unsecure and gives an error, that why I need to force to http if its not order.php, and force https if its order.php
I have 3 similar folders like in 1 server,
I tried to use this
RewriteEngine On
RewriteRule /(order.php) https://%{SERVER_NAME}%{REQUEST_URI} [L]
but failed, I did it on PHP, but I will have to put all of it hundred files.
So Iguess .htaccess will be more time efficient,
Yes it's possible
RewriteEngine on
RewriteCond %{HTTPS} off
RewriteCond %{REQUEST_URI} ^/order.php
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
RewriteCond %{HTTPS} on
RewriteCond %{REQUEST_URI} !^/order.php
RewriteRule (.*) http://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]