CodeIgniter Forums

Full Version: index.php .htaccess trick won't work
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Pages: 1 2 3

El Forum

[eluser]timpiele[/eluser]
I am on a CentOS server and .htaccess files work everywhere else on my server but in CI.

Trying to remove the index.php of course using this snippet in my application/.htaccess file:

RewriteEngine on
RewriteCond $1 !^(index\.php|images|robots\.txt)
RewriteRule ^(.*)$ /index.php/$1 [L]

I am just using those three lines, that's it and I get
a Page Forbidden error...

El Forum

[eluser]Aken[/eluser]
Wrong .htaccess file. Leave the one in the application folder as-is, and create a new one in your root directory (wherever index.php is).

Also, that's a crap .htaccess (CI needs to update their user guide). Use something like this instead:
Code:
<IfModule mod_rewrite.c>
  RewriteEngine On
  RewriteBase /

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

<IfModule !mod_rewrite.c>
  ErrorDocument 404 index.php
</IfModule>

El Forum

[eluser]timpiele[/eluser]
I'm getting 404 which from reading that means mod_rewrite is not working but in phpinfo()
I show mod_rewrite under Loaded Modules and several sites on the server use mod_rewrite successfully... could something in routes be hosing it?

El Forum

[eluser]Aken[/eluser]
Is the 404 page generated by your server or CodeIgniter?

If the latter, then yes routes may be doing it.

El Forum

[eluser]InsiteFX[/eluser]
If your .htaccess file is working the line TEST will generate a Server 500 Error!
If not then it is not working and could be that mod_rewrite module is remarked out!
Code:
<IfModule mod_rewrite.c>

  TEST

  RewriteEngine On
  RewriteBase /

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

<IfModule !mod_rewrite.c>
  ErrorDocument 404 index.php
</IfModule>

El Forum

[eluser]wiredesignz[/eluser]
[quote author="Aken" date="1328738875"]... that's a crap .htaccess ...[/quote]

Actually it is far better than the .htaccess file you propose.

In your case any file or directory not found will force a redundant call to your codeigniter application. This means if favicon.ico or robots.txt files don't exist your application will run pointlessly and you have no way of controlling this behaviour. If css referenced images are missing the problem is accentuated further.

The better option it to specify which files and directories should be ignored and which should access the application. Like so:
Code:
RewriteEngine On

RewriteCond $1 !^(index\.php|gallery|img|images|pdf|css|js|flash|robots\.txt|favicon\.ico)
RewriteRule ^(.*)$ index.php/$1 [L]

El Forum

[eluser]Aken[/eluser]
[quote author="wiredesignz" date="1328770596"][quote author="Aken" date="1328738875"]... that's a crap .htaccess ...[/quote]

Actually it is far better than the .htaccess file you propose.

In your case any file or directory not found will force a redundant call to your codeigniter application. This means if favicon.ico or robots.txt files don't exist your application will run pointlessly and you have no way of controlling this behaviour. If css referenced images are missing the problem is accentuated further.

The better option it to specify which files and directories should be ignored and which should access the application.[/quote]

Disagree. With your solution, you'd need to update your .htaccess file every single time a new physical file or directory needed to be accessed. That's just one more file that shouldn't need to be touched.

Yes, any other non-existant files will be routed to CodeIgniter. But CI will then see the request, realize it's not a valid URL structure or route or something, and throw a typical 404 error - just like your server would for a non-existant file. As long as you don't autoload a ton of stuff, the overhead is much smaller.

If you want custom 404 pages, you'd have to create them in two different places - one inside CodeIgniter and one independently for server requests.

And then taking into consideration how that RewriteRule is written, if I have any URLs that are not physical files but may start with the same keyword, they will not be routed properly to index.php, thus breaking my application. You're basically giving yourself a list of keywords you can't use.

El Forum

[eluser]daniels[/eluser]
I'm using this and it works perfectly.

Code:
<IfModule mod_rewrite.c>
    RewriteEngine On
    RewriteBase /

    RewriteCond %{REQUEST_URI} !^(/index\.php|/public|/robots\.txt|/favicon\.ico)
    RewriteRule ^(.*)$ /index.php/$1 [L]

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

Now just put all of your static files inside /public folder.
I have /public/css, /public/js, /public/img

El Forum

[eluser]wiredesignz[/eluser]
@Aken, A decent developer is actually supposed to be in control of the application, which means specifying and understanding the application requirements and other things that influence server load and response times.

Your "small overhead" for redundant calls to the application for missing files does not scale in the real world.

If you need to use a specific "keyword" in the URL then you should adjust your RewriteRule accordingly.

El Forum

[eluser]wiredesignz[/eluser]
@daniels, Your first RewriteCond makes the second absolutely useless. And your RewriteBase and use of the leading forward-slash everywhere else is redundant.
Pages: 1 2 3