How to remove File Extension (index.php ) from URL using .htaccess - php

I'm unable to remove file extension from my URL
https://www.deemsolar.com/ar/index.php
This is the current URL of my website
I want to change this as https://www.deemsolar.com/ar/
Please Help me out?

Add this in your .htaccess file
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L,QSA]
Note: make sure that in config.php file,
$config['index_page'] = "";
$config['uri_protocol'] = "REQUEST_URI";

You can remove using:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L]
</IfModule>

Read the docs fully and clearly.
codeigniter urls docs

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ $1.php [NC,L]
# For Index
RewriteRule ^index.extension?$ /index.php [NC,L]
# For Another Page
RewriteRule ^login.extension?$ /login.php [NC,L]

Related

Codeigniter remove index.php from URL localhost:82/project

I have URL
http://localhost:82/project/index.php/home
I want to remove index.php from above url.
Like this : http://localhost:82/project/home
I have used many solution
1) I have remove index.php from config.php file :
$config['index_page'] = '';
2) change "uri_protocol" in config.php file :
$config['uri_protocol'] = 'REQUEST_URI';
My htaccess :
RewriteEngine On
RewriteBase /project/
RewriteCond %{REQUEST_URI} ^system.*
RewriteCond $1 !^(index\.php|images|js|uploads|css|robots\.txt)
RewriteRule ^(.*)$ /index.php/$1 [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L]
Anybody master here to solve my question ? Thanks in advance.
use this Codeigniter .htaccess file and install it to your root app.
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L]
</IfModule>
Like this one...
project_name
-application
-assets
-system
-user_guide
-.htaccess // like this one
Try To Use This
RewriteEngine on
#RewriteBase /project/
RewriteCond $1 !^(index\.php|images|admin|robots\.txt)
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond $1 !^(index\.php|images|admin|robots\.txt)
RewriteRule .* index.php/$0 [PT,L]
I use it and work on me.
Thank You
Set your base URL in config as
$config['base_url'] = 'http://localhost:82/project/'
In your .htaccess, Try with this, I have used this in my CI projects.
RewriteEngine on
RewriteCond $1 !^(index\.php|resources|robots\.txt)
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L,QSA]
Place this file in your project folder.
If you are using Apache Server, you must do mod_rewrite enabled. And then modify your .htaccess file with the code below.
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L]
For more information, visit here
Try similar question here

Is it possible that ci url will not work with index.php

