spatie / laravel-missing-page-redirector

Redirect missing pages in your Laravel application
https://freek.dev/598-retain-your-seo-worth-by-correctly-redirecting-missing-pages-in-a-laravel-application
MIT License
488 stars 38 forks source link

preg_replace(): Compilation failed: #48

Closed daikazu closed 5 years ago

daikazu commented 5 years ago

I'm getting the Exception error preg_replace(): Compilation failed: range out of order in character class at offset 6 no matter what my config redirects are. This results in a RedirectNotFound and my 404 page is shown.

example config

redirects' => [
        '/articles/' => '/blog'
    ],

After Digging around in the code I found that it's related to the line $redirectUrl = preg_replace('/\/{[\w-_]+}/', '', $redirectUrl); of the resolveRouterParameters function of the MissingPageRouter class. When I comment that line out everything works as expected. My regex knowledge is small and i'm not sure exactly what that line is suppose to do.

I couldn't find any previous issues that seem to have the same problem. Maybe someone will have some insight.

dominikb commented 5 years ago

Sadly, I was not able to reproduce the error with a fresh install of Laravel and the latest version 2.3.2 of this package. The regex you posted, however, does not match the regex in the repository, which has an extra dash and underscore: $redirectUrl = preg_replace('/\/{[\w-_]+}/', '', $redirectUrl); Have you modified the package source?

The error indicates that the syntax of the regular expression in use is invalid. I reproduced the error here by changing the \w-_ (which means any alphanumeric, underscore and dash) to w-_ which is an invalid character range.

daikazu commented 5 years ago

My bad, I was playing around with the code and didn't undo enough times before I copied and pasted. I edited my initial post to reflect the corrected line of code.

I was only able to see the warning by trapping it with a try catch.

The whole reason I started digging into this was that I cannot for the life of me get this to work with a clean install. I always get a 404 response unless I comment out that line

daikazu commented 5 years ago

Okay I figured it out so it turns out that there are some changes to the preg library used in PHP 7.3. (PHP :: Bug #77334)

Since \w matches any word character (equal to [a-zA-Z0-9_]) you could simplify the expression to just /\/{[\w-]+}/ and this should work the same and doesn't appear to error in PHP 7.3.

Example

freekmurze commented 5 years ago

Thanks for the report and the PR!