I have a Wordpress site and I'd like to redirect traffic based on specific criteria. I'm using a redirect plugin that has a Source URL (which can be a regex) and a Destination URL that can contain the result from the regex such as $1.
/blog/{slug} should redirect to /{slug} but only when {slug} is not ?page= or page/{anything}
So /blog/the-article-name will redirect to /the-article-name but /blog/?page=3 will not redirect and /blog/page/3 will not redirect.
Also, /blog/category/{slug} should redirect to /{slug}.
Thanks
I was able to figure it out after some trial and error:
^/blog\/(?!.*(page\/.*|\?paged=))+(.+)
I can access the 2nd match (the slug) using /$2
https://example.com/blog/the-article-slug will match for group 2 and redirect to https://example.com/the-article-slug
https://example.com/blog/page/2 will not match for group 2
https://example.com/blog/?paged=2 will not match for group 2
I'm using another redirect rule, ^/blog/category/(.*) to match the slug which I can access using /$1
https://example.com/blog/category/the-article-slug will match for group 1 and redirect to https://example.com/the-article-slug
Hope this helps someone else
Related
Hey i am trying to create a redirect map but the links i am trying to redirect are generated and just the ID number at the end is what matters.
I would like to know how can i redirect based on skipping everything in front of the last hyphen "-" in the url.
URL that i am trying to redirect:
http://staging.mysite.com/discover/en/suppliers/TD-Machi-LC-363868
but can be any possible combination:
http://staging.mysite.com/discover/en/suppliers/TD-Machining-101-LCC-363868
http://staging.mysite.com/discover/en/suppliers/Ted-Baker-D-363868
http://staging.mysite.com/discover/en/suppliers/363868
My current redirect that i was trying to get to work is:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteMap discovertxt txt:/opt/bitnami/apps/wordpress/htdocs/maps/discover_all_map.txt
RewriteMap discoverdestinationtxt txt:/opt/bitnami/apps/wordpress/htdocs/maps/discover_destination_map.txt
RewriteMap discoversourcetxt txt:/opt/bitnami/apps/wordpress/htdocs/maps/discover_source_map.txt
# Discover Redirect
# RewriteCond ${discoverdestinationtxt:$1} >${discoversourcetxt:$1} [NC]
# Trying to capture only the last value of the string
RewriteRule ^/discover/en/suppliers/^(.*)-(.*)-(.*)-(.*) /manufacturer/${discovertxt:$4}/ [R=301,L]
# Bellow method works only if ID alone is supplied.
# RewriteRule ^/discover/en/suppliers/(.*) /manufacturer/${discovertxt:$1}/ [R=301,L]
</IfModule>
Answer for how to search for exact ID match within string:
RewriteRule ^/discover/en/suppliers/(.*)([0-9]{6}) /manufacturer/${discover:$2}/ [R=301]
Is it possible to redirect multiple urls by keeping a part of the url or actually removing a part?
For example if I have a site with city guides for multiple cities with the url loremipsum.com/guide-seattle and I change all the urls to loremipsum.com/seattle
So I would want to create a redirect / rewrite rule with maybe regex saying if url is /guide-randomcityname/ then remove guide- and keep randomcityname and redirect to /randomcityname/
A general rule for whatever the city name could be.
Thanks
You can use this redirect rule to remove /guide- from start of your URLs:
RedirectMatch 301 ^/guide-([^/]+/?)$ /$1
I have an ecommerce store with the product URL format:
/categoryname/subcat1name/subcat2name/1450--my-widget
I will shorten it to:
/1450--my-widget
I can do the change within the ecommerce software, but I need to set up a mod rewrite redirect for the old URLs.
To avoid matching URLs for categories, content pages, etc, as well as product URLs of the new format, I need to match on all these conditions:
Does not contain the string "/info/"
Contains a slash, followed by 1 or more characters, followed by another slash, followed by 1 or more digits, followed by "--", followed by 1 or more characters
What directive would work?
EDIT:
More examples of matching and non matching strings
Matches for old product url:
/a-category/this-category/333--my-widget
/some-cat/34--widgetname
Non matches:
/1918--widgetcategory/
/info/12--about-us
/quick-order
/login?back=my-account
/2050--my-widgetname
You can use this 301 redirect rule as your very first rule in site root .htaccess:
RedirectMatch 301 ^(?!.*/info/).*/[^/]+/([0-9]+--[^/]+)/?$ /$1
Hi I want to create a rule to remove the first directory on the url, see the example:
request url: http://www.example.com/San-Salvador/help
I want to redirect that to
Target url: http://www.example.com/help
the pattern is [base url][city name][directory] and I want to recreate it as this [base url][directory name]
Here is a general regex which should work:
^(http:\/\/www\.example\.com\/)(.*\/)(.*)
Each term in parentheses is a group which will potentially match an input string. For the input string:
http://www.example.com/San-Salvador/help
here are the matching groups:
http://www.example.com/
San-Salvador/
help
The groups you want to retain are the first and third ones, i.e. http://www.example.com/ and help to give you http://www.example.com/help
You can explore this regex here at Regex 101.
For the most part this is a rule that you can use to remove the city. However your code will need to handle what happens after the redirected URL is requested. Meaning what is displayed when this is called http://www.example.com/help
RewriteEngine on
RewriteRule ^(?:[^/]+)/([^/]+)/?$ /$1 [L]
This is my .htaccess regex:
RewriteRule ^index.php/news/([0-9][0-9][0-9])?/16$ /index.php/news/$1/16 [L,R=301]
That works. But, I need the range 0-1879 to redirect to /index.php/news/$1/16 and more than 1879 to redirect to http://otherdomain/index.php/news/$1/16.
You shouldn't do this, but...
Here's how you match 0-1879:
[0-9]|[1-9][0-9]{1,2}|1[0-7][0-9]{2}|18[0-6][0-9]|187[0-9]
Here's how you match 1880-infinity
18[8-9][0-9]|19[0-9]{2}|[2-9][0-9]{3}|[1-9][0-9]{4}
Note: with the alternation, you'll need to wrap these in a group () to properly nest the logic.
Instead I recommend...
Just match 0+ digits and redirect to the other domain. If the number is 0-1879 (in your backend language, controller, etc.), then redirect them back to the original domain. Or figure out how to get the old news migrated over to the other domain, if your doing what I think you're doing.