Force HTTPS and remove index.php on CodeIgniter - .htaccess

So I'm using this .htaccess script on my VPS but it doesn't work on the point where it should remove the index.php from the url.
<IfModule mod_rewrite.c>
RewriteEngine On
#Force SSL
RewriteCond %{SERVER_PORT} 80
RewriteRule ^(.*)$ https://domain.com/$1 [R,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]
#When your application folder isn't in the system folder
#This snippet prevents user access to the application folder
#Submitted by: Fabdrol
#Rename 'application' to your applications folder name.
RewriteCond %{REQUEST_URI} ^application.*
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
RewriteRule ^(.*)$ index.php?/$1 [L]
</IfModule>
<IfModule !mod_rewrite.c>
# If we don't have mod_rewrite installed, all 404's
# can be sent to index.php, and everything works as normal.
# Submitted by: ElliotHaughin
ErrorDocument 404 /index.php
</IfModule>
Options -Indexes
As I said everything is fine with the redirect but the when I want to open domain.com/projects it's not found error but if I add domain.com/index.php/projects everything is fine...

In application/config/config.php change:
$config['index_page'] = 'index.php';
to
$config['index_page'] = '';

Check your VHost file, make sure it looks like
<VirtualHost *>
DocumentRoot "C:\xampp\htdocs\http"
ServerName localhost
</VirtualHost>
and not like
<VirtualHost *:80>
DocumentRoot "C:\xampp\htdocs\http"
ServerName localhost
</VirtualHost>

Related

Codeigniter site was not working in live server

Can someone help me,
My site was working fine in staging server and I moved the site live server where I am getting HTTP ERROR 500.
My htaccess file is
<IfModule mod_rewrite.c>
RewriteEngine On
#RewriteBase /
#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]
#When your application folder isn't in the system folder
#This snippet prevents user access to the application folder
#Submitted by: Fabdrol
#Rename 'application' to your applications folder name.
RewriteCond %{REQUEST_URI} ^application.*
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
RewriteRule ^(.*)$ index.php?/$1 [L]
</IfModule>
<IfModule !mod_rewrite.c>
# If we don't have mod_rewrite installed, all 404's
# can be sent to index.php, and everything works as normal.
# Submitted by: ElliotHaughin
ErrorDocument 404 /index.php
</IfModule>
The same htaccess file was there in both the servers. mod_rewrite was enabled and everything was fine from server side. Can someone help me what can be the issue
Not exactly an answer but too large for a comment:
switch (ENVIRONMENT) {
case 'development':
error_reporting(~E_DEPRECATED);
ini_set('display_errors', 1);
break;
case 'testing':
case 'production':
error_reporting(~E_DEPRECATED);
ini_set('display_errors', 1);
break;
default:
header('HTTP/1.1 503 Service Unavailable.', TRUE, 503);
echo 'The application environment is not set correctly.';
exit(1); // EXIT_ERROR
}
Change production to have the same error_reporting as development so you will get the errors instead of seeing a generic 500 page.
I don't believe you .htaccess file is at fault here as you wouuld get a 400 rather than a 500 (internal server error).
you can try this:
upload htaccess file in root
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
Options -Indexes
RewriteCond %{REQUEST_URI} ^system.*
RewriteRule ^(.*)$ /index.php?/$1 [L]
RewriteCond %{REQUEST_URI} ^application.*
RewriteRule ^(.*)$ /index.php?/$1 [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /index.php?/$1 [L]
</IfModule>
<IfModule !mod_rewrite.c>
ErrorDocument 404 /index.php
</IfModule>

mod_rewrite CodeIgniter

Now URL for my website is for example: http://localhost/xxx/index.php/welcome/
I want to change this to: http://localhost/xxx/welcome/
My .htaccess file:
<IfModule authz_core_module>
Require all denied
</IfModule>
<IfModule !authz_core_module>
Deny from all
</IfModule>
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L]
My config.php file:
$config['base_url'] = 'http://localhost/xxx/';
$config['index_page'] = '';
But my new URL still doesn't work. What should I add there?
You can try the following mod_rewrite. It actually works and I saw this on one of the tutorials I watched here and here is the .htaccess.
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /foldername/foldernameifapplicable/foldernameifapplicable
#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]
#When your application folder isn't in the system folder
#This snippet prevents user access to the application folder
#Submitted by: Fabdrol
#Rename 'application' to your applications folder name.
RewriteCond %{REQUEST_URI} ^application.*
RewriteRule ^(.*)$ /index.php?/$1 [L]
</IfModule>
<IfModule !mod_rewrite.c>
# If we don't have mod_rewrite installed, all 404's
# can be sent to index.php, and everything works as normal.
# Submitted by: ElliotHaughin
ErrorDocument 404 /index.php
</IfModule>
I do not recall all the changes made, but I followed the following tutorial and it did what you ask.
https://www.youtube.com/watch?v=I752ofYu7ag
htaccess looked like this
<IfModule authz_core_module>
Require all denied
</IfModule>
<IfModule !authz_core_module>
Deny from all
</IfModule>

