how to write regular express for inserting at specific location? - regex

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/

Related

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.

Find duplicate slugs with 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

How to redirect "/%20rel="?

The following redirect isn't working, I assume it's because of the characters in the page name (generated by Wordpress).
redirect 301 /%20rel= http://www.NewSite.com/OldSiteLandingPage.php
How can I get this 301 redirect from "/%20rel=" to work?
Replace your rule with this:
Redirect 301 "/ rel=" /

301 Redirect rule to cover multple cases

Is there any way I can do all the following ( and similar ) redirects with a rule
Redirect 301 brand/brand1 http://domain.com/department/brand1
Redirect 301 brand/brand2 http://domain.com/department/brand2
Redirect 301 brand/brand3 http://domain.com/department/brand3
Yes you can use RedirectMatch that gives your regex capabilities like this:
RedirectMatch 301 brand/([^/]+) http://domain.com/department/$1

How to 301 redirect from example.com/word.html or word-abc.html or abc-word.html to example.com

how to 301 redirect from example.com/word.html or word-abc.html or abc-word.html or url that contain "word" to example.com
Now I just do it manually with this simple code :
Redirect 301 /word.html example.com
Redirect 301 /word-abc.html example.com
Redirect 301 /abc-word.html example.com
I want do it automatically redirect from url that contain "word" to example.com
Thanks.
You should use RedirectMatch directive for its regex capabilities:
RedirectMatch 301 \bword\b http://example.com
This will redirect any UR that has word (with word boundaries) to http://example.com