Mod Rewrite - WP url &preview_nonce=(.*) - php

I would like to change any url containing the query string &preview_nonce= with any value passed to preview_nonce.
I understand that I have to rewrite based on the condition:
RewriteCond %{QUERY_STRING} ^preview_nonce=(.*)$
But I'm getting caught up only removing that from the query string and leaving everything else, including query strings.
Example URLs:
about-us/history/commitment/?preview=true&preview_id=9999&preview_nonce=9x323k1
stories/companies/?preview=true&preview_id=8888&preview_nonce=c448s88
The desired results:
about-us/history/commitment/?preview=true&preview_id=9999
stories/companies/?preview=true&preview_id=8888

You can use this rule for query string removal:
RewriteCond %{QUERY_STRING} ^(.+?&)?preview_nonce=[^&]*(?:&(.*))?$ [NC]
RewriteRule ^ %{REQUEST_URI}?%1%2 [L,NC,R=302]
Make sure this rule is placed before other WP rules.

Related

Redirect rule to php query but remove any query string in the requested url

My url is being redirected using .htaccess as follows:
RewriteRule ^b/([^/]+)/([^/]+)? b/view.php?id=$2&name=$1
Friendly url -> translates to php url
domain.com/b/hello/2 -> b/view.php?id=2&name=hello
BUT when someone comes to the site as follows:
domain.com/b/hello/2?query=xyz
I don't know how to get rid of the ?query=xyz
I have tried everything including [QSD] and I can't seem to get it to work.
Update
I have managed to get it to work with the following but it does two 301 redirects instead of one:
RewriteCond %{THE_REQUEST} \?[^\ ]+
RewriteRule ^b/(.*)$ /x/$1? [R=301,L]
RewriteRule ^b/([^/]+)/([^/]+)/([^/]+)?$ b/view.php?id=$2&&name=$1
Check if adding the question mark at the end of the rule will cancel appending query string from left side
RewriteRule ^b/([^/]+)/([^/]+)? b/view.php?id=$2&name=$1? [QSD,L]
You can use an additional Rule to catch for GET parameters and strip them off.
RewriteCond %{QUERY_STRING} .+
RewriteRule ^(.*)$ /$1? [R=301,L]
RewriteRule ^b/([^/]+)/([^/]+)? b/view.php?id=$2&name=$1
To make it only work on the /b/ subfolder, use this:
RewriteCond %{QUERY_STRING} .+
RewriteRule ^b/(.*)$ b/$1? [R=301,L]
RewriteRule ^b/([^/]+)/([^/]+)? b/view.php?id=$2&name=$1
The first rule will redirect everything that matches your rule to the URL without any GET parameters (note the ? at the end of the rewrite rule, it will strip off the parameters).
The second rule will match in the case the first rule cannot be applied, i.e., when there are no paramaters

internal server error with htaccess rewrite rule