Xampp 3.2.1 local copy of site 404ing on everypage apart from homepage

I have copied a site down from live (works fine) on to my local machine. Locally I can view my homepage of the site, however if I then try and visit another page I keep getting 404's.
here is my vhosts setup:
<VirtualHost *:80>
DocumentRoot "C:\Users\Sony\Documents\websites\website.com"
ServerName local.website.com
<Directory "C:\Users\Sony\Documents\websites\website.com">
Order allow,deny
Allow from all
Require all granted
</Directory>
</VirtualHost>
Anyone have an idea why this could be happening? Seems to only happen with the latest version of xampp and on urls that have been rewritten.
In case you are interested here is my htaccess file:
ErrorDocument 404 /404.php
RewriteEngine on
RewriteBase /
# remove www from host
RewriteCond %{HTTP_HOST} ^www\.(.+)
RewriteCond %{HTTPS}s/%1 ^(on(s)|offs)/(.+)
RewriteRule ^ http%2://%3%{REQUEST_URI} [L,R=301]
# remove trailing slash
RewriteRule ^(.*)\/(\?.*)?$ $1$2 [R=301,L]
# Don't match real existing files so CSS, scripts, images aren't rewritten
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
# Match the first two groups before / and send them to the query string
RewriteRule ^([+A-Za-z0-9-\.]+)?$ deals.php?country_code=$1 [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([+A-Za-z0-9-\.]+)/([+A-Za-z0-9-\.]+)?$ deals.php?country_code=$1&zone_id=$2 [L]
Any help would be appreciated

.htaccess control remove from a folder

I have a codeigniter project running on root folder. In the same server on a folder called ems2 another project is running. But because of the root .htaccess of codeigniter project, the ems2 project is not working. How can I remove .htaccess control from ems2 folder?
My .htaccess file on root has following code
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} !^www\.
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [R=301,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} ^codeigniter.*
RewriteRule ^(.*)$ /index.php?/$1 [L]
#When your application folder isn't in the system folder
#This snippet prevents user access to the application folder
#Submitted by: Fabdrol
#Rename 'application' to your applications folder name.
RewriteCond %{REQUEST_URI} ^uz\-mi.*
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
RewriteCond $1 !^(index\.php|images|css|style|uploads|js|robots\.txt)
RewriteRule ^(.*)$ index.php/$1 [L]
</IfModule>
<IfModule !mod_rewrite.c>
# If we don't have mod_rewrite installed, all 404's
# can be sent to index.php, and everything works as normal.
# Submitted by: ElliotHaughin
ErrorDocument 404 /index.php
</IfModule>
RewriteCond %{HTTP_HOST} ^encg\.ae$ [OR]
RewriteCond %{HTTP_HOST} ^www\.encg\.ae$
RewriteRule ^/?$ "http\:\/\/www\.abc\.com\/encg" [R=301,L]
RewriteCond %{HTTP_HOST} ^ncg\.ae$ [OR]
RewriteCond %{HTTP_HOST} ^www\.ncg\.ae$
RewriteRule ^/?$ "http\:\/\/www\.abc\.com\/encg" [R=301,L]
You can add a new, simple .htaccess file to the ems2 project folder with a "NOP" content to stop processing the .htaccess from the parent folder.
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
</IfModule>

htaccess and mod_rewrite codeigniter

I am using codeigniter and below is my htaccess file. However, i want a single directory to be accessable outside of the codeigniter app via my domain.com/directory. the other directory is a forum not related to codeigniter. how would i set up a rule in htaccess? i dont have access to http.conf
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
#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]
#When your application folder isn't in the system folder
#This snippet prevents user access to the application folder
#Submitted by: Fabdrol
#Rename 'application' to your applications folder name.
RewriteCond %{REQUEST_URI} ^application.*
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
RewriteRule ^(.*)$ index.php?/$1 [L]
</IfModule>
<IfModule !mod_rewrite.c>
# If we don't have mod_rewrite installed, all 404's
# can be sent to index.php, and everything works as normal.
# Submitted by: ElliotHaughin
ErrorDocument 404 /index.php
</IfModule>
This should already work. RewriteCond %{REQUEST_FILENAME} !-d tells mod_rewrite to ignore actual directories, and RewriteCond %{REQUEST_FILENAME} !-f tells it to ignore actual files.
I use this to exclude any folder from being parsed to codeigniter framework as a controller.
So www.domain.com/images/ would run as normal folder, etc. There you can put your individual directives of htaccess. Best of luck!
RewriteCond $1 !^(folders_to_exclude_from_CI|images|css|assets|resources|robots\.txt)

Resources