Codeigniter remove index.php on https - .htaccess

I am using godaddy as my server provider and I bought an SSL certificate. It gave me some headache with index.php in my CodeIgniter project. I can not remove the index.php from my url.
I researched around and tried everything I found but nothing works for me, so do not consider it as a double please.
If: https://example.com/index.php/home - works perfectly fine
If: https://example.com/home
I get a 404 Error. Not Found The requested document was not found on this server.
So far, I have stoped at these conditions in my htaccess file:
RewriteEngine on
RewriteCond %{HTTPS} off
RewriteCond %{REQUEST_URI} (sale|success|cancel) [NC]
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
RewriteCond %{HTTPS} on
RewriteCond %{REQUEST_URI} !(static|sale|success|cancel) [NC]
RewriteRule ^(.*)$ http://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
RewriteCond $1 !^(index\.php|resources|robots\.txt|static) [NC]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L,QSA]
Any advice would be helpful. Thank you!

remove index.php from $config['base_url'].
Then Use this link to remove index.php from url
http://addictors.in/remove-index-php-from-url-in-codeigniter/
then restart server

My Final Result:
in Helpers:
//Force SSL injection
if(!function_exists('force_ssl'))
{
function force_ssl() {
if (!isset($_SERVER['HTTPS']) || $_SERVER['HTTPS'] != "on") {
$url = "https://". $_SERVER['SERVER_NAME'] . $_SERVER['REQUEST_URI'];
redirect($url);
exit;
}
}
}
In Controllers call the method: force_ssl();
In .htaccess, where the index.php is:
DirectoryIndex index.php
RewriteEngine on
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond $1 !^(index\.php|robots\.txt)
RewriteRule ^(.*)$ index.php?/$1 [L]

Related

how to mask part of URL after doing a mod_rewrite

I have several client domains I would like to point to my droplet(I have a vue app here) and serve the right path depending on the domain being called.
eg:
domainA.com should go to mysite.com/path-a
domainB.com should go to mysite.com/path-b
I have tried to rewrite the URL using .htaccess
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{HTTP_HOST} =domainA
RewriteCond %{REQUEST_URI} =/
RewriteRule ^ %{HOST_NAME}/uri-a [R=301,L]
# Handle subsequent routes
RewriteRule ^index\.html$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.html [L]
</IfModule>
However, I can't seem to be able to remove the URI and just show the client domain name.
Would also like to make it dynamic so I don't have to write the same rule for each client domain
Could you please try following, based on your shown samples, please clear your browser cache before testing your URLs.
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{HTTP_HOST} ^domainA\.com$ [NC]
RewriteRule ^/?$ http://%{HTTP_HOST}/path-a [R=301,L]
RewriteCond %{HTTP_HOST} ^domainB\.com$ [NC]
RewriteRule ^/?$ http://%{HTTP_HOST}/path-b [R=301,L]
# Handle subsequent routes
RewriteRule ^index\.html$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.html [L]
</IfModule>
You may try this code in your root .htaccess:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^(?:www\.)?domainA\.com$ [NC]
RewriteRule ^$ path-a [L]
RewriteCond %{HTTP_HOST} ^(?:www\.)?domainB\.com$ [NC]
RewriteRule ^$ path-b [L]
# Handle subsequent routes
RewriteRule ^index\.html$ - [L,NC]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^ index.html [L]
Make sure to test from a new browser or after clearing your browser cache.

.htaccess RewriteCond for a specific url path without https

