0

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.

Divyesh Jesadiya
  • 1,105
  • 4
  • 30
  • 68

4 Answers4

1

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
Amit Verma
  • 40,709
  • 21
  • 93
  • 115
  • it's display in url bar continues like. ?company=listof.ele.top.manufacturers&company=listof.ele.top.manufacturers&company=listof.ele.top.manufacturers&company=listof.ele.top.manufacturers&company=listof.ele.top.manufacturers.... – Divyesh Jesadiya Oct 20 '15 at 08:36
  • first with same problem where second one redirect me to home page www.example.com – Divyesh Jesadiya Oct 20 '15 at 08:52
0

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

MaveRick
  • 1,181
  • 6
  • 20
  • This won't work on `example.com/file.html?parameters` – Al.G. Oct 20 '15 at 07:51
  • Why?, he just want's to replace all underscores with dots, the link you suggested doesn't need any modification – MaveRick Oct 20 '15 at 07:54
  • Yes, my example was wrong. Consider this: `/some_directory/?parameters`. If he decides to change some part of the url, it will not behave correctly. – Al.G. Oct 20 '15 at 07:56
  • how can i do this in .htaccess file? – Divyesh Jesadiya Oct 20 '15 at 07:57
  • He was asking to replace all underscores to dots not only the query variables, anyway he is looking for .htaccess redirecting now so feel free to tell him bro ;) – MaveRick Oct 20 '15 at 07:59
0

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
}
?>
Zaman
  • 551
  • 4
  • 8
0

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");
Al.G.
  • 4,327
  • 6
  • 31
  • 56