• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
index.php .htaccess trick won't work

#11
[eluser]daniels[/eluser]
My first RewriteRule checks if REQUEST_URI is /index.php* or /public* or /robots.txt or /favicon.ico
So only this files(index.php, robots.txt, favicon.ico) and path (/public) can be accessed from the web.
If for example i have a folder /uploads that won't be accessible as it does not match the rewrite rule.
If the request was not made to any of this files then it gets rewritten to index.php as it sure is something that shouldn't be accessible from the web or is a route, controller, etc which should be handled by the application. So in this case no need for second rewrite rule, just rewrite it to the application. (No extra checks, the apache doesn't even get to the second rule)

If it has got to the second rule, it means that the request was made for either /robots.txt or /favicon.ico or /index.php or a file inside /public folder.
And if so then i just check if that file exists, if it does we show it if not we get the nice 404 page of our app.
Using only -f without -d also makes sure we only allow access to existing files and not folders, as we don;t need to allow a user to see the contents of /public/img for example. (Yes this can be made also using Options Indexes directives, but we would still have that -d check for nothing.)

Anyway this is how i see things and this is how i use it and for me it works perfectly.


And yea, i think the forward slash might be redundant by using RewriteBase. Didn't thought about that.

LE:

So i tried this as you said:
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>
But i get Internal Server Error, doe too many redirects.

So currently for me it still works only the .htaccess that i posted in my first post.

#12
[eluser]wiredesignz[/eluser]
@daniels, In effect I can't even test your RewiteCond as it stands because it causes an HTTP 500 error on both my development and production servers.

#13
[eluser]InsiteFX[/eluser]
Code:
RewriteRule ^(.*)$ index.php/$1 [L]

What wiredesignz is trying to explain to you is that the [L] flag tells it to stop processing anymore rule sets!

So your first Rewrite Rule will stop your second one from being executed!

#14
[eluser]Aken[/eluser]
[quote author="wiredesignz" date="1328777427"]@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.[/quote]
A decent developer will prevent all of those insignificant file calls from creating 404 errors, and thus preventing the redundant calls in the first place.

Likewise, a good developer will prevent the autoloading of resources on said error calls, thus reducing the amount of overhead to the handful of PHP code that handles a request. Sure, this might not be the best scalable option, but 99.9% of projects the users here deal with will not get anywhere near the amount of traffic required for that to become an issue. And since most people use CodeIgniter for its ease and fast application deployment, I don't see why giving them more work in the interest of nearly infinitesimal performance benefits is worth it.

I think it's also worthy to note that, as exampled by chemiks here, most people have no idea how .htaccess works. So giving them the ability to add it once and never have to edit it again is very useful to them and others giving support.

#15
[eluser]daniels[/eluser]
[quote author="InsiteFX" date="1328780065"]
Code:
RewriteRule ^(.*)$ index.php/$1 [L]

What wiredesignz is trying to explain to you is that the [L] flag tells it to stop processing anymore rule sets!

So your first Rewrite Rule will stop your second one from being executed!
[/quote]

I know what [L] does, and as i explained in my post that is normal behavior.
You need it to stop and not go to second rule if it matches the first condition.
If the request url is not for /index.php, /favicon.ico, robots.txt or something that starts with /public then you NEED it to stop.
No need to continue, as you just rewrite to index.php.

You go to second rule ONLY if the request is made to one of the files, folder (/public/xxx) above.

#16
[eluser]wiredesignz[/eluser]
@Aken, Using a RewriteBase set for docroot is a perfect example of not understanding how to use mod_rewrite.
Edit and forget is just telling people not to think for themselves which should not be an option if you're serious about offering help.


#17
[eluser]daniels[/eluser]
[quote author="wiredesignz" date="1328780001"]@daniels, In effect I can't even test your RewiteCond as it stands because it causes an HTTP 500 error on both my development and production servers. [/quote]

I have no idea what your problem is.
This: http://ellislab.com/forums/viewthread/210623/#977829 works fine on my localhost (Mac OS X, Apache/2.2.17), dev (Ubuntu, Apache/2.2.14) and live (CentOS, Apache/2.2.19)

#18
[eluser]Unknown[/eluser]
And if all to make through robots.txt, the robot will just not index

#19
[eluser]CroNiX[/eluser]
The .htaccess proposed in the upcoming CI3:
https://github.com/EllisLab/CodeIgniter/...l/urls.rst
Quote:RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L]




#20
[eluser]timpiele[/eluser]
[quote author="InsiteFX" date="1328768711"]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>
[/quote]

Awesome, yes I got a 500 error.


[quote author="CroNiX" date="1328812196"]The .htaccess proposed in the upcoming CI3:
https://github.com/EllisLab/CodeIgniter/...l/urls.rst
Quote:RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L]



[/quote]

I dropped this in and it seems to work.

Going back to this convo though, I too have a /public directory, so how do I make that and only that folder web accessible? Does the Deny from All in the /application/.htaccess file prevent all of /application from being web accessible?


Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  


  Theme © 2014 iAndrew  
Powered By MyBB, © 2002-2020 MyBB Group.