How to pass-through requests not going to specific directories to index.php/path?

Go To StackoverFlow.com

2

In my present project I've got several directories: application (my MVC files, which mustn't be accessed), images, css, and js. Effectively I want all requests to images/css/js to proceed unchanged, but all others I wish to call index.php/my/path.

My .htaccess currently looks like this, and is wreaking havoc with my routing.

<IfModule mod_rewrite.c>
  RewriteEngine on
  RewriteCond $1 !^(index\.php|images|js|css|robots\.txt)
  RewriteRule ^(.*)$ http://example.com/index.php/$1 [L]
</IfModule>

This isn't working as relative URLs start stacking up, such as: example.com/blog/view/1/blog/view/2.

When I attempt something like,--

<IfModule mod_rewrite.c>
  RewriteEngine on
  RewriteCond %{REQUEST_URI} !^/(index\.php|images|js|css|robots\.txt)
  RewriteRule ^ index.php%{REQUEST_URI} [PT]
</IfModule>

I get this error with any request: No input file specified.


How can I force all requests not to my whitelisted directories to call, not redirect to (redirection murders posting, I found), index.php/path? IE, when /blog/view/1 is requested by the browser, .htaccess calls index.php/blog/view/1. The reference files at Apache's site aren't too clear about how to do this sort of thing—that, or, I am just missing the point of what I'm reading about RewriteRule.

And, I really want to understand this. Why will your answer work? Why are my attempts failing?

2009-06-16 14:23
by Robert K
For guys not knowing like me: "The L flag will tell Apache to stop processing the rewrite rules for that request." because I used PT before - Kai Noack 2017-11-10 06:50


3

This is what I have in my .htaccess for my framework:

<IfModule mod_rewrite.c>
    RewriteEngine on

    #This will stop processing if it's images
    RewriteRule \.(css|jpe?g|gif|png|js)$ - [L] 

    #Redirect everything to apache
    #If the requested filename isn’t a file….
    RewriteCond %{REQUEST_FILENAME} !-f
    #and it isn’t a folder…
    RewriteCond %{REQUEST_FILENAME} !-d

    RewriteRule ^(.*)$ index.php?$1 [L,QSA] 
    #L = (last - stop processing rules)
    #QSA = (append query string from requeste to substring URL)
</IfModule>

Hope this helps.

PS: Maybe you want to remove the lines to stop redirecting if it's a file or folder ;)

2009-06-16 14:32
by AntonioCS
I want all routes that aren't in these whitelisted directories to go to "index.php/request/uri" explicitly. Otherwise, if they so desire they can view my controllers - Robert K 2009-06-16 14:36


2

Antonio helped me get on the right track, so here's the resulting .htaccess:

<IfModule mod_rewrite.c>
  RewriteEngine on
  # skip if whitelisted directory
  RewriteRule ^(images|css|js|robots\.txt|index\.php) - [L]
  # rewrite everything else to index.php/uri
  RewriteRule . index.php%{ENV:REQUEST_URI} [NE,L]
</IfModule>
2009-06-16 15:31
by Robert K


0

You're going to have to do that using PHP. For example, if you wanted to split your URI into something like domain.tld/controller/action/param, then you could use the following PHP code as a start:

<?php

// Filter URI data from full path
$uri_string = str_replace($_SERVER['SCRIPT_NAME'], '', $_SERVER['REQUEST_URI']);
$uri_string = trim($uri_string, '/');    // Make sure we don't get empty array elements

// Retrieve URI data
$uri_data = explode('/', $uri_string);

In that case, $uri_data[0] is the controller, $uri_data[1] is the action, and beyond that are parameters. Note that this isn't a foolproof method, and it's never a great idea to trust user-entered input like this, so you should whitelist those controllers and actions which can be used.

From here, knowing the controller and having a consistent directory structure, you can require_once the proper controller and call the action using variable variables.

2009-06-16 15:44
by Andrew Noyes
I already have the code that handles the URI parsing, I just needed to get .htaccess working to protect my sensitive directories and tidy the url a bit - Robert K 2009-06-16 16:28
Oh okay, I misunderstood your question. I thought you were trying to actually have the URI processed using .htaccess - Andrew Noyes 2009-06-16 16:41


0

This is what I use in my .htaccess file for my CMS:

Options -Indexes

RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.+)$ index.php/$1 [NC,L]

And then in my index.php file I have:

$path_info = '';
$path_info = isset($_SERVER['PATH_INFO']) ? $_SERVER['PATH_INFO'] : $path_info;
$path_info = isset($_SERVER['ORIG_PATH_INFO']) ? $_SERVER['ORIG_PATH_INFO'] : $path_info;

$request = explode('/', trim($path_info, '/'));

// if $request[0] is set, it's the controller
// if $request[1] is set, it's the action
// all other $request indexes are parameters

Hope this helps.

2011-10-04 14:20
by Martin Bean
Ads