I've search high and low for a solution to this. More than likely I'm just not using the right keywords.
I'm trying to rewrite some image urls to clean them up. Easy stuff.
I've managed to get urls from
http://example.com/img/thumb.php?h=400&w=550&a=c&src=/img/stock/example.jpg
to
http://example.com/h/400/w/550/a/c/thumb/img/stock/example.jpg
Easy stuff using RewriteRule ^h/(\\d+)/w/(\\d+)/a/([az]+)/thumb/(.+)$ /img/thumb.php?h=$1&w=$2&a=$3&src=$4 [L]
.
However I'm wanting to clean it up even further by defining this part in my .htaccess file: h=400&w=550&a=c
to remove h/400/w/550/a/c
from the image url so that the new thumb url is simply http://example.com/thumb/img/stock/example.jpg
.
I tried simply defining it in mod_rewrite like so but the image isn't changing to the defined sizes.
RewriteRule ^thumb/(.+)$ /img/thumb.php?h=400&w=550&a=c&src=$1 [L]
Is this not the right way to do it?
The end goal was to create various image sizes on the fly but with cleaner urls.
I am using my .htaccess
to contain the predefined sizes written in clean urls. There are five different sizes that I am using.
Here are the lines for creating these 5 sizes:
# CLEAN IMAGE URLS
# CREATE VARYING SIZED IMAGES WITH PREDEFINED SIZES
RewriteRule ^img/thumb/(.+)$ /img/thumb.php?h=400&w=550&a=c&f=2&src=/img/$1 [L]
RewriteRule ^img/small/(.+)$ /img/thumb.php?h=200&w=275&a=c&f=2&src=/img/$1 [L]
RewriteRule ^img/medium/(.+)$ /img/thumb.php?h=500&w=688&a=c&f=2&src=/img/$1 [L]
RewriteRule ^img/large/(.+)$ /img/thumb.php?h=750&w=1031&a=c&f=2&src=/img/$1 [L]
RewriteRule ^img/display/(.+)$ /img/thumb.php?h=600&w=600&a=c&f=2&src=/img/$1 [L]
This way for a medium
sizes image I simply wrote the url as http://example.com/img/medium/folder/img.jpg
and had an image with the dimensions of 500x688
. This prevented me from having to have 5 different images of the same image uploaded to the server.
You are on the right path (that RewriteRule
you have is legit), but you may have a slight issue with your thumb.php's reasoning with the GET args.
If you add print_r($_GET);exit;
to the top of your thumb.php, and hit this url:
http://example.com/thumb/img/stock/example.jpg
It should spit out:
Array
(
[h] => 400
[w] => 550
[a] => c
[src] => img/stock/example.jpg
)
The difference is that the src
does not come with a leading slash, as your example originally has for the old url:
http://example.com/img/thumb.php?h=400&w=550&a=c&src=/img/stock/example.jpg
So you can solve that one of two ways. First, simply add the slash in php when dealing with the src. The other, is to add a slash in the rewrite rule:
RewriteRule ^thumb/(.+)$ /img/thumb.php?h=400&w=550&a=c&src=/$1 [L]
OR
If are you trying to rewrite an url of:
http://example.com/h/400/w/550/a/c/thumb/img/stock/example.jpg
Using RewriteRule ^thumb/(.+)$
... then you need to change that to:
RewriteRule ^(.*)thumb/(.+)$ /img/thumb.php?h=400&w=550&a=c&src=/$2 [L]
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.