URL正则表达式 - 删除文件名和URL的结尾斜杠?斜杠、文件名、结尾、正则表达式

2023-09-02 10:03:24 作者:大陆马子,装港逼,

所以我有这样的正则表达式的问题,并想知道,如果有人可以帮助我?

So I have this regex problem and wondered if anyone could help me out?

如果用户访问 http://example.com/index.php/ 哪能修改/在末尾添加到我的正则表达式prevent斜线(S)?

If a user visits http://example.com/index.php/ how can i modify/add to my regex to prevent a trailing slash(s) at the end?

我现在有一个网页叫做post.php中,可以像这样 http://example.com访问/评论/ reviewTitle / 和 http://example.com/news/newsTitle/

I currently have a page, called post.php that can be accessed like so http://example.com/reviews/reviewTitle/ and http://example.com/news/newsTitle/

再次,我怎么可能prevent这个结尾的斜线?

again, how could I prevent this trailing slash?

下面是正则表达式我到目前为止有:

Below is the regex I have so far:

RewriteEngine on
RewriteRule ^reviews/([^/.]+)/?$ reviews/post.php?title=$1 [L]
RewriteRule ^news/([^/.]+)/?$ news/post.php?title=$1 [L]
RewriteRule ^page/(.*) index.php?page=$1

注:IM还重新写 http://example.com/index.php?page = 1 以 http://example.com/page/1 等,同样的问题,我怎么能prevent斜杠?

Note: Im also re-writing http://example.com/index.php?page=1 to http://example.com/page/1 etc, same question, how can I prevent a trailing slash?

非常感谢,我真的AP preciate任何帮助:)

Many thanks, I really appreciate any help :)

推荐答案

尝试添加后 RewriteEngine叙述这个规则对

RewriteEngine on
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)/$    $1  [R=301]
RewriteRule ^reviews/([^/.]+)$ reviews/post.php?title=$1 [L]
RewriteRule ^news/([^/.]+)$ news/post.php?title=$1 [L]
RewriteRule ^page/(.*) index.php?page=$1

编辑可显示完整的一套规则

Edited to show full set of rules

编辑第2次添加在

RewriteCond %{REQUEST_FILENAME} !-d

以上的新规则,以允许仍然不会导致无限重定向循环访问directorys

above new rule to allow directorys to still be accessed without causing an infinite redirect loop