简体   繁体   中英

How can I edit a portion of a URL and reload the new page

Evening all.

This is the first post I've made on here, so I hope I get this right.

I've looked everywhere (both on this site, and elsewhere), for a solution to an issue I'm having, but I can't find anything which sounds like my issue, yet I know logically it should be doable!

I maintain two intranet sites - one English and one Welsh. They are mirrors of each other, only the domain and site name change:

Example

  http://english-site/news/item/003/170314.htm
  http://welsh-site/newyddion/item/003/170314.htm

So far, I've only found this: http://www.codingforums.com/javascript-programming/8523-how-do-i-switch-pages-automatically.html

Which gave me some hope, but I can't work out how to apply it to my situation.

So, the question is this - is there a way to edit and reload the hyperlink switching out this:

english-site/news/

For this:

welsh-site/newyddion/

But keeping the rest of the link the same so the page will load with the Welsh or English equivalent of its self.

I know most modern CMS's could do this kind of thing automatically...but I'm not using a modern CMS...I'm using FrontPage...I'm certain this is possible but cant find anywhere that agrees with me!

Cheers in advance for any help anyone can offer!

FrontPage is ancient and you really shouldn't use it.

That being said, this bit of JS should do what you want:

window.location.href = "http://welsh-site/newyddion" + window.location.pathname.substring(window.location.pathname.indexOf('/', 1));

The current page's path (everything that follows the domain name) is accessible using window.location.pathname . Using indexOf('/', 1) on this gives us the position in the string (starting at 0) of the first / character (we pass 1 as the second parameter so as to ignore the starting slash). We then use substring to get everything from that character on.

Finally, we set the new URL to window.location.href , which performs a redirect.

You could possibly do this using regex, but this works just as well.

I am assuming your page has a button/link/select event which triggers which domain you want to serve from saying English or Welsh . Given this condition, you can do a simple javascript replace shown below :

if (selection === "blah blah"){
    domain = "http://domainA...";
} else {

    domain = "http://domainB...";
}


window.location.replace(domain);

reference

I wouldn't recommend having urls generated with javascript, but rather links on each of the page to the corresponding translated page using rel="alternative" and hreflang="code" with the language code corresponding to the 2 letter language code standard as depicted in http://googlewebmastercentral.blogspot.mx/2010/09/unifying-content-under-multilingual.html to instruct the bots the pages are the same content in different languages.

If you could use a bit of php or server side code you could create your link reference very easily by replacing the urls with the new urls you are trying to create out of the current url. This is done by using patterns that perfectly match your criteria of url rewriting, that said, if you dont have any pattern, the best would be to set each link url separate per page.

Lets say you only need to convert english-site domain to welsh-site and news path segment to. According to http://reference.sitepoint.com/html/lang-codes , Welsh lang 2 letter standard would be 'cy'.

<?php
$lang['cy']['domain'] = 'welsh-site';
$lang['cy']['lang'] = 'Welsh';
$lang['cy']['news_slug'] = 'newyddion';
$lang['en']['domain'] = 'english-site';
$lang['en']['lang'] = 'English';
$lang['en']['news_slug'] = 'news';
$lang['default'] = 'en';

Explanation:

We are defining an array of languages where we will setup anything we need to translate. This is a multidimensional array map that defines each language by key in the first dimension, then each segment to translate in the second dimension. In this second dimansion we will setup special keys ending in _slug which will be part of the url to translate. This array can be saved in a special file apart for anything else and where we can go and edit easily without having us to modify the core code.

After defineyour initial language settings, now you need the code to identify the current language and path:

<?php
include('lang.php'); //this is the file where the language array is defined
$path = $_SERVER['REQUEST_URI'];
$host = isset($_SERVER['HTTP_HOST']) ? substr($_SERVER['HTTP_HOST'], 0, strpos($_SERVER['HTTP_HOST'], ':')) : $_SERVER['SERVER_NAME'];
foreach ($lang as $code => $l) {
    if ($l['domain'] == $host) {
        $current_lang = $code;
        break;
    }
}
if ( ! isset($current_lang)) { 
    $current_lang = $lang['default'];
}
$_ = $lang[$current_lang];
$segment_1 = reset(explode('/', trim($path, '/'));
foreach ($lang[$current_lang] as $section => $url_segment) {
    if ($segment_1 == $url_segment && substr($section, -5) == '_slug')
        $current_section = $section;
        break;
    }
}

Explanation:

This code works as a bridge code that obtains the current section and language. First we obtain the host (domain name) and url path. In the first loop we match against any language domain to find the correct language we are on, then in the second loop we try to find the current section we are on with respect to the current language.

Now a little code to write the links using known information :

<head>
<?php foreach ($lang as $code => $l): ?><?php if ($code != $current_lang) : ?>
<?php $lang_path = isset($current_section) ? str_replace('/' . $_[$current_section] . '/', '/' . $l[$current_section] . '/', $path) : $path; ?>
<?php $lang_url = '//' . $l['domain'] . $lang_path; ?>
    <link rel="alternative" hreflang="<?php echo $code; ?>" href="<?php echo $lang_url; ?>">
<?php endif; ?><?php endforeach; ?>
</head>

Explanation:

We are adding links that will tell bots the other links in your page are just different representations of this page in a different language. We also specify the lang code inside hreflang attribute ( http://googlewebmastercentral.blogspot.mx/2010/09/unifying-content-under-multilingual.html )

Then you create your links (in the body section somewhere) the exact same way:

<ul>
<?php foreach ($lang as $code => $l): ?>
<?php     if ($code != $current_lang) : ?>
<?php         $lang_path = isset($current_section) ? str_replace('/' . $_[$current_section] . '/', '/' . $l[$current_section] . '/', $path) : $path; ?>
<?php         $lang_url = '//' . $l['domain'] . $lang_path; ?>
    <li><a href="<?php echo $lang_url; ?>"><?php echo $l['lang']; ?></a></li>
<?php     else : ?>
    <li class="active"><?php echo $l['lang']; ?></li>
<?php     endif; ?>
<?php endforeach; ?>
</ul>

For all this to work your server must support PHP extension. I used php code because this is the most common code support to find.

You also need to change your file extensions, from .html to .php for this to work.

Hope it works for you. This might not be what you wanted, but rather what you actually need.

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