4

I want to use URLs like the followings :

http://mydomain.com/320x200/server/path/to/my/image.jpg

Where you can find 3 parameters to retrieve for rewriting :

  1. 320x200 : optional parameter, can be two numbers (like "320x200"), OR a single number (like "320x") OR empty (only "x")
  2. server : required (this is a specific parameter to find a server where image is hosted, but does not really matter for this case)
  3. path/to/my/image.jpg : required

and rewrite it with another domain like the followings :

http://myotherdomain.com/320/200/server/path/to/my/image.jpg

I tried the following rewrite rules but it is not working :

RewriteRule ^([0-9]+)x([0-9]+)/([a-zA-Z0-9]+)/([a-zA-Z0-9/.]+)$ htp://myotherdomain.com/$1/$2/$3/$4 [L]

RewriteRule ^([0-9]+)x/([a-zA-Z0-9]+)/([a-zA-Z0-9/.]+)$ htp://myotherdomain.com/$1/$2/$3 [L]

RewriteRule ^x/([a-zA-Z0-9]+)/([a-zA-Z0-9/.]+)$ htp://myotherdomain.com/$1/$2 [L]

Why is it not working ?

The 3 regex are working when tested through a website like regexplanet.com

I tried to clear browser cache, restart Apache, remove cookies, ... still not working !

Thanks for your help


Edit :

Finally, the problem was that my .htaccess file was not correctly saved (don't know why).

I just closed and opened the .htaccess again, everything ok !

Nouvi
  • 43
  • 3
  • can you help me with my question here: http://stackoverflow.com/q/29898156/1478789 i have almost identical case as you. I would really appreciate your help. – Jeremy Apr 28 '15 at 00:01

2 Answers2

3

You should divide the work by grouping. Don't bother converting x to / just don't capture it.

RewriteRule ^([0-9]+)x([0-9]+/)([a-zA-Z0-9]+)/([a-zA-Z0-9/.]+)$ http://myotherdomain.com/$1/$2$3/$4 [L]

RewriteRule ^([a-zA-Z0-9]+)/([a-zA-Z0-9/.]+)$ http://myotherdomain.com/$1/$2 [L]
  • ([0-9]+) captures your two resolutions parameters individually.
  • (?:....)? is a non capturing grouping rendered optional.

Edit:

The optionality is a trap. Don't use it -> second rule.

If the other domain end up being the same domain:

RewriteCond %{REQUEST_URI} ![0-9]+/[0-9]+.*$

Will prevent loops.

M'vy
  • 5,696
  • 2
  • 30
  • 43
  • Sorry but it is not working. With 320x200, it is working, but if I remove it (because it is optional), I get a 404 error. – Nouvi Jul 21 '11 at 11:48
  • Updated to take care of the problem. – M'vy Jul 21 '11 at 12:21
  • Thanks, but if you write 2 rules, why do you keep your non capturing grouping in the first rule ? – Nouvi Jul 21 '11 at 12:37
  • Ok, I think it will work... but I have another problem : how to clear the cache for rewritten urls ? – Nouvi Jul 21 '11 at 13:03
  • Finally, the 2nd rule is not working... I always get a 404 error when I remove 320x200... Thanks for helping me :-) – Nouvi Jul 21 '11 at 13:30
  • And what is the address that generates the 404 error? Maybe the rewrite is working but there is no page on the target site. – M'vy Jul 21 '11 at 13:45
  • I tested the 2 rules with http://www.regexplanet.com/simple/index.html and they are working perfectly. To see my htaccess file, see my first post edited... – Nouvi Jul 21 '11 at 13:58
  • @M'vy stumbled upon this old answer, i am trying to replicate this answer for my own case with no luck. Could you perhaps help me with my question here : http://stackoverflow.com/q/29898156/1478789. I really appreciate your help. – Jeremy Apr 28 '15 at 00:02
0

I haven't tried it, but i hope it works:

RewriteRule ^(([0-9]+)(x)([0-9]+)/|)([a-zA-Z0-9]+)\/([a-zA-Z0-9_\/\.-]+)$ htp://myotherdomain.com/$2$3$4$5$6/$7 [L]
$2 = 320 or empty
$3 = 'x' or empty
$4 = 200 or empty
$5 = '/' or empty
$6 = 'server'
$7 = path to image
JercSi
  • 1,037
  • 1
  • 9
  • 19
  • Sorry but not working... I tried similar regex, but still not working, don't know why. My problem is to find how to make an optional parameter (i am trying to write different rules, keep you in touch ! thanks). – Nouvi Jul 21 '11 at 12:14
  • I've finally got home.. i see that in this time M'vy already posted solution with two RewriteRules. Sometimes clear cache in browser works. But i suppose that you already tried this and it is not a problem in browser. – JercSi Jul 21 '11 at 13:31
  • Well, I am trying the 2 rules solution, but the 2nd one does not work... Or my cache is not cleared... I tried restarting Apache, clearing browser cache, cookies, ... – Nouvi Jul 21 '11 at 13:37
  • i've splited my rewriterule into two: Can you try? RewriteRule ^([a-zA-Z0-9]+)\/([a-zA-Z0-9_\/\.-]+)$ htp://myotherdomain.com/$1/$2 [L] RewriteRule ^(([0-9]+)(x)([0-9]+)/|)([a-zA-Z0-9]+)\/([a-zA-Z0-9_\/\.-]+)$ htp://myotherdomain.com/$2/$4$5$6/$7 [L] – JercSi Jul 21 '11 at 15:28
  • Does not work... Dont you think I have to switch your 2 rules ? And it seems that the second one has only 6 parameters (not 7)... – Nouvi Jul 22 '11 at 08:34
  • oups, i see that i copied you wrong second rule: RewriteRule ^([0-9]+)x([0-9]+)\/([a-zA-Z0-9]+)\/([a-zA-Z0-9_\/\.-]+)$ htp://myotherdomain.com/$1/$2/$3/$4 This url might help you to test regular expressions: http://www.regexplanet.com/simple/ – JercSi Jul 22 '11 at 09:27
  • Finally, my rewrite rules (see original post) are working perfectly. The problem was that my .htaccess file was not correctly saved (don't know why). I just closed and opened the .htaccess again, everything ok ! Many thanks to JerSi and M'vy for your help !! – Nouvi Jul 22 '11 at 10:32