【发布时间】:2014-12-24 20:38:55
【问题描述】:
Nginx/Php 爱好者的
我在 Nginx 上运行了 Yourls 安装,使用以下 .conf 文件可以正常工作。重定向工作,数据库是从 apache 安装复制过来的,一切都很好。
但是,在旧服务器上,我在子目录中运行了另一个简单的 shortURL 服务。它有很多用 url.com/p/0aexvibr
风格创建的短 URL它们都在名为“p”的子目录中运行,所有链接看起来都是这样,它们是纯文件,只有一个 URL 在顶行。
我需要让 Nginx 重定向诸如 url.com/p/0aexvibr 之类的 url,并忽略任何内容来维护由下面的 Yourls 重写规则重定向的 URL。将它们带到 /p/ 中的 php 脚本,然后打开并重定向用户链接文件中的 URL(在本例中,0aexvibr 包含 URL http://google.com)有人可以帮忙设置吗?
当前配置
server {
listen 80;
server_name url.com;
access_log /var/log/nginx/access.log;
error_log /var/log/nginx/error.log;
root /var/www/html/url;
charset utf-8;
}
location / {
try_files $uri $uri/ /yourls-loader.php;
index index.php;
include /etc/nginx/conf.d/php-fpm;
}
include /etc/nginx/drop;
}
这里是show.php的内容
<?php
if($_GET['id']){
$id = addslashes($_GET['id']);
if(file_exists("urls/$id"))
{
$url = file_get_contents("urls/$id");
header("location:".$url);
} else {
echo "Error : invalid hash";
}
} else {
echo "Error : no hash";
}
?>
【问题讨论】:
-
就像在other question 中一样,您可以使用 nginx 重写规则将请求重定向到将打开好的 url 的 php 脚本。
-
问题是,如何?
-
正如您在other question 中所做的那样,您想看看那里有什么问题吗?
-
啊,我以为我在另一个问题中做到了,但我没有,它破坏了 Yourls 安装并将一切重定向到 show.php 脚本。我只是想用 /p/ 重定向 URL。
-
因为你的 nginx 重写规则应该是这样的:
rewrite ^p/(.*)$ /dir/show.php?id=$1 last;.