Project

General

Profile

[Solved] Rewrite regex bug? Not related to v1.4.40

Added by lonypny almost 8 years ago

I tested this on 1.4.35 and 1.4.41 (latest git with included REQUEST_URI fix).

url.rewrite-once=(
"^/files/(.*)/?$" => "/files.php?filename=$1",
"^/en/files/(.*)/?$" => "/files.php?filename=$1&lang=en"
)

I want it to match:
//example.com/files/something
//example.com/files/something/

and rewrite it to files.php?filename=something

//example.com/en/files/something
//example.com/en/files/something/

and rewrite it to files.php?filename=something&lang=en

The url without the trailing slash works but the one with the trailing slash does not.


Replies (4)

RE: Rewrite regex bug? Not related to v1.4.40 - Added by gstrauss almost 8 years ago

This is a not a bug. The '.*' is a greedy match and will match the final '/' instead of '/?' matching the final '/'

Effectively, you have written:

url.rewrite-once=(
"^/files/(.*)$" => "/files.php?filename=$1",
"^/en/files/(.*)$" => "/files.php?filename=$1&lang=en" 
)

If your specific 'something' does not contain any slashes (and must contain at least one char), then you can use '([^/]+)' instead of (.*)

url.rewrite-once=(
"^/files/([^/]+)/?$" => "/files.php?filename=$1",
"^/en/files/([^/]+)/?$" => "/files.php?filename=$1&lang=en" 
)

RE: Rewrite regex bug? Not related to v1.4.40 - Added by lonypny almost 8 years ago

My 'something' (slug) doesn't contain any forward slashes or backslashes, so I tried the rules you wrote. Unfortunately they don't work.

RE: Rewrite regex bug? Not related to v1.4.40 - Added by gstrauss almost 8 years ago

Since 'something' does not contain '/', you can try another alternative:

url.rewrite-once=(
"^/files/(.+?)/?$" => "/files.php?filename=$1",
"^/en/files/(.+?)/?$" => "/files.php?filename=$1&lang=en" 
)

I have interpreted the above as that you want to remove the trailing '/' if present. Please be much more descriptive of what "they don't work" actually means if that is not the case.

Another alternative is to simply remove the trailing '/' inside files.php.

RE: Rewrite regex bug? Not related to v1.4.40 - Added by lonypny almost 8 years ago

The link //example.com/files/something/ isn't matched.
//example.com//files/something is matched
//example.com/en/files/some-thing/ isnt matched.
//example.com/en/files/some-thing is matched.

files.php creates a canonical tag without the trailing slash, however users are linking to the files with the trailing slash which gets redirected to the main page as its not matched within these rules.

UPDATE:
The last rules you suggested worked. It seems my last test was cached. Thank you!

    (1-4/4)