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.

5
  • 2
    Have you tried adding a second RewriteCond and RewriteRule after the CodeIgniter rules? stackoverflow.com/a/4026967/2191572. If you already tried this then update your question with your results and how it failed for you or else get slapped with "Duplicate Question". Commented Jun 25, 2018 at 16:27
  • Apologies - see updated question. Commented Jun 25, 2018 at 16:36
  • 1
    Have you tried putting the rule before the CodeIgniter rules? Commented Jun 25, 2018 at 16:37
  • 1
    The hell. That worked. Can you please put that into an answer so I can mark it as solved and upvote you? :) Commented Jun 25, 2018 at 16:39
  • 1
    Sure thing, done Commented Jun 25, 2018 at 16:45

2 Answers 2

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/

Sign up to request clarification or add additional context in comments.

1 Comment

I want to know what is my problem?
-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]

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.