Welcome to WebmasterWorld Guest from 54.166.114.43

Forum Moderators: Ocean10000 & incrediBILL & phranque

Message Too Old, No Replies

Complex redirect

Redirect dynamic to dynamic url

   
10:56 pm on Jun 12, 2010 (gmt 0)



Hi.
I want to redirect a URL scheme like this:
www.test.com/convert.php?from=var1&to=var2&page=var3
to something like this:
www.test.com/convert-var1-to-var2.php?page=var3

If you visit cnet.com you can see an example of what i want. For instance, look this page: [download.cnet.com ]

Could you help me? Thanks in advance.
7:03 am on Jun 13, 2010 (gmt 0)

WebmasterWorld Senior Member g1smd is a WebmasterWorld Top Contributor of All Time 10+ Year Member Top Contributors Of The Month



The topic of redirecting comes up every day here. In this case you'll need two lines of code (a RewriteCond looking at QUERY_STRING and a RewriteRule with the [R=301,L] flags), and there are several thousand similar prior examples here in this forum.

What have you tried so far?
1:05 pm on Jun 14, 2010 (gmt 0)



This:
Options +FollowSymLinks
RewriteEngine on
RewriteRule convert-(.*)-to-(.*).php/?page=(.*)$ convert.php?from=$1&to=$2&page=$3 [L]
6:29 pm on Jun 14, 2010 (gmt 0)

WebmasterWorld Senior Member g1smd is a WebmasterWorld Top Contributor of All Time 10+ Year Member Top Contributors Of The Month



Yes, as above you'll need a preceding
RewriteCond
looking at
QUERY_STRING
because
RewriteRule
only sees the path part of the requested URL.

The
(.*)
pattern is greedy and ambiguous. It will massively slow your server down as it needs hundreds of "back off and retry" attempts to find a match. You need to find a better pattern like
([^-]+)
and
([^\.]+)
or similar.

So, you'll get the "from" and "to" in
$1
and
$2
and the "page" as %1 (%1 not $1) from the preceding
RewriteCond
looking at the
QUERY_STRING
pattern.
12:12 am on Jun 15, 2010 (gmt 0)



Ufff. I could't find an example and i don't know too much about redirection, but i tried this:
RewriteCond %{QUERY_STRING} page=([0-9]+)
RewriteRule convert-(.*)-to-(.*).php/?page=([0-9]+)$ convert.php?from=$1&to=$2&page=%1 [L]

...And it didn't work.
1:57 am on Jun 15, 2010 (gmt 0)

WebmasterWorld Senior Member jdmorgan is a WebmasterWorld Top Contributor of All Time 10+ Year Member



Close, but needs some tweaking for correctness and efficiency:

RewriteCond %{QUERY_STRING} ^([^&]*&)*page=([0-9]+)(&.+)?$
RewriteRule convert-([^-]+)-to-([^-]+)\.php$ convert.php?from=$1&to=$2&page=%2 [L]

As this code illustrates, the RewriteRule looks only at the requested URL-path, and does not 'see' any query string data appended to the URL.

The fancy stuff in the RewriteCond pattern allows for unambiguous and efficient 'extraction' of the "page=" value no matter what order the requested query string parameters are in, or how many there are of them. For example, the original pattern would match on the first "page=" in "foo=bar&newpage=10&page=11" and get the wrong page number, while this new one won't be fooled by that "newpage=" because it requires an ampersand to precede "page=" if *anything* precedes "page=".

The more-specific negative-match patterns in the RewriteRule prevent dozens/hundreds/thousands of unnecessary back-off-and-retry matching attempts otherwise caused by multiple-".*" subpatterns. These latter should be avoided whenever possible, as they are extremely inefficient and a very likely cause of many otherwise-unnecessary server upgrades...

Jim
2:45 am on Jun 15, 2010 (gmt 0)



