Jake
Jake

Reputation: 1501

Redirecting to a photo with htaccess

Basically I'm using htaccess I have a hidden directory which I'd rather not be used and I want shorter links:

I would like

http://example.com/12940.png

to goto

http://example.com/_images/12940.png

Here's my rule:

RewriteRule ^([A-Za-z0-9\_\-\/]+).([A-Za-z]+)$  "_images/$1.$2"

What's wrong with it, I get 500.

Upvotes: 4

Views: 64

Answers (2)

Jona
Jona

Reputation: 2147

This rule fails because you have a / in it. So the redirected URL (_images/12940.png) is caught by the rule again, and mod_rewrite trys to redirect it to (_images/_images/12940.png).

So you should check if the path starts allready with _images:

RewriteRule ^(?!_images)([A-Za-z0-9\_\-\/]+).([A-Za-z]+)$  "_images/$1.$2"

Also you could further improve your rule like this:

RewriteRule ^(?!_images)([a-z0-9_\-/]+\.[a-z]+)$  _images/$1 [NC]

No need to escape the _ and /, but you should escape the . because it matches any character otherwise. The NC makes the rule caseinsensitive, so you don't need the extra A-Z.

Upvotes: 0

Book Of Zeus
Book Of Zeus

Reputation: 49887

Here's what you have to do:

RewriteEngine On
RewriteRule ^([a-z0-9_\-]+)\.([a-z]+)$ _images/$1.$2 [NC,L]

by using NC (case insensitive) you don't need to put A-Za-Z and L means the last rule in case you have other rules after this one.

Upvotes: 3

Related Questions