WordPress.org

Support

Support » Plugins and Hacks » Redirection » Regex for simple redirection

Regex for simple redirection

  • I am trying to set up a redirection but cannot get the regex right.

    I have hundreds of 404’s for URL’s like

    /folder/anythinghere
    /folder/old-file.php
    /folder/not-here/gone

    All I want to do is to redirect them to

    /folder/

    with creating an infinite loop.

    I have tried redirecting with /folder/(.*) to /folder/ and several other variations but cannot get this to work without creating a loop.

    Does anyone have a simple regex to do this?

    https://wordpress.org/plugins/redirection/

Viewing 8 replies - 1 through 8 (of 8 total)
  • Use /folder/(.+) instead and you should be fine 😉

    .* means 0 or more characters
    .+ means 1 or more characters, so that should avoid the infinite loop

    Actually you don’t even need the brackets and you may want to anchor to the beginning of the url, so use the below instead:

    ^/folder/.+

    Should match:
    /folder/anythinghere
    /folder/old-file.php
    /folder/not-here/gone

    Will prevent matching on:
    /folder/
    /some/other/folder/
    /some/other/folder/file.ext

    The last comment almost worked for me, I currently intercept:

    /mantis(.*)

    What I am finding is that this does match:

    /mantis
    /mantis/
    /mantis/page

    … but not /mantis/page.php

    For some reason, the .php extension throws this error:

    “Internal Server Error

    The server encountered an internal error or misconfiguration and was unable to complete your request.

    Please contact the server administrator, root@localhost and inform them of the time the error occurred, and anything you might have done that may have caused the error.

    More information about this error may be available in the server error log.
    Apache/2.2.26 (******) Server at ************ Port 443″

    Any suggestions on how to prevent the .php extension causing this crash would be appreciated. Thanks.

    Strange, it should not do that. Can you try to see if this works better ?

    /mantis(.*)(\.php)?$

    Unfortunately, the result is the same: 500 Internal Server Error

    What I’ve found is that if “.php” is simply present, then the 500 error will occur.

    I also tried your suggestion without the $ symbol and it didn’t help either.

    This is the URL I’m trying:
    https://mydomain.org/mantis/main_page.php
    http://mydomain.org/mantis/view.php?id=1019

    Hang on… you are using the * again… causing you to get into the infinite redirect loop….

    I’m sorry, but I didn’t understand the last comment.

    The regex I’m using is:
    /mantis(.*)(\.php)?$

    There is only one * in there. So the 500 error is an infinite redirect loop?

    Any other suggestions? (Your help is really appreciated)

    I believe it might be.

    You’re probably better off adding a rule for this to your .htaccess file instead of using the redirect plugin.

    Are you familiar with editing .htaccess ?

Viewing 8 replies - 1 through 8 (of 8 total)
  • The topic ‘Regex for simple redirection’ is closed to new replies.
Skip to toolbar