问题
The site i need this for is a wordpress site,
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
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 *
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
(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)
回答1:
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();
?>
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]
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.
来源:https://stackoverflow.com/questions/57467883/redirect-urls-from-specific-folder-to-external-domains-with-php