• Resolved hylanddesign

    (@hylanddesign)


    I have this regex redirect that is working fine when there is a end slash, but it does not work if the url has no end slash. Google has logged 404 pages all with urls with no end slash but not the ones with an end slash.

    I have ignore end slash enabled as well.

    My source url is: ^\/course-schedule\/(.*)\/(.*)\/
    Target url: /product-category/$1/

    How to I amend my regex to work on both?

    Thanks for your help.

    The page I need help with: [log in to see the link]

Viewing 3 replies - 1 through 3 (of 3 total)
  • Plugin Author John Godley

    (@johnny5)

    You’ll need to modify the expression to match a trailing slash and no trailing slash. For example /?

    Note you don’t need to escape / with \/.

    Thread Starter hylanddesign

    (@hylanddesign)

    Thanks for getting back to me, but I don’t believe this is working or I am putting in in wrong? This now works for urls without a trailing slash but if they have a trailing slash it adds the end of the url to it as well.

    Source: ^/course-schedule/(.*)/(.*)/?
    Target: /product-category/$1/

    For example when it has a trailing slash its adding the last part of the url too:
    This: /course-schedule/professional-scrum-master-ii/psmii-070621122/
    Redirects to: /product-category/professional-scrum-master-ii/psmii-070621122

    Correct
    Where as this: /course-schedule/professional-scrum-master-ii/psmii-070621122
    Redirects to: /product-category/professional-scrum-master-ii/

    I’m rusty on my regex but I think the .* might be grabbing more than you expect. Instead of .* you may want [^/]+ which grabs at least one character that’s *not* a slash.

    Take a look at regexr.com – it’s a wonderful tool to mock up patterns and see how the matching engines work. Something like this seemed closer to what I think you want. (You’ll need to add \ before the slashes when using this tool and remove them before entering into Redirection.)

    /course-schedule/([^/]+)/([^/]+)/?

Viewing 3 replies - 1 through 3 (of 3 total)
  • The topic ‘Regex Redirect on urls with and without end slash’ is closed to new replies.