Pass query value to new URL - regex - regex

I am trying to set up some server redirects.
I have an old URL: /product-category/pillows/?pa_position=back-sleeper
The new URL is: /product-category/pillows/?_position=back-sleeper
The ?_position parameter is new, however the values remain the same. Is there an appropriate regex expression to pass the original param value to the new URL?

As Barmar suggested you could simply replace pa_position with _position. Following sed command would do the job.
sed 's/pa_position/_position/'
If you want to capture the last part of the URL you can use the following regex:
\/product-category\/pillows\/\?pa_position=\(.*\)
The string 'back-sleeper' in this case will then be accessible as the first matched group (\1) of this regex.

Related

How to replace part of a URL with regex

I need to remove part of a URL with a regex.
From the words: http or https to the word .com.
And it can be several times in one string.
Can anyone help me with this?
For example a string:
"The request is:https://stackoverflow.com/questions"
After the removal - "The request is:/questions"
The regex that performed the deletion perfectly is: (#"\w+://[^/$]*")
with replace "".
Something like that:
var regex = new Regex(#"\w+:\/\/[^\/$]*");
regex.Replace(url, "");
You can use the re.sub() function from the regex package. Alternatively if your working with python you can use urlparse package to extract different parts of the url and concatenate it to the prefix you want.

Multi Taxonomy URL rewrite not wokring

I am trying to rewrite WP URL and here is the URL:
http://example.com/?job_listing_region=california&job_listing_category=wordpress
I want to change it as http://example.com/california/wordpress
I tried this:
add_rewrite_rule('([^/]*)/([^/]*)/?','job_listing_region=$matches[1]&job_listing_category=$matches[2]','top');
But its not working. Sorry I am not good at regex it might be a small one but I am not able to find a solution. Thanks in advance
Code
See regex in use here
Regex
\??\w+=([^&]+)&?
Replacement
$1/
Results
Input
http://example.com/?job_listing_region=california&job_listing_category=alcohol-abuse-programs
Output
http://example.com/california/alcohol-abuse-programs/
Explanation
Regex
\?? Match between zero and one of the ? character literally
\w+= Match any word character one or more times, followed by the = character literally (\w can be replaced with [a-zA-Z0-9_] if preferred/doesn't work in your regex flavour)
([^&]+) Capture into capture group 1 any character except the & character literally one or more times
&? Match between zero and one & character literally
Replacement
$1/ Matches the same text as most recently matched by the 1st capturing group, followed by a / literally
Using http://example.com/{job_listing_region}/{job_listing_category}/ is too broad - it would affect every single URL on your website, such as /wp-admin.
I'd recommend using http://example.com/jobs/{job_listing_region}/{job_listing_category}/ as your URL structure, in which case the rewrite rule would be set as follows:
add_rewrite_rule('^jobs/([^/]*)/([^/]*)/?','index.php?page_id=1234&job_listing_region=$matches[1]&job_listing_region=$matches[2]','top');
page_id should be set to the page ID of the page/post you'd like to route this to.
It's important to note that the rewrite might not be available until you view/save the Settings -> Permalinks page in the back end.
Thanks for the above answer, they helped me to get an solution finally.
So while passing url strings to wordpress we need to register the variables in functions.php and then instead of using php get we need to use wordpress var queries to get the urls.
As suggested by #athms above, I changed url structure.
Now "wordpress" is a wordpress page in which the query variables are captured.
So my URL is http://example.com/wordpress/?job_listing_region=california
In functions.php I registered these variables:
function custom_query_vars_filter($vars) {
$vars[] = 'job_listing_region';
return $vars;
}
add_filter( 'query_vars', 'custom_query_vars_filter' );
function custom_rewrite_tag1() {
add_rewrite_tag('%job_listing_region%', '([^&]+)');
}
add_action('init', 'custom_rewrite_tag1', 10, 0);
Rewrite Rule in functions.php:
function custom_rewrite_rule3() {
add_rewrite_rule('^wordpress/([^/]*)/?','index.php?page_id=35349&state=$matches[1]','top');
}
add_action('init', 'custom_rewrite_rule3', 10, 0);
Here page id is the id of page I created i.e "wordpress"
And in the page template for "wordpress" I captured the region using:
$region = get_query_var('job_listing_region');
Now you can pass this variable to your query.
So now you can start using this pretty URL:
http://example.com/wordpress/california
The end of URL california is taken as query string and can be used in our template.

How to match 2 query strings from one URL for Wordpress Redirection Plugin

I want to use the 'Redirection' plugin for WordPress to redirect search URLS to a cleaner URL.
The Redirection plugin's source URL accepts regular expressions.
ONE EXAMPLE OF WHAT I NEED:
I need this search URL:
http://www.danceclass.es/?s=&where=Sydney+%28Australia%29&company_category=pole&cat=pole&search_simple=STEP+3+-+Show+details
to redirect to:
http://www.danceclass.es/pole-sydney
but only if where=Sydney AND cat=pole.
Can a Regular Expressions expert please translate this example into regex code for me?
I'll rinse and repeat for different where= AND cat=
Since you need to extract the values of those 2 query parameters, you'll need 2 capturing groups. However the regex also needs to handle different query parameter order i.e cat and where order can be different
Here is the regex
https://regex101.com/r/gP9pZ0/1
You can use captured groups to form your new url, an example using sed's substitute(s) command:
$sed -r 's#^.*where=([a-zA-Z]*).*cat=([^&]*).*|.*cat=([^&]*).*where=([a-zA-Z]*).*$#http://www\.danceclass\.es/\2\3-\1\4#'
http://www.danceclass.es/?s=&where=Sydney+%28Australia%29&company_category=pole&cat=pole&search_simple=STEP+3+-+Show+details
http://www.danceclass.es/pole-Sydney
http://www.danceclass.es/?s=&company_category=pole&cat=pole&search_simple=STEP+3+-+Show+details&where=Sydney+%28Australia%29
http://www.danceclass.es/pole-Sydney
$
Once you the your final URL, you can make the string lowercase which meets your answer. The 2 inputs given to sed have different order i.e 1st URL has where then cat, order is reverse for 2nd URL
No need to iterate over different values, you can match as per the valid chars mentioned in regex, i.e.:
cat=[^&] cat value will give you every char until it doesn't find &
where=[a-zA-Z] where value will give you every char if it is an uppercase/lowercase char

Simple regex to replace first part of URL

Given
http://localhost:3000/something
http://www.domainname.com/something
https://domainname.com/something
How do I select whatever is before the /something and replace it with staticpages?
The input URL is the result of a request.referer, but since you can't render request.referer (and I don't want a redirect_to), I'm trying to manually construct the appropriate template using controller/action where action is always the route, and I just need to replace the domain with the controller staticpages.
You could use a regex like this:
(https?://)(.*?)(/.*)
Working demo
As you can see in the Substitution section, you can use capturing group and concatenates the strings you want to generate the needed urls.
The idea of the regex is to capture the string before and after the domain and use \1 + staticpages + \3.
If you want to change the protocol to ftp, you could play with capturing group index and use this replacement string:
ftp://\2\3
So, you would have:
ftp://localhost:3000/something
ftp://www.domainname.com/something
ftp://domainname.com/something

Extract last part of url without query string or jsessionid

I want a regex that will always return the last part of an url before the query string parameters and without the jessionid if present.
Here's some url examples:
http://www.somesite.com/some/path/test.action;jsessionid=000063vCmvJAn7VWyymA_dPsHZs:16u9pglit?sort=2&param1=1&param2=2
http://www.somesite.com/some/path/test;jsessionid=000063vCmvJAn7VWyymA_dPsHZs:16u9pglit?sort=2&param1=1&param2=2
http://www.somesite.com/some/path/test.action?sort=2&param1=1&param2=2
http://www.somesite.com/some/path/test?sort=2&param1=1&param2=2
Here's my regex so far:
.*http://.*/some/path.*/(.*);?.*\?.*
It is working for the url that does not contain jsessionid, but will return test;jessionid=... if it is present.
To test: http://regex101.com/r/fM0mE2
I would use this regex:
.*http:\/\/.*\/some\/path.*\/([^;\?]+);?.*\?.*
^^^^^^
Basically matches anything that isn't ; or ?. And I think it might be shortened to:
.*http:\/\/.*\/some\/path.*\/([^;\?]+)