Divyesh Jesadiya
Divyesh Jesadiya

Reputation: 957

$_SERVER["REQUEST_URI"] replace string or charecter

in my online project now i change my content and so, i have to change link too. but,for links that google already registered i have to do this. if server get request like this.

$_SERVER["REQUEST_URI"]="http://example.com/category/?company=listof_ele_top_manufacturers";

i want to redirect it to.

$_SERVER["REQUEST_URI"]="http://example.com/category/?company=listof.ele.top.manufacturers";

and company= many deffrent values. so,i want to change my url with _(underscore) to .(dot) so url should change with dot where it's have underscore. if solution come with .htaccess file change will be nice one. i already change my .htaccess file with this.

RedirectMatch 301 ^/com/ http://www.example.com/category/

it's redirect my directory name but not charecter i mention above.

Upvotes: 0

Views: 3163

Answers (4)

Amit Verma
Amit Verma

Reputation: 41219

Try this in your htaccess:

RewriteEngine on

RewriteCond %{QUERY_STRING} ^company=([^_]+)_([^_]+)_([^_]+)_([^&]+)$ [NC]
RewriteRule ^ /category/?company=%1.%2.%3.%4 [QSA,NC,L,R]

Or try :

RewriteEngine on

RewriteCond %{THE_REQUEST} /category/\?company=([^_]+)_([^_]+)_([^_]+)_([^&\s]+) [NC]
RewriteRule ^ /category/?company=%1.%2.%3.%4 [QSA,NC,L,R]

This will redirect

/?company=foo_bar_foobar

to

/?company=foo.bar.foobar

Upvotes: 1

Zaman
Zaman

Reputation: 551

before any redirect you just need to check if the "company" param has underscore in it, if yes the url is redirected

<?php
$company = $_GET['company'];
if (preg_match('/_/', $company)) {
    // contains an underscore, redirect to new url here, using str_replace function
} else {
    // does not contain any underscore
}
?>

Upvotes: 0

Al.G.
Al.G.

Reputation: 4387

Maybe what you have missed was the $_SERVER['QUERY_STRING'] variable.

/* see http://stackoverflow.com/a/6975045/3132718 */
$url = strtok($_SERVER["REQUEST_URI"],'?');

/* add the parameter part */
$url .= str_replace("_", ".", $_SERVER['QUERY_STRING']);
header("Location: $url");

Upvotes: 0

MaveRick
MaveRick

Reputation: 1191

There you go:

mixed str_replace ( mixed $search , mixed $replace , mixed $subject [, int &$count ] )

So it would be like this:

$new_uri = str_replace("_",".",$_SERVER["REQUEST_URI"]);
header('Location: '.$new_uri);

Ref: PHP:str_replace

Upvotes: 0

Related Questions