301 htaccess with %20 in target URL - .htaccess

I need to manage some URL in query string. The landing URL contains space (%20).
I wrote this:
RewriteCond %{QUERY_STRING} ^orderby=([0-9]+)&pagenumber=([0-9]+)$
RewriteRule ^[OLD-SLUG]$ https://www.domain-site.it/Content/Images/uploaded/[TEXT-01]%20[TEXT-02]%20[TEXT-03].pdf? [R=301,L]
But the result is that the URL redirects to:
https://www.domain-site.it/Content/Images/uploaded/[TEXT-01]50[TEXT-02]50[TEXT-03].pdf
What's wrong?
Thanks all

%N, with (0 <= N <= 9), is the syntax for back references to the capture groups of the previous RewriteCond.
So %2 is referring to what you captured with pagenumber=([0-9]+).
You need to escape the percent character with a backslash, for it to be meant literal.
RewriteRule ^[OLD-SLUG]$ .../[TEXT-01]\%20[TEXT-02]\%20[TEXT-03].pdf? [R=301,L]

I've solved it in this way:
RewriteCond %{THE_REQUEST} ^(\S*)\s/(\S*) [NC]
RewriteCond %{QUERY_STRING} ^orderby=([0-9]+)&pagenumber=([0-9]+)$
RewriteRule ^old-slug$ https://www.domain-site.it/Content/Images/uploaded/[TEXT-01]\%20[TEXT-02]\%20[TEXT-03].pdf? [R=301,L,NE]
Tks all!

Related

How do I create dynamic friendly urls for a query string?

So currently I have URLs that looks like this:
http://localhost/?v=register
http://localhost/?v=profile&u=thatgerhard
I want the above to look like this:
http://localhost/register/ (without trailing /)
http://localhost/profile/thatgerhard/ (without trailing /)
I spent a ton of time trying to get this to work even though it seems like it should be a simple fix.
This is what I have atm:
RewriteBase /
RewriteEngine On
RewriteRule ^((.)*)$ /?v=$1&p=$
I would ideally like this to be dynamic so that if you do ?v=foo it will automatically do /foo/ without adding "foo" to the htaccess file.
Any help or direction will be greatly appreciated. :)
You should use RewriteCond Directive and RewriteCond backreferences. The %1 backreference matches the query string parameter v, the %2 backreference matches the query string parameter u. The RewriteRule redirects the request permanently and discard the query string entirely.
RewriteCond %{QUERY_STRING} ^v=([^&]*)$ [NC]
RewriteRule ^/?$ /%1/? [R=301,L]
RewriteCond %{QUERY_STRING} ^v=([^&]*)&u=([^&]*)$ [NC]
RewriteRule ^/?$ /%1/%2/? [R=301,L]

URL redirect with query strings - htaccess

This is really bugging me, I have followed a few tutorials but just can't get anywhere. I'm trying to do a 301 redirect from:
/webpage-mackay.php?wp=Mission
to:
http://domain.org.au/webpage.php?wp=Mackay%20Mission
I have attempted writing like this:
RewriteCond %{QUERY_STRING} ^wp=Mission$
RewriteRule ^/webpage-mackay\.php$ http://domain.org.au/webpage.php?wp=Mackay%20Mission [R=301,L]
and:
RewriteCond %{REQUEST_URI} ^/webpage-mackay.php$
RewriteCond %{QUERY_STRING} ^wp=Mission$
RewriteRule ^(.*)$ http://domain.org.au/webpage.php?wp=Mackay%20Mission [R=301,L]
But the result is:
http://domain.org.au/webpage.php?wp=Mission
Am I missing something? I have used this and this as a reference
I see 2 problems with your first attempt : there is no need for the leading slash in the RewriteRule, and the %20 doesn't work as "%" is a special character.
Here what you can try :
# Solution 1 : with a space character in the final URL
RewriteCond %{QUERY_STRING} ^wp=Mission$
RewriteRule ^webpage-mackay\.php$ http://domain.org.au/webpage.php?wp=Mackay\ Mission [R=301,L]
# Solution 2 : with the %20 in the final URL
RewriteCond %{QUERY_STRING} ^wp=Mission$
RewriteRule ^webpage-mackay\.php$ http://domain.org.au/webpage.php?wp=Mackay\%20Mission [R=301,L,NE]

Redirect htaccess php

