Find duplicate slugs with regex? - regex

I'm trying to use regex to highlight the circular redirects in these 301 redirects.
I'm using Sublime Text, and only need to highlight them, so I can manually edit.
The example below is from a much bigger file.
Redirect 301 /en/about/about /en/about
Redirect 301 /en/about/global /en/about/global
Redirect 301 /en/about/divisions /en/about/divisions
Redirect 301 /en/division /en/about/divisions
Can anyone help?

I'd first remove from capturing the 2 first strings and the spaces "Redirect 301 "
Then, capturing the next string to the space. Then capture the last one as backreference
^(?:.*\s){2}(.*)(?:\s)(\1)$
Output :
Redirect 301 /en/about/about /en/about // doesn't match
Redirect 301 /en/about/global /en/about/global // match
Redirect 301 /en/about/divisions /en/about/divisions // match
Redirect 301 /en/division /en/about/divisions // doesn't match
Regex101

Related

how to write regular express for inserting at specific location?

I have long file containing lines like :
Redirect 301 /abcdef/ /ab/page1
Redirect 301 /abcdefasdff/ /abaff/page2
I need to insert or replace the following place(refer to *) with my domain for all the lines
Redirect 301 /abcdef/ */ab/page1
Redirect 301 /abcdefasdff/ */abaff/page2
result:
Redirect 301 /abcdef/ https://exmaple.com/ab/page1
Redirect 301 /abcdefasdff/ https://example.com/abaff/page2
can I use editor find and replace function to do this ?
Simply search for "/ /" and replace it with "/ https://example.com/"
for missing first slash " /"
Find -> Redirect 301 /([a-zA-Z0-9-]*) /
Replace -> Redirect 301 /$1 https://www.example.com/

Redirect 301 using regexp in .htaccess file

I am looking for a Regex to redirect urls like:
example.net/example-1
example.net/example-2
example.net/example-3
I have many URLs that have the same prefix, but a different suffix after example-.
I was trying around with regex.
Redirect 301 /example-.*$ http://example.net/example-overview
Redirect 301 /example-(.*)? http://example.net/example-overview
Redirect 301 /example-(.+)[/]?$ http://example.net/example-overview
Redirect 301 /([^/example-]+) http://example.net/example-overview
I can't get it work.
Normal redirects for example example-1 to example-overview are working fine.
EDIT:
Found a solution:
RedirectMatch 301 ^/example-(.+)[/]?$ http://example.net/example-overview
Redirect doesn't support regular expressions, use RedirectMatch for that.

htaccess redirect 301 without a separator

End of the url have numbers without any separator
Given url like
example.com/productdetails/product-flowers123
needed to redirect
example.com/productdetails/product-flowers--123
can i do this with .htaccess
I already tried: RedirectMatch 301 ^/productdetails/([A-Za-z]+)$([0-9]+)$ /productdetails/$1--$2
But its not worked.
You can use this rule instead:
RedirectMatch 301 ^/(productdetails/.*?[a-zA-Z])([0-9]+)/?$ /$1-$2
Make sure to use a new browser or clear your browser cache before testing this change.

RedirectMatch: redirects to wrong URL

I switched a Shop-CMS and put a lot of old URL into the .htaccess to redirect old products to their new location.
But some redirects are wrong:
RedirectMatch 301 ^/products/catxy/313? https://www.example.com/products/catxy/product-1/
RedirectMatch 301 ^/products/catxy/314? https://www.example.com/products/catxy/product-2/
RedirectMatch 301 ^/products/catxy/319? https://www.example.com/products/catxy/product-3/
When I go to example.com/products/catxy/319 I get redirected to product-1 and not to product-3
As I understand the regex above it means starting with /products/catxy/319[MAYBEMORE] -> redirect to product-3
I can't write ^/products/catxy/319$ because there are a lot of different endings for 319 (all variations of that product id).
I don't know either if it would be better to use mod_rewrite in my situation.
Problem is presence of ? in the end of pattern: ^/products/catxy/313?, which is making last digit optional so your first rule matches anything that starts with:
/products/catxy/313
or
products/catxy/31
You probably means to keep trailing slash optional and have your rules like this:
RedirectMatch 301 ^/products/catxy/313(?:/.*)?$ https://www.example.com/products/catxy/product-1/
RedirectMatch 301 ^/products/catxy/314(?:/.*)?$ https://www.example.com/products/catxy/product-2/
RedirectMatch 301 ^/products/catxy/319(?:/.*)?$ https://www.example.com/products/catxy/product-3/
Remember to clear your browser cache before testing the changes.

Htaccess redirect wiout trailing slash is working but with it is not

I have a WordPress website. And I want to redirect a URL to another 3rd party domain. The issue is that destination domain cannot have trailing slash.
So I want to redirect http://www.domain.com/xyzxyzxyz to http://3rd-party-domain.com/?something
It's working fine for without trailing slash request urls
http://www.domain.com/xyzxyzxyz -> http://3rd-party-domain.com/?something
but when I try http://www.domain.com/xyzxyzxyz/ then it does not work because it adds trailing slash in destination URL too, which gives error.
I have tried these rewrite rules
Redirect 301 /xyzxyzxyz http://3rd-party-domain.com/?something
Redirect 301 /xyzxyzxyz/ http://3rd-party-domain.com/?something
Not working :(
Please let me know how to fix this.
Try RedirectMatch for its regex capabilities:
RedirectMatch 301 ^/xyzxyzxyz/?$ http://3rd-party-domain.com/?something