HTTPS and Remove index.php on CodeIgniter - php

I am using codeigniter framework,set .htaccess file to remove index.php and trying to enable HTTPS protocol for the server, and something happened.
HTTP:
Everything is okay, when I access http://www.example.com/controller/method or http://www.example.com/index.php/controller/method
HTTPS:
Everything is okay, when I access https://www.example.com/index.php/controller/method
Got 404 not found when I access https://www.example.com/controller/method
I think that is .htaccess file problem, it is look like htaccess file not working for HTTPS protocol.
.htaccess file of my site.
DirectoryIndex index.php
RewriteEngine on
RewriteCond %{HTTPS} off
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
RewriteCond $1 !^(index\.php|(.*)\.swf|forums|images|css|downloads|js|robots\.txt|favicon\.ico)
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ ./index.php?$1 [L,QSA]
Is something wrong? thanks a lot.

Note: The SSL rewrite should happen before the index.php rewrite in your .htaccess file.
So, Don't do this setting (as we don't change anything in server apache config file.
)
AllowOverride All
just apply the Setting 2 it works.
I have faced the same issue and resolved it by adding
AllowOverride All
Setting 1:
FYI we have 2 config files.
Default Apache config (which runs with http)
SSL config (which runs with https)
Go to SSL config and add
<Directory "/var/www/html">
AllowOverride All
</Directory>
**Setting 2:**The .htaccess file should be like this...
RewriteEngine On
RewriteCond %{HTTPS} !=on
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^ index.php [L]
</IfModule>
It worked for me.

First you remove $config['base_url] from your config file and put ''(blank) in your $config['index'] attribute ..
please set your .htaccess file like..
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
# Rewrite all other URLs to index.php/URL
RewriteRule ^(.*)$ index.php?url=$1 [PT,L]
</IfModule>
<IfModule !mod_rewrite.c>
ErrorDocument 404 index.php
</IfModule>

Try this
I used this in one of my project. May its helps you
#AuthType Basic
#AuthName "restricted area"
#AuthUserFile /home/site_name/public_html/.htpasswd
#require valid-user
RewriteEngine On
#RewriteBase /
RewriteCond %{SERVER_PORT} 80
RewriteRule ^(.*)$ https://www.site_name.com/$1 [R,L]
RewriteCond %{HTTP_HOST} ^([a-z.]+)?.site_name.lk$ [NC]
RewriteRule ^(.*)$ https://www.site_name.com/$1 [R=301,L]
RewriteCond %{HTTP_HOST} ^([a-z.]+)?site_name\.com$ [NC]
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule .? https://www.%1site_name.com%{REQUEST_URI} [R=301,L]
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
# RewriteCond $1 !^(index\.php|assets|files|robots\.txt)
# RewriteRule ^(.*)$ /index.php/$1 [L]
RewriteRule ^(.*)$ index.php/$1 [L,QSA]
</IfModule>
Note site_name in the above htaccess should replaced to your website name

you can try this, it works for my project.
RewriteEngine On
RewriteCond %{HTTPS} !=on
RewriteRule ^/?(.*) https://%{SERVER_NAME}/$1 [R,L]
RewriteCond $1 !^(index\.php|(.*)\.swf|forums|images|css|downloads|js|robots\.txt|favicon\.ico)
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L,QSA]
reference: https://wiki.apache.org/httpd/RewriteHTTPToHTTPS

if you want to http->https AND remove www AND remove index.php here's the solution:
RewriteEngine On
RewriteCond %{HTTPS} off [OR]
RewriteCond %{HTTP_HOST} ^www\. [NC]
RewriteCond %{HTTP_HOST} ^(?:www\.)?(.+)$ [NC]
RewriteRule ^ https://%1%{REQUEST_URI} [L,NE,R=301]
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^ index.php [L]
</IfModule>

Related

.htaccess code to make http go to https with a CodeIgniter complication

