URL redirect with query strings - htaccess - .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]

Related

Htaccess - RewriteRule, how to keep only the value part of a query string

I'm trying to rewrite an url from:
https://www.website.com/test/test-detail?name=abc
to:
https://www.website.com/test/abc
I can't seem to find a way to keep only the value part of the query string.
This is the code:
RewriteEngine On
RewriteCond %{REQUEST_URI} ^/test/test-detail/
RewriteCond %{QUERY_STRING} name=
RewriteRule ^(test)/(test-detail)/$ /test/$3 [R=301,L]
For the input:
https://website.com/test/test-detail/?name=abc
The result is:
https://website.com/test/?name=abc
Link to the example.
You can use this :
RewriteEngine On
RewriteCond %{REQUEST_URI} ^/test/test-detail/
RewriteCond %{QUERY_STRING} name=(.+)$
RewriteRule ^(test)/(test-detail)/$ /test/%1? [R=301,L]
Make sure to clear your browser's cache before you test this.

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.

.htaccess - How to remove an end part of a url?

I would like to redirect the following urls from:
http://example.com/index.php/item123-detail?tmpl=component&format=pdf
to:
http://example.com/index.php/item123-detail
In essence removing the "?tmpl=component&format=pdf" from all urls.
I have tried multiple different examples from other Stack questions without luck so far. Any help would be much appreciated. Thank you.
This part of URL ?tmpl=component&format=pdf called QUERY_STRING and if you want to remove it from any request you could do several scenarios like putting this code at main directory .htaccess this :
RewriteEngine On
RewriteCond %{THE_REQUEST} \?
RewriteRule ^(.*)$ /$1? [L,R=301]
So , by the code above you will be able to remove even request with ? only.
If you want to match only this query string and keep others , let me know to give you another scenario with another condition RewriteCond %{QUERY_STRING}
Ok , to match only this query string , replace the code with :
RewriteEngine On
RewriteCond %{QUERY_STRING} ^tmpl=component&format=pdf$
RewriteRule ^(.*)$ /$1? [L,R=301]
And if tmpl & format values not fixed and come only into letters, replace the code with:
RewriteEngine On
RewriteCond %{QUERY_STRING} ^tmpl=([a-zA-Z]+)&format=([a-zA-Z]+)$
RewriteRule ^(.*)$ /$1? [L,R=301]
Solution found on another post as follows:
remove query string from end of url URL using .htaccess
RewriteCond %{QUERY_STRING} "post_type=" [NC]
RewriteRule (.*) /$1? [R=301,L]

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]

Resources