使用 Mod Rewrite 以句点结尾
我想知道如何在我的网址末尾添加句点。目前,如果我有网址:
http://www.mydomain.com/Page/IDK 。
抓取信息的页面将返回
标题:IDK(没有结束句点)
其他标点符号也会发生这种情况,并且它会影响我的页面错误地显示信息。感谢您的浏览,希望有人知道解决方案。
我在 htaccess 文件中使用以下内容:
RewriteRule ^([^/.]+)/?$ /index.php?Page=$1 [L]
RewriteRule ^([^/.]+)/([^/]+)/?$ /index.php?Page=$1&Level1=$2 [L]
RewriteRule ^([^/.]+)/([^/]+)/([^/]+)/?$ /index.php?Page=$1&Level1=$2&Level2=$3 [L]
RewriteRule ^([^/.]+)/([^/]+)/([^/]+)/([^/]+)/?$ /index.php?Page=$1&Level1=$2&Level2=$3&Level3=$4 [L]
I would like to know how to allow periods at the end on my url's. Currently if I had the url:
http://www.mydomain.com/Page/I.D.K.
The page grabbing the information would return
Title: I.D.K (Without the ending period)
This also happens with other punctuation and it is effecting my pages displaying information wrongly. Thanks for looking, hope somebody knows the solution.
I am using the following in my htaccess file:
RewriteRule ^([^/.]+)/?$ /index.php?Page=$1 [L]
RewriteRule ^([^/.]+)/([^/]+)/?$ /index.php?Page=$1&Level1=$2 [L]
RewriteRule ^([^/.]+)/([^/]+)/([^/]+)/?$ /index.php?Page=$1&Level1=$2&Level2=$3 [L]
RewriteRule ^([^/.]+)/([^/]+)/([^/]+)/([^/]+)/?$ /index.php?Page=$1&Level1=$2&Level2=$3&Level3=$4 [L]
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
好吧,问题基本上解决了。如果我尝试执行
Artist1 & 操作,
但我只会使用字符串替换来用其他东西替换该符号。无论如何,为了允许在任何地方使用标点符号,即使在最后,我在重写规则中将所有&
仍然会破坏我的琴弦。 Artist2+
替换为*
。现在看起来是这样的:希望这可以帮助其他有类似问题的人。
Ok problem virtually solved. The
&
still breaks my strings if I try doingArtist1 & Artist2
but I will just use string replace to replace that symbol with something else. Anyway, to allow punctuation anywhere, even at the end, I replaced all+
with*
in my rewrite rules. Here is how it looks now:Hope this helps some other people with similar problems.
我假设它位于 http://www.mydomain.com/ 中。这是我用于第二条规则的内容(然后可以将其转换为其他规则):
它将匹配以下内容:(并允许使用连字符、逗号和 Level1 的句点)
但不会:
我尝试将我的规则列入白名单选项(例如
/[AZ]/
),而不是将它们列入黑名单(/[^3-5]/
)。我确信这两个选项都有争议,但我最喜欢白名单,因为我可以控制可以使用的内容。如果我不将其包含在我的列表中,那么它就不是一个有效的选项。当然,如果您接受很多选项,那么这可能会导致一些非常长的字符类或诸如此类的情况。如果有人访问 http://www.mydomain.com/Page/IDK&somevariable 该怎么办=某个值?它可能会破坏你的脚本(如果这个人很聪明)。您的 RewriteRule 会将它们发送到/index.php?Page=Page&Level1=IDK&somevariable=somevalue
。完全不是你预期会发生的事情。I'm assuming this is located in http://www.mydomain.com/. Here's what I'd use for the second Rule (which can then be transposed to the other rules):
That will match the following: (and will allow for hyphens, commas, and periods for Level1)
But not:
I try and whitelist my options (something like
/[A-Z]/
), rather than blacklist them (/[^3-5]/
). I'm sure there are arguments for both options, but I like whitelisting mostly because I have control over what can be used. If I don't include it in my list, it's not a valid option. Of course, if you're accepting a LOT of options, then this could make for some very long character classes or whatnot. What if someone went to http://www.mydomain.com/Page/I.D.K.&somevariable=somevalue ? It could potentially break your script (if the person is smart). Your RewriteRule would send them to/index.php?Page=Page&Level1=I.D.K.&somevariable=somevalue
. Not at all what you anticipated happening.