I use codeigniter with some URL routing.
I want to migrate from http to https.
This is my current .htaccess code:
RewriteEngine On
RewriteCond %{HTTP_HOST} !^eternalcitytours\.com$ [NC,OR]
RewriteCond %{HTTP_HOST} ^XXX\.XXX\.XXX\.XXX [NC]
RewriteRule ^(.*)$ http://eternalcitytours.com/$1 [R=301,L]
<IfModule mod_rewrite.c>
# Development
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond $1 !^(index\.php|images|scripts|styles|vendor|robots\.txt)
RewriteRule ^(.*)$ index.php/$1 [L]
</IfModule>
DirectoryIndex index.php
RewriteEngine on
RewriteCond $1 !^(index\.php|images|css|js|robots\.txt|favicon\.ico)
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ ./index.php/$1 [L,QSA]
The above works fine on http.
To go to https i changed the .htaccess to be:
RewriteEngine On
RewriteCond %{HTTPS} !=on
RewriteRule .* https://eternalcitytours.com/%{REQUEST_URI} [R,L]
RewriteEngine On
RewriteCond %{HTTP_HOST} !^eternalcitytours\.com$ [NC,OR]
RewriteCond %{HTTP_HOST} ^037\.061\.237\.216 [NC]
RewriteRule ^(.*)$ https://eternalcitytours.com/$1 [R=301,L]
<IfModule mod_rewrite.c>
# Development
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond $1 !^(index\.php|images|scripts|styles|vendor|robots\.txt)
RewriteRule ^(.*)$ index.php/$1 [L]
</IfModule>
DirectoryIndex index.php
RewriteEngine on
RewriteCond $1 !^(index\.php|images|css|js|robots\.txt|favicon\.ico)
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ ./index.php/$1 [L,QSA]
However, the above code doesnt work as I get 2 problems:
whenever incoming traffic comes to me from an http link, the resultant url has an extra / in it: e.g. https://eternalcitytours.com//en/5/About-Eternal-City-Tours instead of https://eternalcitytours.com/en/5/About-Eternal-City-Tours
If someone has https://WWW.eternalcitytours.com then the page wont load and an error is given. (ERR_SSL_PROTOCOL_ERROR)
Can someone help with my .htaccess code as i honestly dont know what i'm doing.
thanks
You need to use this rule for http -> https:
RewriteCond %{HTTPS} !=on
RewriteRule ^ https://eternalcitytours.com%{REQUEST_URI} [R=301,NE,L]
Note / removed before %{REQUEST_URI}.
Make sure to clear browser cache before testing it.
For problem #2 it seems your SSL cert is for eternalcitytours.com domain only. It is because SSL handshake hapens before mod_rewrite rules are executed and there is no rule based fix except that you keep https links with www in your HTML source.

Forcing HTTPS with www and remove index.php .htaccess in CodeIgniter