Wow. Thank you very much. It worked.
And what if i add another variable?
Would be something like this?:
RewriteCond %{QUERY_STRING} ^([^&]*&)*page=([0-9]+)(&.+)?$
RewriteCond %{QUERY_STRING} ^([^&]*&)*var2=([^-]+)(&.+)?$
RewriteRule convert-([^-]+)-to-([^-]+)\.php$ convert.php?from=$1&to=$2&page=%2&var2=%3 [L]

[edited by: tedster at 3:24 am (utc) on Jun 15, 2010]
[edit reason] Disable graphic smile faces [/edit]

7:22 am on Jun 15, 2010 (gmt 0)

WebmasterWorld Senior Member g1smd is a WebmasterWorld Top Contributor of All Time 10+ Year Member Top Contributors Of The Month



For multiple parameters, you need to "carry over" the back-reference from the first RewriteCond to the next RewriteCond otherwise the value is lost.

See the code beginning with the comment
# Redirect two-parameter-based index.php¦html? or / URL request in post 3752708 in the thread at [webmasterworld.com...] here.
4:48 pm on Jun 15, 2010 (gmt 0)

10+ Year Member



I have a similar question, I want to redirect an old domain with PHP URL variables oldsite.com?var1=1&var2=2, to one domain www.newsite.com

I currently have this:
Options +FollowSymLinks
RewriteEngine on
RewriteCond %{HTTP_HOST} ^www.oldiste.com$
RewriteRule ^(.*)$ www.newsite.com/ [R=301,L]

RewriteCond %{HTTP_HOST} ^oldsite.com$
RewriteRule ^(.*)$ www.newsite.com/ [R=301,L]

But it seems the URL variables are being passed with the redirect... I don't want the variable to pass. Any suggestions?
6:19 pm on Jun 15, 2010 (gmt 0)

WebmasterWorld Senior Member g1smd is a WebmasterWorld Top Contributor of All Time 10+ Year Member Top Contributors Of The Month



Yes, where .net is the old site and .com is the new site, you can try

Options +FollowSymLinks
RewriteEngine on
RewriteCond %{HTTP_HOST} ^(www\.)?example\.net
RewriteRule (.*) http://www.example.com/$1[b]?[/b] [R=301,L]


on the old hosting, or

Options +FollowSymLinks
RewriteEngine on
RewriteCond %{HTTP_HOST} !^(www\.example\.com)?$
RewriteRule (.*) http://www.example.com/$1[b]?[/b] [R=301,L]


on the new hosting, with all domains pointing at the new hosting.
10:26 pm on Jun 15, 2010 (gmt 0)

WebmasterWorld Senior Member jdmorgan is a WebmasterWorld Top Contributor of All Time 10+ Year Member



jreyest,

Only the back-references created in the last-matched RewriteCond are available. Those created by previous RewriteConds are over-written.

This will work if both variables are always present:

RewriteCond %{QUERY_STRING} ^([^&]*&)*page=([0-9]+)(&.+)?$
RewriteCond %2>%{QUERY_STRING} ^([^>]+)>([^&]*&)*var2=([^-]+)(&.+)?$
RewriteRule convert-([^-]+)-to-([^-]+)\.php$ convert.php?from=$1&to=$2&page=%1&var2=%3 [L]

Otherwise, you'll need three rules to handle page present/var2 not present, var2 present/page not present, and both present.

Jim
1:12 am on Jun 17, 2010 (gmt 0)



Aja. What means
%2>%
?
2:14 pm on Jun 17, 2010 (gmt 0)

WebmasterWorld Senior Member jdmorgan is a WebmasterWorld Top Contributor of All Time 10+ Year Member



It means "grab" the value of %2 from the previous RewriteCond and insert it ahead of the %{QUERY_STRING} value, delimited by an arbitrary but 'rare' character to make it easy to parse the two variables using the second RewriteCond's pattern.

This is one way to 'carry forward' the results of a previously-matched RewriteCond, which otherwise would be overwritten by the match in the current RewriteCond.

Jim
3:27 am on Jun 18, 2010 (gmt 0)



OK. Thanks!
 

Featured Threads

Hot Threads This Week

Hot Threads This Month