Forum Moderators: phranque

Message Too Old, No Replies

[Help] htaccess redirect

         

gunz

11:35 am on Jun 29, 2012 (gmt 0)

10+ Year Member



Hello guys,

I need a little help about redirect, i have a problem and i don't know how to solve.

I redirect:

http://example.com/index.php?p=image&url=media/qkg/1296779201/logo.png

to

http://www.s3.example.com/media/qkg/1296779201/logo.png

wit this rule:



RewriteCond %{QUERY_STRING} ^(.*&)?p=image&url=media/([^&]+)(&.*)?$ [NC]

RewriteRule ^index\.php$ http://www.s3.example.com/media/%2? [NC,L,R]



That work perfect.

Now i need a little help about other two redirect.

1.

=====================================================

From:

http://example.com/thumb.php?i=media/gb9/1281954648/Shot00007.jpg&d=160x160

To:

http://s3.example.com/thumb.php?i=media/gb9/1281954648/Shot00007.jpg&d=160x160

I tried with:



#RewriteCond %{QUERY_STRING} ^(.*&)?i=media/([^&]+)(&.*)?$ [NC]

#RewriteRule ^thumb\.php$ http://www.s3.example.com/thumb.php?i=media/%2? [NC,L,R]



But the first link is redirect to

http://www.s3.example.com/thumb.php?i=media/gb9/1281954648/Shot00007.jpg%3f

With this %3f at the end that give an error on the thumb.

2.

=====================================================

From:

http://www.example.com/media/qkg/1296779201/logo.png

To:

http://www.s3.example.com/media/qkg/1296779201/logo.png

I tried with:



RewriteCond %{QUERY_STRING} ^(.*&)media/([^&]+)(&.*)?$ [NC]

RewriteRule ^media$ http://www.s3.example.com/media/%2? [NC,L,R][NC,L,R]



But it doesn't work.

Thank you for help

Cheers

[edited by: incrediBILL at 4:18 am (utc) on Jul 2, 2012]
[edit reason] fixed URLS, use Example.com [/edit]

g1smd

11:59 am on Jun 29, 2012 (gmt 0)

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



First example:
RewriteCond %{QUERY_STRING} ^(.*&)?p=image&url=media/([^&]+)(&.*)?$ [NC]
RewriteRule ^index\.php$ http://www.example.com/media/%2? [NC,L,R]

While it might appear to work, there's a number of errors in there.

RewriteCond %{QUERY_STRING} (^|&)p=image&url=media/([^&]+)(&|$) [NC]
RewriteRule ^(index\.php)?$ http://www.example.com/media/%1? [R=301,L]


Redirect will fail if
p=image
is not immediately before
url=


Never use .* at the beginning of a pattern. Trailing unused .* does not need to be stated or captured.

Allow for query string directly on root, as well as on named index file. Both should be redirected.


Use example.com in this forum.

gunz

2:29 pm on Jun 29, 2012 (gmt 0)

10+ Year Member



Thank you for answer g1smd.

I tested you code and didin't work. It redirect me to:

http://www.example.com/media/

About point 1. and 2.?

Cheers

gunz

2:34 pm on Jun 29, 2012 (gmt 0)

10+ Year Member



Okay now with this it works fine:

RewriteCond %{QUERY_STRING} (^|&)p=image&url=media/([^&]+)(&|$) [NC]
RewriteRule ^(index\.php)?$ http://www.example.com/media/%2? [R=301,L]

I changed the %1 with %2.

[edited by: incrediBILL at 4:16 am (utc) on Jul 2, 2012]
[edit reason] fixed URLS, use Example.com [/edit]

g1smd

2:36 pm on Jun 29, 2012 (gmt 0)

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



Typo: %1 should be %2. Not sure how that changed.


Let's see your code for the other problems.

Use example.com in this forum. Click 'Disable Smilies' otherwise your code is unreadable.

gunz

