0

I am new in mod_rewrite regular expressions. I have the following lines in my .htaccess file:

RewriteRule ^gallery/([0-9]+)/ gallery.php?id=$1 [L]
RewriteRule ^gallery/ gallery.php [L]

When I open http://127.0.0.1/gallery/ everything is OK, but when I open http://127.0.0.1/gallery/some_string it opens the same page. How can I force such invalid URL-s to go to my 404 page. I need the second line to be strict - with no extra characters after the last slash and the first rule must continue to work with integer values.

Any ideas how to achieve this?

4

1 回答 1

0
RewriteRule ^gallery/ gallery.php [L]

在此规则中,模式^gallery/匹配所有以“gallery/”开头的字符串,包括“gallery/some_string”。为了使其严格,以确保它只匹配“gallery/”而不匹配其他内容,请使用 $ 元字符,它表示字符串的结尾:

RewriteRule ^gallery/$ gallery.php [L]

(您可能希望通过使用 ? 运算符 ( RewriteRule ^gallery/?$ gallery.php [L]) 使最后一个斜杠成为可选,以确保http://example.com/gallery也可以工作——用户期望这种行为,即使它很草率。)

于 2012-08-26T16:39:26.747 回答