简体   繁体   中英

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. 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?

I believe you don't need to pass name param because id can get that.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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