将 URL 从特定文件夹重定向到外部域(使用 php?) [英] redirect urls from specific folder to external domains (with php?)

查看:46
本文介绍了将 URL 从特定文件夹重定向到外部域(使用 php?)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要这个网站是一个 wordpress 网站,

The site i need this for is a wordpress site,

基本上我的/out/文件夹后跟合作伙伴的网址.我希望访问此类链接的人被转发到该网址我知道有插件可以做这本手册,但我需要数千个网址,所以我基本上想用通配符来做

Basicly my /out/ folder is followed by an url of a partner. i want people that go to such a link to get forwarded to that url I know there are plugins to do this manual, but i will need thousands of urls, so i basicly want to do it with a wildcard somehow

这可以用 php 做吗?

Is this possible to do with php?

https://example.com/out/https://externaldomain.com
should go to https:// externaldomain.com

这部分会改变,但始终是一个网址:

this part will change, but is always an url:

https://example.com/out/*  https:// externaldomain.com  *  

所以基本上https://example.com/out/"应该消失,只留下 * https://externaldomain.com * 部分

So basicly "https:// example.com/out/" should vanish, leaving just the * https:// externaldomain.com * part

如果有人能指出我正确的方向,那就太好了

if anyone can point me in the right direction that would be great

(我最初问这个问题的想法是用 htaccess 来做,但我相信这不可能像我想要的那样)

(i originally asked this question with the idea to do it with htaccess, but i believe this isn't possible to do like i want)

推荐答案

AltoRouter 是一个用 PHP 编写的路由类(5.3+)
您可以在这里充分利用它,它对于创建视图非常有用.这不使用 file.php?=%URL%,但允许您使用 /out/https://example.com.

AltoRouter is a routing class written in PHP (5.3+)
You can use it to your advantage here, it is really useful for creating views. This is without using file.php?=%URL%, but allows you to use /out/https://example.com.

index.php

<?php 

require("AltoRouter.php");

$router = new AltoRouter();

/* Routes */
/* $router->map(Method, Route , Target , Name) */

$router->map('GET|POST','/out/[*:url]', 'file.php', 'out');

/* Match route */
$match = $router->match();

/* Check if there is a match */
if($match) {
  header("HTTP/1.1 200 OK");
  require $match['target'];
} else {
  header("HTTP/1.0 404 Not Found");
  echo "Error 404";
}

?>

然后,您需要另一个文件,您可以任意命名,只需确保文件名与路由器映射中的目标匹配即可.

Then, you'll need another file, you can name this anything, just make sure that the name of the file matches the target in your router map.

file.php

<?php

/* Get the URL we need to redirect to */
$url = $match["params"]["url"];

/* Redirect to the URL */
header("location: $url");

/* Exit to prevent any further processing */
die();

?>

据我所知,仅使用 htaccess 无法正常工作.要使用 AltoRouter,您需要将以下内容添加到您的 htaccess 文件中:

As far as I can tell, using just htaccess would not function properly. To use AltoRouter, you will need to add the following into your htaccess file:

RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule . index.php [L]

所有这些文件都需要进入您的网络服务器的根目录.如果您不确定,它通常位于 public_html 文件夹内,但如果您找不到它,请联系您的托管服务提供商.

All of these files need to go into the root directory of your webserver. If you're not sure, it's typically inside of the public_html folder, but if you're unable to find it, contact your hosting provider.

这篇关于将 URL 从特定文件夹重定向到外部域(使用 php?)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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