I want to redirect this page with htaccess
products_filter.php?f16%5B0%5D=bla+bla+bla&cPath=72&M_ID=12x
to
products_filter.php?f16%5B0%5D=bla+bla&cPath=72&M_ID=12x
i tried this (and many other ways)
RewriteCond %{QUERY_STRING} ^f16%5B0%5D=bla+bla+bla$
RewriteRule ^products_filter\.php$ http://www.example.com/products_filter.php?f16%5B0%5D=bla+bla&cPath=72&M_ID=12x [L,R=301]
what am i doing wrong here?
Problem is using $ (end of input) in this regex:
RewriteCond %{QUERY_STRING} ^f16%5B0%5D=bla+bla+bla$
Since your query string is: 16%5B0%5D=bla+bla+bla&cPath=72&M_ID=12x
Change that line to:
RewriteCond %{QUERY_STRING} ^f16%5B0%5D=bla+bla+bla(&|$)
Update:
Looking at your question I realize that you are using quite a few special characters that need to be escaped.
RewriteCond %{QUERY_STRING} ^(f16%5B0%5D)=bla\+bla\+bla(?:&(.*)|$) [NC]
RewriteRule ^(products_filter\.php)$ /$1?%1=bla+bla&%2 [L,R=301,NE]
PS: It is important to use NE flag here. Otherwise %5B and %5D will be further encoded by Apache.
Try this code :
RewriteCond %{QUERY_STRING} ^f16%5B0%5D=bla+bla+bla
RewriteRule ^products_filter\.php$ http://www.example.com/products_filter.php?f16%5B0%5D=bla+bla&cPath=72&M_ID=12x [L,R=301]

Rewrite rule url character replace

How can i replace + to - in my url. What code shoud I add in my htacces to get rid of + and replace it with a minus.
RewriteCond %{QUERY_STRING} ^search=([^&]+)$
RewriteRule ^ http://mysite.com\/Download\/free\/%2.html? [R,L,NE]
If i type a search that contain spaces, every space become + and i want -.
I'm assuming that the bla+bla+bla part of the URL: http://mysite.com/Download/free/bla+bla+bla.html originated from the query string search='s value. Depending on what other rules you may have, you can go about this in 2 different ways. You can either remove all of the spaces from the query string first, before it's redirected to the .html file. Or you can rewrite the query string into the URI, then remove the spaces before redirecting. It'll be something like this:
RewriteCond %{QUERY_STRING} ^search=(.*?)(\+|%20)(.*)$
RewriteRule ^ /?search=%1-%3 [L,NE]
RewriteCond %{QUERY_STRING} !(\+|%20)
RewriteCond %{QUERY_STRING} ^search=([^&]+)$
RewriteRule ^ http://mysite.com\/Download\/free\/%1.html? [R,L,NE]
Note that in your htaccess you have the %2 back reference, which doesn't seem to reference anything.
Or rewrite to URI first, then redirect:
RewriteCond %{QUERY_STRING} ^search=([^&]+)$
RewriteRule ^ /Download\/free\/%1.html? [L,NE]
RewriteCond %{REQUEST_URI} !(\ )
RewriteRule ^ - [L,R]
RewriteRule ^(.*)\ (.*)$ /$1-$2 [L]

replace character in query string via .htaccess

My client wants a query string munged (by changing % to A) on certain pages.
For example, I can remove the query string completely on the desired pages via:
RewriteCond %{QUERY_STRING} !=""
RewriteCond %{REQUEST_URI} ^/SpecialPage(.*)
RewriteRule ^(.*)$ /$1? [R=301,L] #remove query string
Here's what I thought should remove % on the query string and replace with A but it's not:
RewriteCond %{QUERY_STRING} ^(.*)\%(.*)$
RewriteCond %{REQUEST_URI} ^/SpecialPage(.*)
RewriteRule ^(.*)$ /$1?%1A%2 [L]
What am I doing wrong in this? I just can't quite spot it. Thanks for the expert eyes!
You're real close.
The problem here is that you've got a condition and the match of your rule should be together. Your backreference to the previous RewriteCond is broken because it's for the REQUEST_URI and not the QUERY_STRING like you want.
RewriteCond %{REQUEST_URI} ^/SpecialPage(.*)
RewriteRule ^(.*)$ /$1?%1A%2 [L]
Here, the %1 backreference matches the (.*) at the end of the /SpecialPage URI. The backreferences from your query string match gets lost, and that's the ones you really want. You can combine the condition to match the REQUEST_URI with the regular expression pattern in the RewriteRule:
RewriteCond %{QUERY_STRING} ^(.*)\%(.*)$
RewriteRule ^SpecialPage(.*)$ /SpecialPage$1?%1A%2 [L]
Here, the %1 and %2 backreferences correctly reference the query string and the SpecialPage condition in the URI is met by the regex pattern.
Redirect Query String
RewriteEngine On
RewriteCond %{QUERY_STRING} ^(.)=(.)$
RewriteRule ^(.)?(.)$ /$1--%0? [R=301,L]
From Url: http://localhost/sholay-slide.jsp?slide=2
To Url: http://localhost/sholay-slide.jsp--slide=2

Resources