简体   繁体   English

添加-代替URL重写中的空格

[英]Add - instead of white spaces in url rewriting

I have this url: 我有这个网址:

http://www.site.com/en/about.php?id=112&name=andrew marshall dickens

and i would like to rewrite it like this: 我想这样重写它:

http://www.site.com/112/andrew-marshall-dickens.html

so far: 至今:

RewriteRule ^([^/]*)/([^/]*)\.html$ /en/about.php?id=$1&name=$2 [L]

I'm having trouble with the '-' character.Any suggestions ? 我在使用'-'字符时遇到麻烦。有什么建议吗? Thanks! 谢谢!

Well you're attempting to use a Regex to remove characters from the middle of a string which could have any number of that character in it in the middle of a RewriteRule. 好吧,您正在尝试使用正则表达式从字符串中间删除字符,而在RewriteRule中间可能包含任意数量的该字符。 On one hand that's not really possible, on the other hand, you're passing the ID in, so I assume you can get the name using the id in your PHP script, so there's not really a need to parse the name from the URL variables, and as a 3rd option, why not just str_replace the - characters in PHP and ucwords() the string before outputting it if you want to use the name variable? 一方面,这实际上是不可能的,另一方面,您要传递ID,因此我假设您可以在PHP脚本中使用ID获得名称,因此实际上并不需要从URL解析名称。变量,作为第三个选项,如果要使用name变量,为什么不直接在输出字符串之前先str_replace PHP和ucwords()中的-字符串呢?

I believe you don't need to pass name param because id can get that. 我相信您不需要传递名称参数,因为id可以实现。

Anyway: 无论如何:

RewriteRule ^([0-9]+)/([a-z-]+)\.html$ /en/about.php?id=$1&name=$2 [L]

But hey, reading the comment, i just realized: what's your problem? 但是,嘿,阅读评论后,我才意识到:您有什么问题? Your regex should already work 您的正则表达式应该已经可以工作了

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM