Eugene Tang
Eugene Tang

Reputation: 83

How to i change the rewrite rule to read images only

hi all i have this question as my rewrite rule is shown below. But everytime when i am using this code the js file wont work.

RewriteRule ^([^thumb].*\.[jJ].*)$ /images/files/w.php?i=$1

Can anyone tell me where went wrong?

Upvotes: 0

Views: 85

Answers (2)

JaredMcAteer
JaredMcAteer

Reputation: 22545

In order to handle more than just jpgs you can change it to be the following:

RewriteRule ^([^thumb].*\.(jpe?g|png|gif).*)$ /images/files/w.php?i=$1

This will covers jpg, jpeg, png and gif file extensions while not matching javascript files.

Note: [^thumb].* won't match any file names starting with t, h, u, m, or b, (e.g., test.jpg, home.jpg, umbrella.jpg, monster.jpg, or beach.jpg won't match) not just files starting with thumb..., for the whole word, thumb, you need a negative lookahead (?!regex). To make the rule not match files starting with "thumb" this is what the rule should look like:

RewriteRule ^((?!thumb).*\.(jpe?g|png|gif).*)$ /images/files/w.php?i=$1

One last problem the rule handles text after the file extension which I assume means that you may have urls that look like:

http://www.example.com/test.png?foo=a&b=baz

Your rule will create a rewrite that ends up looking like

http://www.example.com/images/files/w.php?i=test.png?a=foo&b=baz

Which is not a valid url as there are now two ?s in the query string. To fix this I'd go with the following regex.

RewriteRule ^((?!thumb).*\.(?:jpe?g|png|gif))\??(.*)$ /images/files/w.php?i=$1&$2

This regexp rule will result in a properly formed url

http://www.example.com/images/files/w.php?i=test.png&a=foo&b=baz

Upvotes: 2

arkascha
arkascha

Reputation: 42984

Quick and dirty 'solution', since you obviously want to separate jpeg and js:

RewriteRule ^([^thumb].*\.[jJ][pP].*)$ /images/files/w.php?i=$1

Upvotes: 2

Related Questions