Mod重写包括标点符号? [英] Mod rewrite including punctuation?

查看:119
本文介绍了Mod重写包括标点符号?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在网站上遇到了一个错误:基本上,它不允许页面名称中包含句号(句点).我知道问题的根源在我的.htaccess文件中.

I've come across a bug in my site: basically, it won't allow page names with full stops (periods) in them. I know the root of the problem is in my .htaccess file.

RewriteEngine on
RewriteRule ^([^/\.]+)/?$ index.php?section=$1 [L]
RewriteRule ^([^/\.]+)/([^/\.]+)/?$ index.php?section=$1&page=$2 [L]
RewriteRule ^([^/\.]+)/([^/\.]+)/([^/\.]+)/?$ index.php?section=$1&page=$2&split=$3 [L]

这里正则表达式将不会与任何句点匹配,如果用户试图直接访问文件,这是有意的.

Here the regex will not match anything with a period, this is intentional if a user is trying to directly access a file.

以下是一些常见网址的示例.

Here are some examples of common URLs.

games/Spider:+The+Secret+of+Bryce+Manor
games/Orbital
features/Interviewed:+Dennis+Sijnen+from+No+Monkeys
news/all/4

URL的split部分通常仅用于分页. URL的page部分是我想放置句号的地方,例如:

The split portion of the URL is usually only used for pagination. The page portion of the URL is where I would want to place full stops, for example:

games/Must.Eat.Birds

由于我不太擅长于mod rewrite或regex,所以我只想一种允许句号停止的解决方案.我知道它可能是更复杂的正则表达式,在这里,我超出了我的理解范围.

As I'm not particularly good at either mod rewrite or regex I'd just like a solution that allows full stops. I know it could potentially be a more complex regex and here I'm out of my depth.

感谢您的帮助.

推荐答案

Simons响应将解决您的问题,只需将部分替换为[^/]而不是[^/\.],顺便说一句,您无需转义套期.

Simons response will solve your problem, just replace the "page" portion with [^/] instead of [^/\.] which btw, you don't need to escape periods in sets.

根据问题的文本,您可能会对一点mod_rewrite魔术感兴趣:重写的这一部分将与DocumentRoot中的任何实际文件,链接或目录匹配-如果首先发生,则不会与您以后的匹配规则-因此确保/css/main.css进入真实文件.然后,其他所有内容都将被重写并转储到index.php中.

A little bit of mod_rewrite magic you may be interested in based on the text of your question: This portion of rewrite will match any real files, links, or directories in your DocumentRoot - if this happens first, it wont match your later rules - therefore ensuring /css/main.css goes to the real file. Then anything else gets rewritten and dumped into index.php.

# -s = File Exists
RewriteCond %{REQUEST_FILENAME} -s [OR]
# -l = Is a SymLink
RewriteCond %{REQUEST_FILENAME} -l [OR]
# -d = Is a Directory
RewriteCond %{REQUEST_FILENAME} -d
# if we match any of the above conditions - serve the file.
RewriteRule ^.*$ - [NC,L]

# adding your rules with the slight modifications pointed out above and by simon.
# only allows '.' in the "page" portion.
RewriteRule ^([^/.]+)/?$ index.php?section=$1 [L]
RewriteRule ^([^/.]+)/([^/]+)/?$ index.php?section=$1&page=$2 [L]
RewriteRule ^([^/.]+)/([^/]+)/([^/.]+)/?$ index.php?section=$1&page=$2&split=$3 [L]

这篇关于Mod重写包括标点符号?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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