More .htaccess redirect help needed
I have a bunch of links that have to be redirected... But there are only two kind of those links:
URLs like
http://mydomain.com/category/new%20york
should be redirected tohttp://mydomain.com/category/New-York
, so, every space (or%20
) should be "minus", and first letter should be upper case (in category name).http://mydomain.com/category/california
should behttp://mydomain.com/category/California
, so first letter have to be upper case.Sure, if it is
http://mydomain.com/category/New%20York
, it should behttp:/开发者_运维技巧/mydomain.com/category/New-York
.
Can you help me with those please?
I'm not too sure that mod_rewrite itself can convert words into their uppercased versions nor switch %20
into a -
- you could however redirect to the lower-case version of the page, and let PHP set the record straight.
As far as I know, RewriteMap is only capable of converting a whole url to either upper- or lowercase... so you'll probably have to go with my suggestion above.
The code for that would be the following:
^category/([a-z\-\ ]+)(/)?$ index.php?category=$1 [NC,L]
...and in php, you would want to do the following:
<?php
//Get the category
$cat = ( isset($_GET['category']) )? $_GET['category'] : '';
//Check if we need to convert the url
if( ($cat != '') && (strpos($cat, ' ') !== false) || !ctype_upper($cat[0]) ) {
//$new_category = Word-Number-One
$new_category = str_replace(' ', '-', ucwords($cat));
//Redirect to the new URL
header('Location: http://' . $_SERVER['SERVER_NAME'] . '/category/' . $new_category);
}
?>
I've tried it out on my own server and it works just fine, so hopefully I didn't manage to cripple the code upon copying it to the textarea.
精彩评论