I'm trying to take a URL with multiple querystring parameters into a folder structure URL and am getting a 500 internal server error.
My incoming URL looks like
www.fivestarprofessional.com/ag?PY=16&PF=wm&MKT=Delaware
My destination URL I need written as
delaware.fivestarprofessional.com/16/wm/index.html
I created 3 RewriteCond statements to capture the parameters and a RewriteRule to create the output
RewriteCond %{QUERY_STRING} !^(PY=.*)$ [NC]
RewriteCond %{QUERY_STRING} !^(PF=.*)$ [NC]
RewriteCond %{QUERY_STRING} !^(MKT=.*)$ [NC]
RewriteRule %3.fivestarprofessional.com/%1/%2/index.html
Any assistance would be greatly appreciated.
You can use this rule for your redirection:
RewriteCond %{QUERY_STRING} (?:^|&)PY=([^&]+) [NC]
RewriteCond %1::%{QUERY_STRING} (.*?)::(?:|.*&)PF=([^&]+) [NC]
RewriteCond %1/%2::%{QUERY_STRING} (.*?)::(?:|.*&)MKT=([^&]+)
RewriteRule ^ag/?$ http://%2.fivestarprofessional.com/%1/index.html? [L,NC,NE,R=302]
% variables are captured only from the most recent condition using same %{QUERY_STRING}
Try this one:
RewriteCond "%{QUERY_STRING}" "^PY=([^&]+).*&PF=([^&]+).*&MKT=([^&]+)" [NC]
RewriteRule "www.fivestarprofessional.com/ag" "%3.fivestarprofessional.com/%1/%2/index.html"
The following issues have been corrected:
RewriteRule takes 3 arguments instead of 2.
Arguments to RewriteCond hadn't been enclosed in quotes.
The backreferences into the RewriteCond rules ...
... only refer to the last condition matched (pointed out by #anubhava, being documented in the apache httpd docs). Therefore, the rules need to be collapsed in one.
... referred to the parameter name plus the complete remainder of the query string
... would have matched against query string starting with the respective ul parameter due to the use of the ^ anchor.
... would have been empty anyway, as the rules fired when the patterns did not match ( use of ! prefix )
Recommended reading is the proper section of the apache httpd documentation.
The solution as it stands assumes that...
the query string starts with the url parameter PY
the order in which the url parameters PY, PF, MKT appear in the query string is fixed.

Mod rewrite issue redirect with post

In fact I am working on a small php script, and now I am struggling with doing redirect using mod-rewrite.
What i want is to redirect
www.xx.com/motivational/api.php?latest=1
to
www.xx.com/api.php?latest=1&app=motivational
I tried this but it doesn't work:
RewriteRule ^/motivational/api\.php$ /api.php?latest=&%{QUERY_STRING}
%{QUERY_STRING} represents the entire query string, in your case latest=1. So when you append it to ...?latest= in your substitution string the result is ...?latest=latest=1 which is not what you want.
Change your rule to
RewriteRule ^/motivational/api\.php$ /api.php?%{QUERY_STRING}&app=motivational
and you should be fine.
Or you could do:
RewriteRule ^/motivational/api\.php$ /api.php?app=motivational [QSA]
The QSA flag means to append the new query string to the old, rather than to replace it, so your latest variable will not be lost.
You can not match against query strings in a RewriteRule, You will need a RewriteCond to match query strings in url :
RewriteEngine on
RewriteCond %{THE_REQUEST} /([^/]+)/api\.php\?latest=1 [NC]
RewriteRule ^ /api.php?latest=1&app=%1 [NC,L,R]
%1 is part of the regex "([^/]+)" in RewriteCond, it contains dynmically captured path in the request line.

Apache rewrite for query string value not equal to a digit

I'm trying to determine the correct syntax for an Apache rewrite to send visitors to a different page if a query string value does not equal a certain format.
URL 1: http://www.foo-bar.xqx/?video=123 (route one way)
URL 2: http://www.foo-bar.xqx/?video=fgh (route differently)
The format for the value should be all digits \d{0,x} but nothing I've tried so far seems to be returning as I'm expecting. The most current version for the rewrites is as follows:
RewriteCond %{QUERY_STRING} ^video=[^\d](\d{0,})$
RewriteRule - /404.php$1 [NC,L]
Use ! to negate a rule
RewriteCond %{QUERY_STRING} ^video=
RewriteCond %{QUERY_STRING} !^video=\d+$
RewriteRule - /404.php$1 [NC,L]
That will only rewrite requests that have ?video=, but only if there's no number after the =.

RewriteRule .htaccess is forwarding PHP Variable

I am trying to forward old posts on my site using .htaccess RewriteRule to new addresses.
First rule is
RewriteCond %{QUERY_STRING} id=16
RewriteRule ^current_article\.php$ /article/basics-of-electrosurgery [L,R=301]
which rewrites http://www.megadyne.com/current_article.php?id=16
to http://www.megadyne.com/article/basics-of-electrosurgery
Second rule is
RewriteRule ^article/([A-Za-z0-9-]+)/?$ articles.php?id=$1 [NC]
which rewrites http://www.megadyne.com/article/basics-of-electrosurgery to http://www.megadyne.com/articles.php?id=basics-of-electrosurgery
All works fine except the final result is http://www.megadyne.com/article/basics-of-electrosurgery?id=16 and I don't want it to append the final variable "?id=16" to the address.
You need a ? at the end of your first rule, and should probably add some boundaries to the query string match:
RewriteCond %{QUERY_STRING} (^|&)id=16($|&)
RewriteRule ^current_article\.php$ /article/basics-of-electrosurgery? [L,R=301]
When there's a query string in the request, it automatically gets appended to the end of the rule's target unless you are creating a query string in the target. So here, you're creating an empty query string, and without the QSA flag, the existing query string won't get appended.

Categories