views:

235

answers:

6

For example, I have an URL that looks for an image like this:

http://example.com/img/foo.png
http://example.com/img/interface/menu/bar.png
http://example.com/static/users/avatars/small/3k5jd355swrx221.jpg

I don't want to redirect those. They should just pass through. But then, I have URLs like this:

http://example.com/register/
http://example.com/my_account/my_picture/
http://example.com/contact/email/

All such URLs that don't request for an .png or .jpeg should be redirected to:

http://example.com/index.php/x

Where x stands for everything after example.com/, so in this example for example:

http://example.com/register/ to
http://example.com/index.php/register/

http://example.com/my_account/my_picture/ to
http://example.com/index.php/my_account/my_picture/

http://example.com/contact/email/ to
http://example.com/index.php/contact/email/

(AcceptPathInfo is enabled)

Is there any way to do that in the .htaccess? I only know how I could do this if I had always something like http://example.com/someKindOfMarkerHere/stuff/stuff/stuff but I don't want to have the someKindOfMarker there to detect if it's an URL that has to be rewritten. I don't know how to exclude them.

+3  A: 

You can either exclude specific URLs:

RewriteEngine on
RewriteCond %{REQUEST_URI} !^/index\.php$
RewriteRule !.*\.(jpeg|png)$ index.php%{REQUEST_URI}

Or you exclude any existing file:

RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php%{REQUEST_URI}
Gumbo
Nice. That'll do it.
Christian
+1  A: 

Hell yes it's possible.

mod_rewrite will do all of that for you pretty easily.

You can also set up an error handler, so every 404 on your site gets redirected through index.php. This is a nice little way of making sure all requests load index.php (or your bootstrap).

The mod_rewrite will need a regex and regex's hurt my head, so I'll let somebody else write one.

Hope that helps. Just comment if you need more info from me. :)

Christian
+2  A: 
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} ^.+\.png$ [OR]
RewriteCond %{REQUEST_FILENAME} ^.+\.jp(e)?g$ 
RewriteRule ^.*$ - [NC,L]
RewriteRule ^.*$ index.php/%{REQUEST_URI} [NC,L]
Stefan Gehrig
+1  A: 

Put something like this in a .htaccess file and make sure mod_rewrite is enabled:

RewriteEngine On

RewriteRule ^(.*?)(?!(\.png|\.jpg))$ index.php/$1

http://www.regular-expressions.info/lookaround.html

Aistina
+1  A: 

I would use a slight variation to Gumbo's answer:

RewriteCond %{REQUEST_FILENAME} !-d 
RewriteCond %{REQUEST_FILENAME} !-f 
RewriteRule ^ index.php%{REQUEST_URI}

It excludes folders as well as files (the !-d flag) - you may not may not want this, but think it belongs here for completeness.

benlumley
A: 

The following ignores existing files, folder and files with the extension matching: jpg, jpeg, png, gif. If you wish to add additional extension, just add "|extension" before the )$ on line 3.

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !.(jpg|jpeg|png|gif)$
RewriteRule ^(.*)?$ /index.php/$1 [QSA,L]
philhq