2:49 pm on Jun 29, 2012 (gmt 0)

10+ Year Member



Yes sorry. Now i was checking the point 1.

=====================================================

From:

example.com/thumb.php?i=media/gb9/1281954648/Shot00007.jpg&d=160x160

To:

s3.example.com/thumb.php?i=media/gb9/1281954648/Shot00007.jpg&d=160x160

I'm using that code:


#RewriteCond %{QUERY_STRING} (^|&)i=media/([^&]+)(&|$) [NC]
#RewriteRule ^(thumb\.php)?$ http://www.s3.example.com/thumb.php?i=media/%2? [R=301,L]


But it didn't work, because don't take that part "&d=160x160".

Sorry but i didn't find "Disable Smilies".

[edited by: gunz at 3:00 pm (utc) on Jun 29, 2012]

g1smd

2:49 pm on Jun 29, 2012 (gmt 0)

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



Use example.com in this forum. Click 'Disable Smilies' otherwise your code is unreadable.

The edit button is just below your username.

gunz

3:00 pm on Jun 29, 2012 (gmt 0)

10+ Year Member



Thank you for help, i modified my last post.

g1smd

3:11 pm on Jun 29, 2012 (gmt 0)

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



You'll need to also capture the value part of the
d=
parameter in another backreference.

However if the whole query string is identical in both URLs you don't need to capture and reuse anything, just re-append the original query string.

The RewriteCond will need to just test that the right query strings are being requested, without capturing anything. The rule target will include the hostname and path, but the query string will be re-appended without stating it.

gunz

3:43 pm on Jun 29, 2012 (gmt 0)

10+ Year Member



Yes the URL is the same, the change is only on the domain from example.com to s3.example.com .

gunz

6:26 am on Jul 3, 2012 (gmt 0)

10+ Year Member



Can you give me an example? to do that:

From:

example.com/thumb.php?i=media/gb9/1281954648/Shot00007.jpg&d=160x160

To:

s3.example.com/thumb.php?i=media/gb9/1281954648/Shot00007.jpg&d=160x160

g1smd

6:43 am on Jul 3, 2012 (gmt 0)

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



Is it for just that one URL?

Is it for a number of URLs? In which case, what things are common to them all?

gunz

6:58 am on Jul 3, 2012 (gmt 0)

10+ Year Member



Now is not only for that url, for example that is another images.

example.com/thumb.php?i=media/8k5/1281716614/Shot00002.jpg&d=160x160

The part that can change is only "8k5/1281716614/Shot00002.jpg" the rest is fixed.

g1smd

7:06 am on Jul 3, 2012 (gmt 0)

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



Use a RewriteRule to redirect requests for
^thumb\.php
to the new hostname and path.

Use a
RewriteCond
to detect that the requested
QUERY_SRING
contains
(^|&)d=160x160(&|$)


Specify the new hostname and path in the rule target. Use the
[R=301,L]
flags and the query string will be automatically re-appended unaltered.

gunz

7:25 am on Jul 3, 2012 (gmt 0)

10+ Year Member



Thank you for help, something like that?


RewriteCond %{QUERY_STRING} (^|&)d=160x160(&|$) [NC]
RewriteRule ^(thumb\.php)?$ http://www.s3.example.com/thumb.php?%2? [R=301,L]

lucy24

9:54 am on Jul 3, 2012 (gmt 0)

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



You shouldn't need an [NC] in there. Presumably you set your own query strings so their names will always be in the same form.

You might say \b instead of (&|$) at the end of the Condition. But an ending anchor probably isn't necessary at all, unless you might have a query value d=160x1600 that you don't want to send to your subd...

Urk! Where is this Rewrite located? If it is in a directory where requests for s3.example.com will also pass through, you need a second RewriteCond saying "only do this if the host is not already www.s3.example.com".

gunz

10:20 am on Jul 3, 2012 (gmt 0)

10+ Year Member



If i try to use that:

