-
Want to change a url
Hi I have a question.
I have a site that has an indexing searchengine on it (searchblox). There is a sub dir that is mapped to an actual domain name but it it a dir on another domain. Basically making http://www.myrealdomain/something/an...ir/index.shtml into http://www.mymappeddomain.com/index.shtml. Can someone point me in the correct direction to deal with something like this? The index will return the first full path url (by my understanding) anyway to make it go to the second url instead? Is preg_match or preg_replace (or a combo of both a valid solution). Thanks in advance for any help.
-
As a general practice it is best to avoid PCRE unless you need it because it is rather processor heavy. Perl, which is what PCRE is modeled after, is far less so.
If you just want to change the following:
http://www.myrealdomain/something/anotherdir/thisdir/index.shtml
to
http://www.myrealdomain/index.shtml
and something/anotherdir/thisdir/
is always the same maybe str_replace() would be a better choice in this situation?
I should add that I am not familiar with searchblox.
EDIT: you seem to be missing your top level domain. For example example.com where .com is the top level domain.
-
Hi James,
Thank you for the suggestions. The missing .com is a typo. Basically Searchblox is an indexing, search software. It pulls a
Code:
<title>
<url>
<description>
Tags from the indexed content. These tags are generated for each returned object from a given query. My question (forgive me if I am not explaining it correctly) can I turn the returned url (which would be
Code:
http://www.myrealdomain.com/something/anotherdir/thisdir/index.shtml
) into
Code:
http://www.mymappeddomain.com/index.shtml
and have the urls linkable?
Also the url string isnt always the same i.e.
Code:
http://www.myrealdomain.com/something/anotherdir/thisdir/index.shtml
is just one dir that needs indexed. There are several dir (all in the same dir) For example
Code:
http://www.myrealdomain.com/something/anotherdir/thisdir1/index.shtml
Code:
http://www.myrealdomain.com/something/anotherdir/thisdir2/index.shtml
Since the beginning of the url is always the same is str_replace() still a viable option?
Thanks
-
If I understand you correctly http://www.myrealdomain.com is always the same and index.shtml is almost always different, correct?
Here is the PCRE that would replace the middle directories that you said you don't want:
Code:
<?php
$test="http://www.myrealdomain.com/something/anotherdir/thisdir/index.shtml";
$test=preg_replace('/\.com\/.*\//','.com/',$test);
echo "$test";
?>
what would an example link look like as displayed to the user?
-
The url that the user would see would look like this:
Code:
http://www.mymappeddomain.com/dir1/somename.shtml
or
Code:
http://www.mymappeddomain.com/dir2/somename.shtml
etc.
I guess my question is more based on getting the data to look like it is coming from the
Code:
http://www.mymappeddomain.com
instead of
Code:
http://www.myrealdomain.com
Where it is actually located. I hope that makes sense.
-
This is not what you asked for earlier. To avoid confusion please give a real example of the preformatted data and what you want it to look like afterwards. It should be data you are working with instead of the hypothetical ones we've been using. Please show the link that you want users to see complete with the anchor tags like the following:
Code:
<a href="http://www.mysite.com/text.txt">http://www.mysite.com/text.txt</a>
preferably using the code tags so that it is formatted like above.
-
Code:
<?php
if (!isset($_GET['qry'])) {
echo '<ul class="listings-results search">';
echo "<li><h4>We’re sorry, search is currently undergoing maintenance.</h4></li></ul>"; //error check
}
$qry = urlencode($_GET['qry']);
$url = "http://mysearchserver.com/searchblox/servlet/SearchServlet?cname=inchicago&fe=utf-8&st=adv&q_phr=&q_low=&q_not=&oc=all&pagesize=100&q_all={$qry}";
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
$xml_string = curl_exec($ch);
curl_close($ch);
$xml = new SimpleXMLElement($xml_string);
if ($xml != ''){
echo '<ul class="listings-results search">';
}
foreach($xml->results->result as $the) {
echo '<li class="listing-results">'.'<a class="list-title" href="'.$the->url.'">'.'<h4>'.$the->title.'</h4>'.'</a>'.'<span class="desc">'.$the->description.'</span>'.'</li>'.'</ul>'; //output results
} ?>
When this is indexed the xml takes on the title, url, and description of each page. However, since the files reside on http://www.mytravelsite.com/states/c.../0/apage.shtml and the mapped drive for http://www.inchicago.com is the "thiscity" dir making this
The Real indexed URL
Code:
http://www.mytravelsite.com/states/cities/thiscity/0/apage.shtml
The one that needs to be returned to the client
Code:
http://www.inchicago.com/0/apage.shtml
Hopefully that will make my question clearer.
Thanks
-
Much clearer thanks. I am playing around with it to see what I can come up with. preg_match seems to be the way to go with this one.
-
I'm betting this pattern could be simplified, but here is what I came up with:
Code:
<?php
$test="http://www.mytravelsite.com/states/cities/thiscity/0/apage.shtml";
$test=preg_match('/\/(?!\/)[^\/]*?\/(?!(\/|.*?\/)).*/',$test, $extract);
echo "http://www.inchicago.com$extract[0]";
?>
I'm a bit out of practice with my PCRE knowledge.
-
Thank you for the information. I will let you know how it goes.
-
The following does the same thing as the code I posted earlier, but has been simplified somewhat.
PHP Code:
<?php
$test="http://www.mytravelsite.com/states/cities/thiscity/0/apage.shtml";
preg_match('/\/{1}[^\/]*?\/{1}(?!.*?\/).*/',$test, $extract);
echo "http://www.inchicago.com$extract[0]";
?>
EDIT: and simplified a little further:
PHP Code:
<?php
$test="http://www.mytravelsite.com/states/cities/thiscity/0/apage.shtml";
preg_match('/\/[^\/]*?\/(?!.*?\/).*/',$test, $extract);
echo "http://www.inchicago.com$extract[0]";
?>
-
I am having a bit of trouble with this. I am a beginner with regular expressions, so please bear with me. What I am trying to do is match
Code:
http://www.mytravelsite.com/states/cities/thiscity/
to
Code:
http://www.inchicago.com/
so that when I pass the url through my object
Code:
<a href="http://www.mytravelsite.com/states/cities/thiscity/dir0-6/whateverpageisindexed.shtml">title</a>
becomes a clickable link to thats file location that looks like
Code:
<a href="http://www.inchicago.com/dir0-6[whicheverisreturned]/whateverpageisindexed.shtml">title</a>
The code
Code:
$test="http://www.mytravelsite.com/states/cities/thiscity/0/apage.shtml";
preg_match('/\/[^\/]*?\/(?!.*?\/).*/',$test, $extract);
echo "http://www.inchicago.com$extract[0]";
returns
Code:
http://www.inchicago.com/0/apage.shtml
but the numbered dir may fluctuate as well as the returned page. Hopefully that makes sense. How can I make
Code:
http://www.mytravelsite.com/states/cities/thiscity/
resolve to
Code:
http://www.inchicago.com/
but leave
alone so that it will be flexible?
-
Will the grabbed url always be this in the front of the url?- http://www.mytravelsite.com/states/cities/thiscity/
If that is a static part of the url then you could use a str_replace() on that part and set it to nothing so you are left with the last part of the url.
PHP Code:
$url = "http://www.mytravelsite.com/states/cities/thiscity/0/apage.shtml";
$new_url = str_replace("http://www.mytravelsite.com/states/cities/thiscity/", "", $url);
echo $new_url;
-
So to make it
Code:
http://www.inchicago.com/dir0-6/somepage.shtml
Would I do this?
Code:
$url = "http://www.mytravelsite.com/states/cities/thiscity/";
$new_url = str_replace("http://www.mytravelsite.com/states/cities/thiscity/", "http://www.inchicago.com/", $url);
$endurl = $passedurldata;
echo $new_url.$endurl;
-
Looks like that should work.
-
PHP Code:
<?php
$test="http://www.mytravelsite.com/states/cities/thiscity/0/apage.shtml";
preg_match('/\/{1}[^\/]*?\/{1}(?!.*?\/).*[^\/]$/',$test, $extract);
if ($extract[0]=="") $prefix="http://www.inchicago.com/";
else $prefix="http://www.inchicago.com";
echo "$prefix$extract[0]";
?>
I modified it just a little to account for the possibility that no file name is listed. This will now match
Code:
http://www.mytravelsite.com/states/cities/thiscity/0/apage.shtml
http://www.mytravelsite.com/states/cities/thiscity/
http://www.mytravelsite.com/states/cities/thiscity/27/apage.shtml
http://www.anysite.com/stage/theater/0who/apage.shtml
I do not think I fully understand what you are asking for.
How does my earlier example not give you what you are looking for?
-
Hi James,
Basically there are several hundred indexed items. Since the beginning of the url
Code:
http://www.mytravelsite.com/states/cities/thiscity/
needs to be equated to
Code:
http://www.inchicago.com/
always.
but the directories 0-6 come after and have several hundred .shtml pages in each.
So I really need to match those urls but leave the last two /dirs/ i.e.(/0/apage.shtml/ might be /1/apage23.shtml) as however they are served back from the query.
Hopefully that is clearer.
-
In that case it looks like what fastsol1 is suggesting would be better. I hope it helps.