0

Before I get slapped with a "Duplicate Question," please note that I am inquiring as to how I can add on some mod_rewrite rules to my current .htaccess with the following:

Options +FollowSymLinks

RewriteEngine on
RewriteCond $1 !^(index\.php|resources|robots\.txt)
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L] 

My issue is, I have files outside of the application directory that are not set up with a Controller. So, how can I flat-out remove all .php extensions with my current .htaccess, WHILST retaining the ability to remove the index.php (or, index) parameter from the URL?

EDIT: I have tried another RewriteCond and RewriteRule. CodeIgniter throws a 404, though, since it thinks it is a Controller.

Devin Rowan
  • 135
  • 8

2 Answers2

2

According to Remove .php extension with .htaccess you should add

RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule !.*\.php$ %{REQUEST_FILENAME}.php [QSA,L]

to your .htaccess BUT you should add it before the CodeIgniter rules like so:

Options +FollowSymLinks

RewriteEngine on

# Serve up non-CodeIgniter PHP files if they are requested without their extension
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule !.*\.php$ %{REQUEST_FILENAME}.php [QSA,L]

# CodeIgniter rewrite rules
RewriteCond $1 !^(index\.php|resources|robots\.txt)
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L]

Since mod_rewrite processes and applies rules in order, you want non-CodeIgniter files to be tried first and then fall into the CodeIgniter rules.


I have no affiliation with this site and I am sure there are others like it but if you ever wanted to just test rewrite rules with various URLs then check out https://htaccess.madewithlove.be/

MonkeyZeus
  • 20,375
  • 4
  • 36
  • 77
-1

for removing index.php from URL in CodeIgniter. you must follow these steps.

1 . add the following code in .htaccess and .htaccess must be in the root folder.

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

2 . change $config['index_page'] = 'index.php'; to $config['index_page'] = '';

you don't need to remove .php in CodeIgniter because that not exist. if you want to learn! so use this code to remove .php extension add this code to .htaccess

RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule !.*\.php$ %{REQUEST_FILENAME}.php [QSA,L]
AbdulAhmad Matin
  • 1,047
  • 1
  • 18
  • 27