If you like DNray Forum, you can support it by - BTC: bc1qppjcl3c2cyjazy6lepmrv3fh6ke9mxs7zpfky0 , TRC20 and more...

 

Redirecting URLs with "?" and "%" in Apache

Started by semastr, Oct 05, 2024, 12:20 AM

Previous topic - Next topic

semastrTopic starter

Hey there, dev!

I'm stuck on crafting a redirect for this gnarly URL:

http://www.site.com/?t_link=pimp3174%3Aft%3Asmaxipp%3A%3A13%3A1%3Acodes

The goal is to seamlessly redirect it to:

https://site2.com

As I've dug into the issue, I've realized that the htaccess handler is being finicky and won't play nice with URLs containing question marks and percent signs. I've found separate solutions for each problem, but I need a silver bullet to tackle both at once.

Can you enlighten me on how to make this redirect work when the URL is packed with both "?" and "%" characters?
  •  


TonyMontac

You can use the B flag in your RewriteRule to enable backreference mapping, which will allow you to match and rewrite URLs containing percent signs. Next, to deal with the question mark, you'll need to use the QSA flag, which stands for Query String Append. This will allow you to preserve the query string during the redirect.

Here's a possible solution:

RewriteEngine On
RewriteCond %{QUERY_STRING} ^t_link=.*$ RewriteRule ^$ https://site2.com? [R=301,L,B,QSA]`

In this example, we're using a RewriteCond to check if the query string starts with t_link=, and if so, we're redirecting to https://site2.com with a 301 status code. The B flag enables backreference mapping, and QSA preserves the query string.

However, if you're still experiencing issues with the percent signs, you might need to use a more advanced approach, such as using a RewriteMap to decode the URL before applying the redirect.

Here's an alternative solution:

RewriteEngine On
RewriteMap unescape int:unescape RewriteCond %{QUERY_STRING} ^t_link=([^&]+)$
RewriteRule ^$ ${unescape:%1} [B]
RewriteRule ^$ https://site2.com? [R=301,L,QSA]

In this example, we're using a RewriteMap to decode the URL using the unescape function. We're then applying the redirect to the decoded URL.

As a third option, you could also consider using a more modern approach, such as using a server-side programming language like PHP or Python to handle the redirect. This would allow you to avoid the complexities of URL rewriting and encoding altogether.

For example, in PHP, you could use the following code to redirect the URL:

<?php $url $_SERVER['REQUEST_URI']; $parsed_url parse_url($url); $query_string $parsed_url['query']; 
if (
strpos($query_string't_link=')!== false) { header('Location: https://site2.com'); 
exit; } 
?>

This code uses the parse_url function to extract the query string from the URL, and then checks if it starts with t_link=. If so, it redirects to https://site2.com using the header function.
  •  

ZokEntinnyhok

When you're dealing with URLs, the question mark is a dead giveaway that there are params attached to the string. So, the first order of business is to sniff out whether there are any params present.
You can leverage QUERY_STRING or similar variables to get the job done. Now, when it comes to those pesky percentages, it's a whole different ball game. What you're looking at is a URL-encoded string, not actual percentages. That means you need to circle back to checking those QUERY params and then take the necessary actions based on what you find.
  •  

ecozyarrafe

Let's ditch the PHP approach for a more scalable solution. Instead, we can leverage URL rewriting to achieve the desired redirect. Here's a possible implementation:

We can craft a rewrite rule that captures the t_link parameter and redirects the user to site2.com if it matches the specified pattern. This approach is more efficient and flexible than relying on PHP conditionals.

RewriteCond %{QUERY_STRING} ^t_link=pimp3174:ft:smaxipp::13:1:codes$
RewriteRule ^(.*)$ https://site2.com/ [R=301,L]

Regarding the second part, you're correct that URL encoding is necessary to ensure the redirect works correctly. We can use URL encoding to convert the colon ( : ) and other special characters to their corresponding encoded values (e.g., %3A). This ensures that the redirect URL is properly formatted and avoids any potential issues.
  •  


If you like DNray forum, you can support it by - BTC: bc1qppjcl3c2cyjazy6lepmrv3fh6ke9mxs7zpfky0 , TRC20 and more...