Rewrite rule url character replace - .htaccess

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]

Related

htaccess Redirect with URL Parameters

I'm trying to redirect all of my products from my old zen-cart store to my new shopify store.
RewriteCond %{QUERY_STRING} products_id=1408
RewriteRule ^ https://shop.xyz.com/collections/products/mno? [L,R=301]
RewriteCond %{QUERY_STRING} ^products_id=1282 [NC]
RewriteRule ^ https://shop.xyz.com/collections/products/abc? [L,R=301]
RewriteCond %{QUERY_STRING} ^products_id=12 [NC]
RewriteRule ^ https://shop.xyz.com/collections/products/xyz? [L,R=301]
However, I can't figure out why products_id 12 and 1282 both redirect to https://shop.xyz.com/collections/products/xyz?
You line
RewriteCond %{QUERY_STRING} products_id=12 [NC]
contains the expression: products_id=12 which indeed matches both 12 and 1282.
If it's a single parameter, you can fix it by adding the end of string token $: products_id=12$
Otherwise there will be a delimiter & after the products_id number.
https://example.com/shop/?products_id=12&cat_id=1
you could match it with:
products_id=12&
Another unorthodox but quick and dirty solution that may work is to sort the numbers down, the higher numbers will match correctly before hitting the lower number rules.

How to redirect 301 with dynamically generated URL while removing a slash?

I have been at this all day long, tried dozens of variations but can't quite seem to get this rewrite to work.
RewriteCond %{THE_REQUEST} /pwreset\.php\ [NC]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{THE_REQUEST} \s(.+?)/+[?\s]
RewriteRule ^(.+?)/$ https\:\/\/www\.example\.com\/support\/pwreset\.php [L]
The URL it returns is:
https://www.web-jive.com/support/pwreset.php/?key=cdc3b1aa842785f7345be501a30ddc83
What I need to be removed is the pwrest.php trailing slash before the question mark. Where am I going wrong on this?
The idea is to have the first URL below, redirect to the second:
https://example1.com/pwreset.php?key=cdc3b1aa842785f7345be501a30ddc83
https://example2.com/support/pwreset.php?key=cdc3b1aa842785f7345be501a30ddc83
EDIT
Per Mr. White's suggestion, I'm posting the whole .htaccess file.
RewriteEngine On
# Announcements
RewriteRule ^announcements/([0-9]+)/[a-z0-9_-]+\.html$ ./announcements.php?id=$1 [L,NC]
RewriteRule ^announcements$ ./announcements.php [L,NC]
# Downloads
RewriteRule ^downloads/([0-9]+)/([^/]*)$ ./downloads.php?action=displaycat&catid=$1 [L,NC]
RewriteRule ^downloads$ ./downloads.php [L,NC]
# Knowledgebase
RewriteRule ^knowledgebase/([0-9]+)/[a-z0-9_-]+\.html$ ./knowledgebase.php?action=displayarticle&id=$1 [L,NC]
RewriteRule ^knowledgebase/([0-9]+)/([^/]*)$ ./knowledgebase.php?action=displaycat&catid=$1 [L,NC]
RewriteRule ^knowledgebase$ ./knowledgebase.php [L,NC]
#Password reset
RewriteCond %{QUERY_STRING} ^key=[0-9a-f]{32}$
RewriteRule ^pwreset\.php$ https://www.web-jive.com/support%{REQUEST_URI} [R=302,L]
#Redirect to new support URL
RewriteCond %{HTTP_HOST} ^members\.web\-jive\.com$ [OR]
RewriteCond %{HTTP_HOST} ^www\.members\.web\-jive\.com$
RewriteRule ^/?$ "https\:\/\/www\.web\-jive\.com\/support" [R=301,L]
RewriteCond %{HTTP_HOST} ^members\.web\-jive\.com$ [OR]
RewriteCond %{HTTP_HOST} ^www\.members\.web\-jive\.com$
The output you are seeing (with the trailing slash on the URL-path) isn't the result of just the directives you posted, so maybe you have a conflict with other directives or you are seeing a cached response.
However, the rule you posted would seem to be far more complex than it needs to be.
1. https://example1.com/pwreset.php?key=cdc3b1aa842785f7345be501a30ddc83
2. https://example2.com/support/pwreset.php?key=cdc3b1aa842785f7345be501a30ddc83
Assumptions:
You don't need to match the key value; just the URL-path (ie. /pwreset.php)
example1.com and example2.com point to different places (the filesystem does not overlap).
To redirect from 1. to 2. try the following at the top of your .htaccess file in the root of example1.com:
RewriteEngine On
RewriteRule ^pwreset\.php$ https://example2.com/support%{REQUEST_URI} [R=302,L]
Any query string (eg. key=abc...) is passed through unaltered.
Note that this is a 302 (temporary) redirect. Only change it to a 301 (permanent) when you have confirmed it works OK.
If you need to check that a key= URL param is present and is set to a 32 hex string (which appears to be what your example represents) then include a condition before the above RewriteRule that checks against the QUERY_STRING server variable. For example:
RewriteCond %{QUERY_STRING} ^key=[0-9a-f]{32}$
RewriteRule ^pwreset\.php$ https://example2.com/support%{REQUEST_URI} [R=302,L]
If any other URL params are present on the request then the redirect will fail.
Aside:
RewriteRule ^(.+?)/$ https\:\/\/www\.example\.com\/support\/pwreset\.php [L]
This looks very cPanel-esque. There is no need to backslash colons, slashes and dots in the RewriteRule susbtitution argument. This is an "ordinary" string, not a regex. These characters have no special meaning here.

Cut off parameter from redirect in htaccess

This silent redirect in htaccess:
RewriteCond %{QUERY_STRING} ^pic_id=(\d+)$ [NC]
RewriteRule ^album_showpage\.php$ /gallery/image_page.php?image_id=%1 [L]
redirect a url that looks like:
example.com/album_showpage.php?pic_id=1906
to:
example.com/gallery/image_page.php?image_id=1906
that works great. But when the URL has a parameter like:
album_showpage.php?pic_id=1906&mode=prev
or
album_showpage.php?pic_id=1906&mode=next
the redirect wont work.
Question: How to cut of any parameter after pic_id=1906
thank you
You need match against the rest of the query string.
RewriteCond %{QUERY_STRING} ^pic_id=(\d+)(&.*)?$ [NC]
RewriteRule ^album_showpage\.php$ /gallery/image_page.php?image_id=%1%2 [L]
If you don't want the mode=prev stuff to be included in the rule's target, then you can simply remove the $ instead of attempting to match against it:
RewriteCond %{QUERY_STRING} ^pic_id=(\d+) [NC]
RewriteRule ^album_showpage\.php$ /gallery/image_page.php?image_id=%1 [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]

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