URL正则表达式 - 删除文件名和URL的结尾斜杠? [英] URL Regex - remove trailing slash from file name and end of URL?

查看:561
本文介绍了URL正则表达式 - 删除文件名和URL的结尾斜杠?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

这篇关于URL正则表达式 - 删除文件名和URL的结尾斜杠?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