How can I achieve all of this using htaccess. Thus far I have--
RewriteEngine On
To remove index.php
RewriteCond %{REQUEST_URI} ^system.*
RewriteCond $1 !^(index\.php|images|js|uploads|css|robots\.txt)
RewriteRule ^(.*)$ /index.php/$1 [L]
#Checks to see if the user is attempting to access a valid file,
#such as an image or css document, if this isn't true it sends the
#request to index.php
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
To enforce SSL and non www to www
RewriteRule ^(.*)$ index.php/$1 [L]
RewriteCond %{HTTP_HOST} !^(www|abc|cde|efe) [NC] #Subdomain abc and cde
RewriteRule ^(.*)$ https://www.example.com/$1 [R=301,L]
know why its happening but can't figure out a rule to combine everything I need and make it work.
need force to https and remove index.php and non-www to www.
Answers will appreciated and thanks in advance
update your code with below and have a read the comments too
Options +FollowSymLinks
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
#Checks to see if the user is attempting to access a valid file,
#such as an image or css document, if this isn't true it sends the
#request to index.php
#First rewrite any request to the wrong domain to use the correct one (here www.)
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule ^(.*)$ https://www.%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
#Now, rewrite to HTTPS:
RewriteCond %{HTTPS} off
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /index.php/$1 [L]
#Removes access to the system folder by users.
#Additionally this will allow you to create a System.php controller,
#previously this would not have been possible.
#'system' can be replaced if you have renamed your system folder.
RewriteCond %{REQUEST_URI} ^system.*
RewriteRule ^(.*)$ /index.php?/$1 [L]
#Rename 'application' to your applications folder name.
RewriteCond %{REQUEST_URI} ^application.*
RewriteRule ^(.*)$ /index.php?/$1 [L]
</IfModule>
Have your rules in this order with a fix for http->https + www rule. Finally cut down redundant rules:
DirectoryIndex index.php
RewriteEngine On
# To enforce SSL and non www to www
RewriteCond %{HTTPS} off [OR]
RewriteCond %{HTTP_HOST} !^(www|abc|cde|efe)\. [NC]
RewriteRule ^ https://www.example.com/$1 [R=301,L,NE]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L]
I am not deep into codeigniter right now but please check the below rule,
RewriteEngine on
RewriteCond %{HTTPS} off
RewriteCond %{HTTP_HOST} !^www$
RewriteRule ^(.*)$ https://www.%{HTTP_HOST}/$1 [R=301]
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.*)$ index.php/$1 [L]
please tell if missed something.
Please use this code in you .htaccess
RewriteEngine on
RewriteCond %{HTTP_HOST} ^domain.([a-z]*)$
RewriteRule ^(.*)$ http://www.domain.%1/$1 [L,R=301]
RewriteCond $1 !^(index\.php|assets|robots\.txt)
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?/$1 [L,QSA]
code htaccess :
<IfModule mod_rewrite.c>
# Make sure directory listing is disabled
Options +FollowSymLinks -Indexes
RewriteEngine on
# NOTICE: If you get a 404 play with combinations of the following commented out lines
#AllowOverride All
#RewriteBase /wherever/codeginiter/is
# Restrict your site to only one domain
# !important USE ONLY ONE OPTION
# Option 1: To rewrite "www.domain.com -> domain.com" uncomment the following lines.
RewriteCond %{HTTPS} !=on
RewriteCond %{HTTP_HOST} ^www\.(.+)$ [NC]
RewriteRule ^(.*)$ http://%1/$1 [R=301,L]
# Option 2: To rewrite "domain.com -> www.domain.com" uncomment the following lines.
RewriteCond %{HTTPS} !=on
RewriteCond %{HTTP_HOST} !^www\..+$ [NC]
RewriteCond %{HTTP_HOST} (.+)$ [NC]
RewriteRule ^(.*)$ http://www.%1/$1 [R=301,L]
# Remove index.php from URL
RewriteCond %{HTTP:X-Requested-With} !^XMLHttpRequest$
RewriteCond %{THE_REQUEST} ^[^/]*/index\.php [NC]
RewriteRule ^index\.php(.*)$ $1 [R=301,NS,L]
# Keep people out of codeigniter directory and Git/Mercurial data
# RedirectMatch 403 ^/(system|\.git|\.hg).*$
# Send request via index.php (again, not if its a real file or folder)
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
<IfModule mod_php5.c>
RewriteRule ^(.*)$ index.php/$1 [L]
</IfModule>
<IfModule !mod_php5.c>
RewriteRule ^(.*)$ index.php?/$1 [L]
</IfModule>
on Application/config/config.php delete index.php on
$config['index_page'] = 'index.php';

Mod-rewrite for GET parameter on subdomains

