RewriteRule not matching regex correctly?

I have the following RewriteRule in my .htaccess file.

RewriteRule ^news/[a-z]+/([-a-z0-9]+)/$ http://localhost/story/$1/ [R=301]

Its purpose is to send requests for

http://localhost/news/category/story-title/

to http://localhost/story/story-title/

And it works fine for that purpose, but the problem is that if I have a link to http://localhost/news/category/ that link is now broken, because it’s also trying to do a redirect on that.

I’ve tested the above regex in several testing tools, and according to all of them, it should not be seeing news/category/ as a match, and yet on the server that’s exactly what it seems to be doing. What can I do to fix this?

Yeah that really shouldn’t match. Are you sure it isn’t some other rule being fired? Can you post the complete .htaccess please?

Options +FollowSymLinks
<FilesMatch “\.(html|htm|js|css)$”>
FileETag None
<IfModule mod_headers.c>
Header unset ETag
Header set Cache-Control “max-age=0, no-cache, no-store, must-revalidate”
Header set Pragma “no-cache”
Header set Expires “Wed, 11 Jan 1984 05:00:00 GMT”
</IfModule>
</FilesMatch>

RewriteEngine On

RewriteRule ^news/[a-z]+/([-a-z0-9]+)/$ http://localhost/story/$1/ [R=301]

RewriteRule ^story/([-a-z0-9]+)/$ /news.php [L]

ErrorDocument 404 /404.html
ErrorDocument 500 /404.html
ErrorDocument 400 /404.html
ErrorDocument 401 /404.html
ErrorDocument 403 /404.html

<Files 403.shtml>
order allow,deny
allow from all
</Files>

And the category itself doesn’t contain a slash? Like “food/drink” ?

Correct, they contain only letters a-z and hyphens.

I’ve found that this actually works:

RewriteRule ^news/[a-z]+/([-a-z0-9]+)/$ http://localhost/story/$1/ [R=301]
RewriteRule ^story/[-a-z0-9]+/$ /news.php [L]
RewriteRule ^news/./.$ /news.php [L]

but I hate having to use .* like that

Oh, wait, that file is called news.php …

It may be that you have that stupid idiotic MultiViews option enabled (as you can tell, I’m not a fan…)

Try adding Options -MultiViews at the top of your .htaccess

Rémon,

No comment on the <IfModule> block (and it’s repeated tests for every @#$%^ request?

rst01,

Wait a minute! You’re redirecting from news/././ to story/./ to news.php AND from news/./.* to news.php? Why the two step process you begin with? Why the “catch all” afterward?

Also, if your

Sorry, that’s not what “[a-z]+” says - you’re missing a hyphen at the start of the range definition (which you do have correctly for the story-title).

Back to your original question, though: I believe that Rémon has hit on the likely culprit.

Regards,

DK

I was going to get to that when everything worked as it should :slight_smile:

Rémon,

:tup: Yeah, sorry, a pet peeve of mine (as you’re well aware).

Regards,

DK