Forcing https on codeigniter on fastcgi - .htaccess

I am trying to force ssl on certain urls using the following .htaccess on apache FastCGI. But the problem is when I try to browse http://[hostname]/basket it redirects to http://[hostname]/index.php?/basket instead of https://[hostname]/basket. Below is the code. I am stuck with this please help
RewriteEngine on
RewriteBase /
RewriteCond %{HTTPS} off
RewriteCond $1 (checkout|basket)
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
RewriteCond %{HTTPS} on
RewriteCond $1 !(checkout|basket)
RewriteRule ^(.*)$ http://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?/$1 [QSA,L]

This issue has been resolved: below is the code if anyone else having the same issue:
RewriteEngine on
RewriteBase /
RewriteCond %{HTTPS} off
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond $1 (checkout|basket)
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
RewriteCond %{HTTPS} on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond $1 !(checkout|basket)
RewriteRule ^(.*)$ http://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?/$1 [QSA,L]

Related

How to redirect a URL and then rewrite it to same url with htaccess but from the root?

This code below for rewrite/redirect from localhost/moneyworld/exchange?title=BTC_PMUSD to localhost/moneyworld/BTC_PMUSD but i want to rewrite/redirect from www.domain.com/exchange?title=BTC_PMUSD to www.domain.com/BTC_PMUSD Without also /moneyworld/
RewriteEngine ON
RewriteCond %{THE_REQUEST} \s/(moneyworld)/exchange\?title=([^\s]*)\s [NC]
RewriteRule ^ /%1/%2 [NE,QSD,R=301]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^/]+)/([^/]+)/?$ /moneyworld/exchange?title=$2 [L]
Thanks
The problem was that i was forwarding exchange.php to exchange after the url rewrite rules so simply the code below solved it
RewriteEngine ON
RewriteBase /
RewriteCond %{DOCUMENT_ROOT}/$1\.php -f [NC]
RewriteRule ^(.+?)/?$ $1.php [L]
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,L]
RewriteCond %{THE_REQUEST} \s/exchange\?title=([^\s]*)\s [NC]
RewriteRule ^ /%1 [NE,QSD,R=301]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^/]+)/?$ /exchange?title=$1 [L]

How force www except url

Use code force https with www
RewriteEngine On
RewriteCond %{HTTP_HOST} !^www\.example\.com [OR]
RewriteCond %{HTTP:X-Forwarded-SSL} !on
RewriteRule (.*) https://www.example.com/$1 [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?/$1 [L]
How do I make the url below work both with WWW and without WWW
https://example.com/license/get_license.php
https://example.com/license/verified_license.php
Resolution
RewriteEngine On
RewriteCond %{REQUEST_URI} !^/license/get_license\.php$
RewriteCond %{REQUEST_URI} !^/license/verified_license\.php$
RewriteCond %{HTTP_HOST} !^www\.example\.com [OR]
RewriteCond %{HTTP:X-Forwarded-SSL} !on
RewriteRule (.*) https://www.example.com/$1 [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?/$1 [L]
Credits: Hosting Webfaction

Redirect from sub.domain.co.id to https://sub.domain.co.id

Good day. I have a website called with sub.domain.co.id and now I want to redirect it to https://sub.domain.co.id.
I'm using codeigniter and this what i change in my .htaccess
Before .htaccess look like this
RewriteEngine on
RewriteCond $1 !^(index\.php|public|\.txt)
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?$1
then I change it to ref(redirecting https://domain.com to https://www.domain.com)
RewriteEngine on
RewriteCond %{HTTPS} off [OR]
RewriteCond %{HTTP_HOST} !^http://. [NC]
RewriteRule ^ https://sub.domain.co.id%{REQUEST_URI} [R=301,L,NE]
RewriteCond $1 !^(index\.php|public|\.txt)
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?$1
but no help. the URL still use http. How can i achieve that?
try this and see. (index.php will removed too)
RewriteEngine on
RewriteCond $1 !^(index\.php|assets|image|resources|robots\.txt)
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?/$1 [L,QSA]
RewriteCond %{HTTP_HOST} ^sub.domain.co.id [NC]
RewriteRule ^(.*)$ https://www.sub.domain.co.id/$1 [L,R=301]
didn't test. 95% Sure. Not the 100%

htaccess rewrite get url section not working

I am trying to rewrite the url example.com/parts/toolbucket/part.php?id=1 into example/toolbucket/part/1
I have most of the .htaccess working but the last part of the rewrite doesn't work.
This is my .htaccess
Options -Indexes
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} ^www.example.com$ [NC]
RewriteRule ^(.*)$ https://example.com/$1 [R=301,L]
RewriteCond %{THE_REQUEST} \s/parts/([^\s]+)\s [NC]
RewriteRule ^ %1 [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.+)$ /parts/$1 [L,QSA]
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s([^.]+)\.php [NC]
RewriteRule ^ %1 [R,L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.*?)/?$ $1.php [L]
RewriteRule ^toolbucket/([0-9]*)$ ./part.php?id=$1
Any ideas?
Because you are missing the directory part in the rule. If you want your URL to be this example.com/toolbucket/part/1
Replace this
RewriteRule ^toolbucket/([0-9]*)$ ./part.php?id=$1
With this.
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^toolbucket/part/([0-9]*)/?$ /parts/toolbucket/part.php?id=$1 [L]

How to make this with htaccess?

How to make this with htacess:
http://example.com/category => /index.php?action=category
http://example.com/category?query=string => /index.php?action=category&query=string
http://example.com/category/subcategory => /index.php?action=category/subcategory
http://subdomain.example.com => /index.php?action=subdomain
http://subdomain.example.com/category/subcategory => /index.php?action=subdomain/category/subcategory
This is My current code:
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC]
RewriteRule ^(.*)$ http://%1/$1 [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)?$ /index.php?action=$1 [L]
The below rewrite rules will rewrite:
http://subdomain.domain.com => /index.php?action=subdomain/
I suggest you to handle that in your index.php to avoid extra rewrite rules.
RewriteEngine on
RewriteBase /
RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC]
RewriteRule ^(.*)$ http://%1/$1 [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-l
RewriteCond %{HTTP_HOST} ^(subdomain)\. [NC]
RewriteRule ^(.*)$ /index.php?action=%1/$1 [L,QSA]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-l
RewriteCond %{HTTP_HOST} ^(domain)\. [NC]
RewriteRule ^(category)(^/.*)?$ /index.php?action=$1$2 [L,QSA]
Thanks for helping.
.htaccess code:
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC]
RewriteRule ^(.*)$ http://%1/$1 [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-l
RewriteRule ^(.*)?$ /index.php?action=$1 [L]
index.php code:
$domain = "domain.com";
if($_SERVER["HTTP_HOST"] != $domain)
{
$subdomain = str_replace(".$domain","",$_SERVER["HTTP_HOST"]);
$_GET['action'] = (isset($_GET['action'])) ? "$subdomain/".$_GET['action']:$subdomain;
}

Resources