I'm trying to rewrite a subdomain (NOT REDIRECT) to a $_GET parameter as such:
Desired result:
http://go.example.bz/link/abcde -> http://example.bz/go/link?id=abcde
or
http://go.example.bz/hrm/employee/8 -> http://example.bz/go/hrm/employee?id=8
What's currently working:
http://example.bz/go/link/abcde -> http://example.bz/go/link?id=abcde
and
http://example.bz/go/hrm/employee/8 -> http://example.bz/go/hrm/employee?id=8
with this .htaccess in the root:
RewriteEngine On
RewriteRule ^go/link.php/([^/\.]+)/?$ go/link.php?id=$1 [L]
RewriteRule ^go/hrm/employee.php/([^/\.]+)/?$ go/hrm/employee.php?parameter=$1 [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.*)$ $1.php [NC,L]
AddCharset UTF-8 .php
Options -Indexes
and this is how I redirect the subdomain:
<VirtualHost *:80>
Servername go.example.bz DocumentRoot /var/www/go
</VirtualHost>
I do not want to redirect to the -> destination, rather to keep the http://go.example.bz/link/abcde url but have the results of the /link?abcde
I figured that my problem came from not remove .php extension from subdomain URLs of which I found an answer here https://css-tricks.com/forums/topic/remove-php-extension-from-subdomain-urls/ and then I alternated my RewriteRules as such:
RewriteEngine On
RewriteRule ^go/link/([^/\.]+)/?$ go/link.php?id=$1 [L]
RewriteRule ^go/hrm/employee/([^/\.]+)/?$ go/hrm/employee.php?id=$1 [L]
RewriteEngine on
RewriteCond %{HTTP_HOST} ^go\.example\.bz$ [OR]
RewriteCond %{HTTP_HOST} ^www\.go\.example\.bz$
RewriteRule ^/?$ "http\:\/\/tgc\.bz\/go" [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^([^\.]+)/$ $1.php
AddCharset UTF-8 .php
Options -Indexes
You can use this rule in root .htaccess:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^(?!www\.)([^.]+)\.(example\.com)$ [NC]
RewriteRule ^(link)/(.+)$ http://%2/%1/$1?id=$2 [NC,L,QSA,R=302]
RewriteEngine On
RewriteCond %{HTTP_HOST} ^go.example.com
RewriteRule ^(.*?)/(.*)$ http://example.com/go/$1?id=$2 [L]
Does this help:
RewriteCond %{HTTP_HOST} ^go.example.com$
RewriteRule ^link/([a-zA-Z0-9]+)$ http://example.com/go/link?id=$1 [P]

Apache htaccess mod rewrite

So, I am having some issues with my htacces file trying to do some re-write conditions.
What I am trying to do is:
There are multiple domains pointed to this web folder.
If the domain is anything but example.com I want it to rewrite to example.com
UNLESS - the path is /path
Everything is working now except when you go to www.otherexample.com/path
It is hitting the last rule and changing the url to http://dev.otherexample.com/index.php?qs-rewrite=path and causes a redirect loop.
Here's the entire htaccess file
DirectoryIndex index.php
<IfModule mod_rewrite.c>
RewriteEngine On
#RewriteBase /
RewriteCond %{HTTP_HOST} !example\.com$ [NC]
RewriteCond %{REQUEST_URI} !^/path [NC]
RewriteRule ^.*$ http://dev.example.com%{REQUEST_URI} [L,R=301]
RewriteCond %{REQUEST_URI} ^(.*)//(.*)$
RewriteRule . %1/%2 [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^.*$ index.php?qs-rewrite=$0&%{QUERY_STRING} [L]
</IfModule>
This should be your complete .htaccess:
DirectoryIndex index.php
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{HTTP_HOST} !example\.com$ [NC]
RewriteCond %{REQUEST_URI} !^/path [NC]
RewriteCond %{QUERY_STRING} !qs-rewrite=[^&]+ [NC]
RewriteRule ^ http://dev.example.com%{REQUEST_URI} [L,R=301,NE]
RewriteCond %{REQUEST_URI} ^(.*)//(.*)$
RewriteRule . /%1/%2 [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^ /index.php?qs-rewrite=%{REQUEST_URI} [L,QSA]
</IfModule>

VHost & htaccess - 500 Internal Server error

I use ISPConfig 3 to manage my domains/subdomains.
I added subdomain parim.kristian.ee which would redirect to web/parim/ (note: web/ is my document root).
ISPConfig generated such redirect to apache config:
RewriteEngine on
RewriteCond %{HTTP_HOST} ^parim.kristian.ee [NC]
RewriteRule ^/(.*)$ /parim/$1 [L]
Now if i'll try to get static resources, such as http://parim.kristian.ee/images/1x1.gif, it serves fine, but when redirect is to codeigniter, it doesn't work.
Htaccess in web/parim/ looks like this:
<IfModule mod_rewrite.c>
RewriteEngine on
Options -Indexes
#Handle CodeIgniter request
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ ./index.php?/$1 [L]
</IfModule>
<IfModule !mod_rewrite.c>
ErrorDocument 404 ./index.php
</IfModule>
NOTE: accessing the same folder via http://kristian.ee/parim/ works!
I s*ck at htaccess, so any help is appreciated.
# To remove index.php from URL
RewriteEngine On
RewriteBase /parim
RewriteCond %{REQUEST_URI} ^system.*
RewriteRule ^(.*)$ /index.php/$1 [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L]
Or Simple remove RewriteBase /parim

Categories