there is a problem with the htaccess rewrite on a specified url (service/scripts/) path. I dont like to redirect if the path contains "service/scripts and just open the link with http". All other links should redirect to https. I m using the Codeigniter Framework. And i have tried the following code TRY1 and TRY2:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
#RewriteRule ^(.+)\.(\d+)\.(php|js|css|png|jpg|gif|gzip)$ $1.$3 [L]
RewriteCond $1 !^(index\.php|(.*)\.swf|assets|service/*|robots\.txt)
RewriteRule ^(.*)$ index.php?/$1 [L]
Header add Access-Control-Allow-Origin "https://url.info/"
<IfModule mod_rewrite.c>
# TURN SSL ON
RewriteCond %{HTTPS} off
RewriteRule ^(.*)$ https://www.url.info/$1 [R,L]
# TRY 1
#RewriteCond %{HTTPS} on
#RewriteCond %{REQUEST_URI} ^/service.*
#RewriteRule ^(.*)$ http://www.url.info/$1 [R=301,L]
#RewriteCond %{HTTPS} off
#RewriteCond %{REQUEST_URI} !^/service.*
#RewriteRule ^(.*)$ https://www.url.info/$1 [R=301,L]
# TRY 2
#RewriteCond %{HTTPS} on
#RewriteCond %{REQUEST_URI} ^/service/scripts/?.*$
#RewriteRule ^(.*)$ http://www.url.info/$1 [R=301,L]
#RewriteCond %{HTTPS} off
#RewriteCond %{REQUEST_URI} !^/service/scripts/?.*$
#RewriteRule ^(.*)$ https://www.url.info/$1 [R=301,L]
# Removes index.php RewriteCond $1 !\.(gif|jpe?g|png)$ [NC]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /index.php/$1 [L]
# If 404s, "No Input File" or every URL returns the same thing
# make it /index.php?/$1 above (add the question mark)
</IfModule>
Its always redirecting like the and the RewriteCond %{REQUEST_URI} !^/service.* is not triggert or the secound RewriteCond is overriding it...
Please can somebody help me?
Best regards,
Dave
Now I used a PHP Redirect in the HTML Head on all pages to solve the Problem.
if (!isset($_SERVER['HTTPS']) || $_SERVER['HTTPS'] !== 'on') {
if(!headers_sent()) {
header("Status: 301 Moved Permanently");
header(sprintf(
'Location: https://%s%s',
$_SERVER['HTTP_HOST'],
$_SERVER['REQUEST_URI']
));
exit();
}
}

SSL issue - too many redirects

The website in question is: sophostechnologies.com
https://sophostechnologies.com or sophostechnologies.com works fine and loads in SSL, HOWEVER, if I type www. or https://www. in front of the url name, I get too many redirects loop error.
My .htaccess file looks like this:
RewriteEngine On
RewriteCond %{SERVER_PORT} 80
RewriteRule ^(.*)$ https://www.sophostechnologies.com/$1 [R,L]
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} !(.*)\.(.*)$
RewriteRule ^(.+[^/])$ http://%{HTTP_HOST}%{REQUEST_URI}/ [L,R=301]
# Google sitemap controller
RewriteRule ^(tmp/)?sitemap.xml$ tmp/sitemap.xml [L]
RewriteCond %{REQUEST_FILENAME} -f
RewriteRule ^(.*)(\.php([0-9]*)|\.json|\.xml|\.tpl|\.phtml|\.ini|\.inc|/)$ index.php?_p=$1 [NC,QSA,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ index.php?_p=$1 [QSA,L]
Any help is appreciated.

HTTPS and REST API with .htaccess

Firstly I implemented a rewrite rule for my REST API on an apache2 server with the following .htaccess file:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-s
RewriteRule ^(.*)$ index.php?rquest=$1 [QSA,NC,L]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^(.*)$ index.php [QSA,NC,L]
RewriteCond %{REQUEST_FILENAME} -s
RewriteRule ^(.*)$ index.php [QSA,NC,L]
</IfModule>
It worked fine. If I entered e.g. http://mypage.de/REST/SomeRule/12 my index.php in the folder REST/ received the information SomeRule/12.
Now I enabled SSL on my server and I wanted to force the API to use https so I added the following code to my .htaccess file:
RewriteCond %{SERVER_PORT} !^443$
RewriteRule (.*) https://%{HTTP_HOST}/$1 [L]
Now I receive a page not found error. Can you see the error in the .htaccess file, or must it be somewhere else? I am not that familiar with these files. To create my API I used this tutorial.
With the help of anubhava I found the solution.
When I updated my server to run with https my folder sites-enabled included a new file sites-enabled/default-ssl. In this file I had to set
AllowOverride All
like I already did for the on using http. I addition the correct solution for the .htaccess file was the following:
<IfModule mod_rewrite.c>
RewriteEngine On
#use https
RewriteCond %{SERVER_PORT} !^443$
RewriteRule (.*) https://%{HTTP_HOST}/%{REQUEST_URI} [L]
#used for api
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-s
RewriteRule ^(.*)$ index.php?rquest=$1 [QSA,NC,L]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^(.*)$ index.php [QSA,NC,L]
RewriteCond %{REQUEST_FILENAME} -s
RewriteRule ^(.*)$ index.php [QSA,NC,L]
</IfModule>
I was not able to use
RewriteRule (.*) https://%{HTTP_HOST}/$1 [L]
because the $1 then was somehow overwritten for the next Rule.

codeigniter htaccess rewrites the url to the BASEPATH../

i have a website that i made with codeigniter it works relativly fine on the local machine but when i uploaded it to the server it gives me a 500 error, and the error is gone when i remove the htaccess, and in the local machine the error appears in a different way some times i couldn't find what causing this but some urls get rewritten like this:
the original url => http://domain.com/controller/method
the rewritten url => http://domain.com/absolute/path/to/controller/method
here's my htaccess file:
RewriteEngine On
RewriteRule ^(welcome(/index)?|index(\.php)?)/?$ / [L,R=301]
RewriteRule ^(.*)/index/?$ $1 [L,R=301]
RewriteRule ^(.+)/$ $1 [L,R=301]
RewriteCond %{REQUEST_URI} ^system.*
RewriteRule ^(.*)$ /index.php/$1 [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L]
any help, thanx in advance.
try with my .htaccess code........
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L]

Resources