使用nginx重写与许多单独的URL [英] Using nginx rewrite with many individual URLs

查看:79
本文介绍了使用nginx重写与许多单独的URL的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要在nginx上重写大量的URL(大约250个).

I need to rewrite a large number of URLs (about 250) on nginx.

来自:http://xyzwiki.de/wiki/index.php?title=Article1

至:http://wiki.zyx.de/wiki/AlternativeNameForArcticle1

很明显,对于每个文章,来源确实使用经典的url和其他名称,我有一个表,其中包含所有来源和目的地.

As obviously the source does use classic url and also other names for the individual articles I have a table with all the sources and destinations.

我尝试使用基本的重定向示例,但是没有使它起作用.我认为其原因可能是源URL使用URL参数-但我没有找到解决方案.

I have tried to work with the basic redirect examples however I did not get it to work. I think the reason for this might be that the source URLs use URL parameters - but I did not find a solution for this.

因此,我需要一个映射,在其中我要告诉nginx一堆源URL及其各自的重写目标.

So I'd need a mapping where I tell nginx a bunch of source URLs and their respective rewrite target.

推荐答案

我个人将使用这样,如果将以下代码块保存在服务器上的某个位置,并且将auto_prepend_file设置为加载它,则将为每个php调用运行以下代码块.

With this, the code block below will run for every php call if saved somewhere on the server and the auto_prepend_file set to load it.

if ($_SERVER['SCRIPT_NAME'] == '/index.php') {
    // only proceed if this is the root index.php file 
    $title = $_GET['title'];
    $urlMap = array (
            'article1' => 'alternative1',
            'article2' => 'alternative2',
            'article3' => 'alternative3',
            ...
            'article250' => 'alternative250'
    );

    if (array_key_exists($title, $urlMap)) {
        // redirect to alternative url
        header("HTTP/1.1 301 Moved Permanently");
        header("Location: http://wiki.zyx.de/wiki/" . $urlMap[$title]);
        exit;
    } else {
        // unset vars and continue otherwise
        unset($title);
        unset($urlMap); 
    }
}

这篇关于使用nginx重写与许多单独的URL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