Ad

How Can I Redirect Every URL That's Not Requesting An JPEG Or PNG Image To Index.php/x?

- 1 answer

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.

Ad

Answer

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}
Ad
source: stackoverflow.com
Ad