How To Redirect to https in Apache Using mod_rewrite

1 2 3 |
RewriteEngine On RewriteCond %{HTTPS} off RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} |
![]() |
1 2 3 |
RewriteEngine On RewriteCond %{HTTPS} off RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} |
Below information copied without permission from Josh Sharp’s site because it is too valuable to lose – great work Josh, and THANKS!
1 2 3 4 |
RewriteEngine On RewriteCond %{REQUEST_FILENAME} !-d RewriteCond %{REQUEST_FILENAME} !-f RewriteRule ^(.*)$ index.php?url=$1 [QSA,L] |
Note: Only rewrite if the requested file is not an existing directory (-d) or a file (-f)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
include('app/classes.php'); include('app/app.php'); //init our app $App = new App; //grab the mod_rewrite url $url = $_REQUEST['url']; //if the url is empty then the user has navigated just to root if ($url == ''){ //start with default controller $App->setController('home'); } else { //grab each part of our URL - controller, action, and identifier $path = explode('/',$url,3); //pass these onto the app to handle $App->setController($path[0],$path[1],$path[2]); } |
1 2 3 |
RewriteEngine On RewriteCond %{HTTPS} off RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} |
1 2 3 4 |
RewriteEngine On RewriteCond %{HTTP_HOST} !^www.YourDomain.com RewriteCond %{HTTP_HOST} !^$ RewriteRule ^.*$ http://www.YourDomain.com%{REQUEST_URI} [R=301,L] |