Forum Moderators: phranque

Message Too Old, No Replies

rewrite directory depending on screen resolution

modrewrite cookies and resolution

         

qvrb

7:18 pm on Aug 28, 2014 (gmt 0)

10+ Year Member



What I want to do is to use a different directory for images depending on the resolution and whether the file exists or not

I set a cookie called resolution in the php, and i calculate cookie from a bit of javascript, i'm then trying to pick up the cookie in htaccess and if the file exists in a sub directory rewrite that, ideally i'd like it so that if the resolution is 780 it would use the narest file that had a directory, but i'm not sure how to do greater than and less than.

RewriteEngine On
# get the cookie which i presume goes into %1
RewriteCond %{HTTP_COOKIE} resolution=^([0-9]+)$[AND]
# the matching directory structure should look like
# /wp-content/uploads/2014/08/filename.(jpg|png)
RewriteCond ^/wp-content/uploads/([0-9]{4}/[0-9]{2})/%1/(.*\.(jpg|jpeg|png|gif))$
# i'm not sure if i can use %1 in here again
RewriteRule ^/wp-content/uploads/([0-9]{4}/[0-9]{2})/(.*)$ $1/%1\/$2 [L]

Any help gratefully appreciared.

lucy24

9:17 pm on Aug 28, 2014 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



resolution=^([0-9]+)$[AND]

Ouch. Did you try this? Did your server explode?

#1 there seems to be an extraneous ^ in a position where it's meaningless
#2 there's no space between pattern and [AND] flag
#2b in any case [AND] is the default, so you can just omit it

Now, there's a more serious problem, which by amazing coincidence I just explained in detail a few hours ago in a neighboring thread.

RewriteRule ^/wp-content/uploads/([0-9]{4}/
[0-9]{2})/(.*)$ $1/%1\/$2 [L]

Is this rule lying loose in the config file? If not (either htaccess or <Directory> section), it will always fail if the pattern has a leading / so leave that out. But don't throw the slash away; you need it at the front of the [L] target instead.

get the cookie which i presume goes into %1

Unfortunately, no. See above about amazing coincidence [webmasterworld.com]. mod_rewrite can only use captures from the most recently matched condition. That would be the second condition ... which appears to be a bit garbled anyway. Is it meant to look at %{THE_REQUEST}? Or at %{REQUEST_URI}? If so you don't need it, because that belongs in the body of the rule-- where, in fact, it looks as if you've already got it. This in turn means you may be in luck, because if there is only one Condition then you can capture from it :)

Since the rewrite target adds something to the middle of the URL path, you won't need a Condition looking at %{THE_REQUEST}. That's only needed when there's a risk of infinite loops.

Oh yes and: you don't need to escape slashes in the target (or, for that matter, anywhere else in mod_rewrite). Was that a typo?

phranque

9:38 pm on Aug 28, 2014 (gmt 0)

WebmasterWorld Administrator 10+ Year Member Top Contributors Of The Month



welcome to WebmasterWorld, qvrb!


using RewriteCond...

http://httpd.apache.org/docs/current/mod/mod_rewrite.html#rewritecond
You can perform lexicographical string comparisons:


and if you are running apache 2.4
You can perform integer comparisons: