简体   繁体   English

如何编辑URL的一部分并重新加载新页面

[英]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. 我维护着两个Intranet站点-一个英语站点和一个威尔士站点。 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 到目前为止,我只发现了以下内容: 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/ 威尔士现场/ 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! 我知道大多数现代CMS都可以自动执行此类操作...但是我没有使用现代CMS ...我正在使用FrontPage ...我确定这是可能的,但找不到与我意见相符的地方!

Cheers in advance for any help anyone can offer! 提前为任何人可以提供的帮助加油!

FrontPage is ancient and you really shouldn't use it. FrontPage是古老的,您真的不应该使用它。

That being said, this bit of JS should do what you want: 话虽如此,这部分JS应该可以完成您想要的工作:

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 . 使用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). 在其上使用indexOf('/', 1)会给我们第一个/字符在字符串中的位置(从0开始)(我们将1作为第二个参数传递,以便忽略起始斜杠)。 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. 最后,我们将新的URL设置为window.location.href ,它执行重定向。

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 . 我假设您的页面有一个button / link / select事件,该事件触发您要说EnglishWelsh来服务的域。 Given this condition, you can do a simple javascript replace shown below : 在这种情况下,您可以执行如下所示的简单javascript替换:

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. 我不建议使用javascript生成网址,而是使用rel =“ alternative”和hreflang =“ code”并使用对应于2个字母的语言代码标准的语言代码将每个页面上的链接链接到相应的翻译页面,如图所示在http://googlewebmastercentral.blogspot.mx/2010/09/unifying-content-under-multilingual.html中 ,指示漫游器页面使用不同的语言显示相同的内容。

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. 如果您可以使用一些php或服务器端代码,则可以通过使用要在当前url之外尝试创建的新url替换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. 这是通过使用与您的URL重写条件完全匹配的模式来完成的,也就是说,如果您没有任何模式,则最好将每个页面的链接URL设置为单独。

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'. 根据http://reference.sitepoint.com/html/lang-codes ,威尔士lang 2字母标准为'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. 在第二维中,我们将设置以_slug结尾的特殊键,这些键将成为要翻译的url的一部分。 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. 首先,我们获取主机(域名)和url路径。 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. 我们正在添加链接,这些链接将告诉bot您页面中的其他链接只是该页面在不同语言中的不同表示。 We also specify the lang code inside hreflang attribute ( http://googlewebmastercentral.blogspot.mx/2010/09/unifying-content-under-multilingual.html ) 我们还在hreflang属性中指定了lang代码( 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. 为了使所有这些正常工作,您的服务器必须支持PHP扩展。 I used php code because this is the most common code support to find. 我使用了php代码,因为这是最常见的代码支持。

You also need to change your file extensions, from .html to .php for this to work. 您还需要将文件扩展名从.html更改为.php,这样才能起作用。

Hope it works for you. 希望对你有效。 This might not be what you wanted, but rather what you actually need. 这可能不是您想要的,而是您实际需要的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM