TalkPHP

TalkPHP (http://www.talkphp.com/forums.php)
-   Advanced PHP Programming (http://www.talkphp.com/advanced-php-programming/)
-   -   Need help of Mod Rewrite (http://www.talkphp.com/advanced-php-programming/3870-need-help-mod-rewrite.html)

kokjj87 01-15-2009 03:03 AM

Need help of Mod Rewrite
 
Hi, i am trying to rewrite my url.
-the first rule will rewrite images/C.jpg to images/C.php.
But the second line of my mod rewrite would redirect all files except for(js, gif, jpg, png and css) to index.php.

The problem is, after my images/C.jpg is rewrite, the request will treat is as requesting a php file, and would be redirected to the index.php, so having a [L](Lock) would be useless too??

how do i solve this?

Thanks

I currently have this:
Code:

RewriteRule images/C.jpg images/C.php [NC,L]
RewriteRule !\.(js|gif|jpg|jpeg|png|css)$ index.php [NC,L]

And also Mod Rewrite, is really hard to debug. Is there any tools out there, that can help us to debug?

jlodell 02-22-2009 07:22 AM

i use something along the lines of the following...

Code:

RewriteEngine on
Options FollowSymLinks
## IF
## URL does not exist
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
## THEN
## Rewrite to bootstrap
RewriteRule ^(.*)$ index.php/$1 [PT,L,QSA]


xenon 02-22-2009 04:06 PM

@jlodell: he wants to deny access to an existing file, so your rules will have no effect there.

one quick fix would be to add images/C.php into the second's rule exception list, which should be a RewriteCond rule rather than a RewriteRule:

Code:

# your rules go here

RewriteCond $1 !(\.(?:js|gif|jpg|jpeg|png|css)|(?:images\/C\.php))
RewriteRule ^(.*)$ index.php/$1 [QSA,L]

ps: L in [L] stands for last rule, not lock.
ps2: ALWAYS escape characters other than letters and numbers in regex rules. just to be on the safe side.

jlodell 02-22-2009 06:08 PM

@xenon: you are right, but i'm just throwing out another approach on how to go about filtering with mod_rewrite. I honestly don't know what is going on with the C.jpg -> C.php but i don't see why that would imply that it is for denying access to a file?

Clarity...
What I really meant was, "Here is a basic start at a .htaccess file that shows a different approach to redirecting traffic thru a front controller."

Code:

RewriteEngine on
Options FollowSymLinks
## IF
## URL does not exist
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
## THEN
## Rewrite to bootstrap
RewriteRule ^(.*)$ index.php/$1 [PT,L,QSA]

So then if you take your .jpg->.php rule and stick it in...

Code:

RewriteEngine on
Options FollowSymLinks

###### YOUR RULE ######
RewriteRule images/C.jpg images/C.php [NC]

## IF
## URL does not exist
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
## THEN
## Rewrite to bootstrap
RewriteRule ^(.*)$ index.php/$1 [PT,L,QSA]

as long as C.php exists then everything is kosher.

2 cents:
I don't see the reasoning for the jpg->php change? I guess i am missing it or something. i realize that this will keep the image from getting served but wouldn't it be easier to just place it somewhere else instead of checking every request? Using this method it will NEVER get served to any request. It isn't like it is being filtered to only be served to a certain IP or what-not. It just seems a bit pointless, thats all.

good luck, justin


All times are GMT. The time now is 06:55 AM.

Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0