3

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/([a-z]+)/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/cfrom 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?

Since this is solved I want to elaborate on the usage for future viewers:

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.

2
  • Try it where you change [L] to [QSA,L]. I have some lines exactly like you wrote in my htaccess for search page paging... the only difference is I have QSA in the flags. Doubt its that easy of a solution though. Commented Nov 10, 2017 at 18:38
  • Sadly it didn't but it was worth a shot. Commented Nov 10, 2017 at 18:44

1 Answer 1

2

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]
Sign up to request clarification or add additional context in comments.

5 Comments

I tested all these on my apache server, and all results were OK.
I gave the first RewriteRule you listed a go and it works flawlessly. It's amazing how one little missing slash can cause a world of problems. This is perfect though. Now I can easily just define thumb, small, medium, large in my image urls to get varying sizes on the fly. Thank you very much.
Ah thanks for clarifying which was the right way. I wasnt entirely sure because your example made me re-think that you meant the longer url. And yes, its fun doing some of these rewrites. I don't think anything on my server is 'face value' anymore LOL.
Yeah I know I have some cleaning to do. Essentially what it is though I was wanting urls like http://example.com/thumb/img/stock/example.jpg for thumbnail images with predefined sizes and others like http://example.com/small/img/stock/example.jpg, http://example.com/large/img/stock/example.jpg, and so forth with predefined sizes and each with it's own rewrite rules.
I updated my question with an elaboration of the usage.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.