RewriteCond %{QUERY_STRING} (^|&)d=160x160(&|$) [NC]
RewriteRule ^(thumb\.php)?$ http://www.s3.example.com/thumb.php?%2? [R=301,L]


From

example.com/thumb.php?i=media/cvz/1280699489/Shot00011.bmp&d=192x192

I'm redirected to

s3.example.com/thumb.php?%3f

g1smd

12:53 pm on Jul 3, 2012 (gmt 0)

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



You don't need to specify ?%2? here.

Query strings are re-appended automatically unless otherwise stated.


You said you wanted to redirect when parameter contains d=160x160 but now you're talking about d=192x192. Do you want to redirect all sizes?

gunz

1:25 pm on Jul 3, 2012 (gmt 0)

10+ Year Member



Sorry about that, no it's always 160x160.

Thank you for help

gunz

1:36 pm on Jul 3, 2012 (gmt 0)

10+ Year Member



If i try

RewriteCond %{QUERY_STRING} (^|&)d=160x160(&|$) [NC]
RewriteRule ^(thumb\.php)?$ http://www.example.com/thumb.php? [R=301,L]


Is redirected to:

example.com/thumb.php

Can you write the correct rwrite? Thank you

g1smd

9:36 pm on Jul 3, 2012 (gmt 0)

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



The trailing question mark on the rule target clears the query string.

Omit the question mark and mod_rewrite will automatically re-append the original query string.

gunz

10:16 pm on Jul 3, 2012 (gmt 0)

10+ Year Member



Thank you very much it worked! Now i know how to do it.

RewriteCond %{QUERY_STRING} (^|&)d=160x160(&|$) [NC]
RewriteRule ^(thumb\.php)?$ http://www.s3.example.com/thumb.php [R=301,L]


While if i want to change also the 160x160 i mean there are different images with different value.

g1smd

10:26 pm on Jul 3, 2012 (gmt 0)

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



If you want to redirect any numbers you can do
(^|&)d=[0-9x]+(&|$)
or
(^|&)d=[0-9]+x[0-9]+(&|$)
or similar.

If you only want specific numbers, perhaps
(^|&)d=1(60|92)x1(60|92)(&|$)

lucy24

11:30 pm on Jul 3, 2012 (gmt 0)

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



s3.example.com/thumb.php?%3f

Reading back over this thread it's not totally clear whether you understood what's happening there. %3F is the percent-encoded form of the question mark ? If you've already got a ? denoting "query string" then any further question marks will be treated as text. They will automatically be encoded when they go out into the world.

gunz

7:16 am on Jul 4, 2012 (gmt 0)

10+ Year Member



Thank you g1smd, it worked perfect!

Now last point and we had finished :)

From:

htp://www.example.com/media/qkg/1296779201/logo.png

To:

htp://www.s3.example.com/media/qkg/1296779201/logo.png

I tried with this but didn't work:


RewriteCond %{QUERY_STRING} (^|&)/media/(&|$) [NC]
RewriteRule ^(media)$ http://www.s3.example.com/media/ [R=301,L]

lucy24

8:01 am on Jul 4, 2012 (gmt 0)

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



^(media)$

means "The request is for the exact URL 'www.example.com/media'" (anchor at both ends). You do not often see extensionless URLs with attached query strings. Conversely, you do not often see query strings named that begin and end with a directory slash. Did you simply cut-and-paste too fast?

No need for parentheses, since you are not doing anything with the word.

gunz

9:08 am on Jul 6, 2012 (gmt 0)

10+ Year Member



Can you fix it for me?

From:
htp://www.example.com/media/qkg/1296779201/logo.png

To:
htp://www.s3.example.com/media/qkg/1296779201/logo.png

lucy24

9:25 pm on Jul 6, 2012 (gmt 0)

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



Backtracking further:

There has been a lot of talk about query strings in this thread. But I think we really need to get the details of the subdomain nailed down, since that seems to be the only part that's changing.