Is it possible that ci url will not work with index.php.I dont want to add index.php in url. If someone adds index.php in url it should not work.I have tried many methods . Please give me suggestion.
I have tried but it's not working
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L]
for e.g
if i use below url This should work..............
`example.com/abc`
if i use below url shouldn't work
`example.com/index.php/abc`
Create .htaccess file in your root directory And keep this code into it
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L]
</IfModule>
Open application/config/config.php
//find the below code
$config['index_page'] = "index.php"
//replace with the below code
$config['index_page'] = ""
/find the below code
$config['uri_protocol'] = "AUTO"
//replace with the below code
$config['uri_protocol'] = "REQUEST_URI"
You can use:
RewriteEngine On
# 403 Forbidden for index.php
RewriteCond %{THE_REQUEST} index\.php [NC]
RewriteRule ^ - [F]
# Rewrite to index.php
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L]
In Config.php
$config['base_url'] = 'http://localhost/website/';
$config['index_page'] = '';
# If Live site
# $config['base_url'] = 'http://stackoverflow.com/';
In .htaccess
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]
Check my answer on another question Set up the base URL in codeigniter
Try the following code than..
change the following in config.php
$config['index_page'] = ""
$config['uri_protocol'] = "REQUEST_URI"
Now open .htaccess file and replace with below code..
RewriteEngine on
RewriteCond $1 !^(index\.php|resources|robots\.txt)
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L,QSA]
if above index line do not work the replace the line with new code. Because .htaccess depend on server
// Replace last .htaccess line with this line
RewriteRule ^(.*)$ index.php?/$1 [L,QSA]
Add below code in the top of the index.php. I think this will solve your problem.
if(strpos($_SERVER['REQUEST_URI'],'index.php') !== false){
header('location: http://xyx.com/404');
die();
}
Try with below rule,
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L,END]
RewriteRule ^index\.php* - [F]
What exactly do you mean by saying url shouldn't work should it redirect to another or be replaced?
url shouldn't work - i think you must say to a user (to a crawler) that there is an error in this link, if the link is already indexed, tell crawler that this is 404 not found page, and it will stop indexing that pages. You can't disable index.php/foo totally, because all requests are going through index.php (i believe, if you are using routing)
Here is replacement example - https://htaccess.madewithlove.be?share=04dcf45c-32e9-5c29-b706-5869cd369ffd
Input url http://ex.com/index.php/abc
RewriteEngine On
RewriteRule index.php/(.*) /404 [L] #send user to not found page
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L]
Output url http://ex.com/404
Just change your root .htaccess file:
RewriteEngine on
RewriteBase /projectname
# Hide the application and system directories by redirecting the request to index.php
RewriteRule ^(application|system|\.svn) index.php/$1 [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [QSA,L]
Change in config file:
$config['index_page'] = '';
$config['uri_protocol'] = 'REQUEST_URI';

CodeIgniter .htaccess remove index.php on Server

I know there's a lot of questions and answers here but I'm really confused and I couldn't fix my problem yet. Please help!
I can easily access my controller via this url:
http://www.example.com/new/index.php/welcome
but I can't get it via this URL:
http://www.example.com/new/welcome
I'm configuring my CodeIgniter site in a subdirectory "new" and on my server, my directory structure is:
/
htdocs
new
/ (this is empty), htdocs(here is my directory named as "new" and new(this is the exact folder where I've my codeIgniter files.
I'm confused here about "/" and "htdocs", I think this is the thing which I'm not able to handle because my domain example.com is pointing to htdocs i.e if I put index.php in htdocs, example.com will load index.php.
my .htaccess file in "new" directory is :
RewriteEngine On
#RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule (.*) index.php/$1
in config/config.php:
$config['index_page'] = '';
$config['uri_protocol'] = 'AUTO';
I've tried $config['uri_protocol'] for "Auto", "REQUEST_URI" and "QUERY_STRING" etc.
With this configuration I'm able to remove index.php in my local host but I can't fix it on server. Please help!! Thanks in advance.
this one worked for me
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]
make sure to remove index.php from appliation/config/config.php
$config['index_page'] = '';
put this code in .htaccess file inside new directory
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule .* index.php/$0 [PT,L]
And dont forget to remove whitespace from:
$config['index_page'] = ' ';
Try with this code in .htaccess :
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteBase /
RewriteCond $1 !^(index\.php|robots\.txt)
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L,QSA]
</IfModule>
Try either changing the
$config['uri_protocol'] = 'PATH_INFO';`
or changing the rewrite rule to use the query string instead:
RewriteEngine On
#RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule (.*) index.php?$1
Then setting the protocol to query string:
$config['uri_protocol'] = 'QUERY_STRING';`
If you say that it's working on localhost, then it might be a server problem. Check if the server mod_rewrite is on by writing phpinfo(); somewhere in your view code. If it's not on and you don't have permissions, contact your host admin.
This code works for me:
RewriteEngine On
RewriteCond %{REQUEST_URI} ^/system.*
RewriteRule ^(.*)$ index.php?/$1 [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.+)$ index.php?/$1 [L]
And the config.php
$config['index_page'] = '';
Try adding ? after index.php in htaccess,
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?/$1 [L]

Removing index.php of codeigniter on local

I'm trying to remove index.php,in my localhost, but it seems doesn't working,its on http://localhost/testing.
I put .htacces in 'testing' directory under the htdocs.
LoadModule rewrite_module modules/mod_rewrite.so
at Apache/conf also already unchecked.
Here my .htaccess:
RewriteEngine On
RewriteBase /testing/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /testing/index.php/$1 [L]
Here my config:
$config['base_url'] = "http://localhost/testing";
$config['index_page'] = '';
$config['uri_protocol'] = 'AUTO';
When I access login controller, it's not found.
Not Found
The requested URL /testing/login was not found on this server.
I really don't know what to try next. Any help would be appreciated.
Try this :-
Config.php :-
$config['base_url'] = 'http://localhost/testing/';
$config['index_page'] = '';
.htaccess
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L]
Your htaccess should be like
RewriteEngine On
RewriteBase /testing/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L]
There's no need to append /testing/ since RewriteBase already takes care of that.
basically the index.php is included in all URLs:
we can remove this using .htaccess with a simple rules. following is an example of such a file, using the "negative" method in which everything is redirected,
RewriteEngine on
RewriteBase /testing/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /index.php/$1 [L]
now restart your server and check
Leave out the trailing slash in RewriteBase:
RewriteEngine On
RewriteBase /testing
RewriteCond $1 !^(index\.php|robots\.txt)
RewriteRule ^(.*)$ index.php/$1 [L]
$config['base_url'] should be http://localhost/testing/
$config['index_page'] should be blank.
Try this one in the .htaccess file:
RewriteEngine on
RewriteCond $1 !^(index\.php|robots\.txt|resources)
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?$1 [L]
ErrorDocument 404 /mainpage
your config file is fine
Difference being the "?" after index.php
You may also want to try changing the value of $config[‘uri_protocol’] in ./application/config/config.php. Sometimes CodeIgniter gets it wrong. Changing it to PATH_INFOmight work.

Codeigniter without index.php changing in config and .htaccess not working

I have this issue with codeigniter for my subdomain where I would like to take out index.php but is not working for some reason.
I have set .htaccess with below:
RewriteEngine on
RewriteCond $1 !^(index\.php|images|captcha|css|js|user_guide|robots\.txt)
RewriteRule ^(.*)$ index.php/$1 [L]
and also config.php index_page settings have been changed like below:
$config['index_page'] = '';
do you know why it's not working?
And this is mine
DirectoryIndex index.php
RewriteEngine on
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]
This is how mine is currently setup, hope this helps.
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?/$1 [L]

Categories