通过mysql数据库重定向多个网址?多个、重定向、网址、数据库

2023-09-02 01:06:45 作者:白衬衫

我在想,如果有可能到一个URL重定向到正在从数据库中得到了另一种特定的网址是什么?例如,如果www.mydomain.com/page被输入会重定向到www.mydomain.com/folder/page~~V如果www.mydomain.com/page2被输入会重定向到www.mydomain.com/folder/page2~~V。

I was wondering if it would be possible to redirect a url to another specific url that is got from a database? For example if www.mydomain.com/page was entered it would redirect to www.mydomain.com/folder/page and if www.mydomain.com/page2 was entered it would redirect to www.mydomain.com/folder/page2.

目前一刻起,我只是重定向我404页与此code:

At the minute I just have redirects in my 404 page with this code:

<?php
$url = "http://$_SERVER[HTTP_HOST]$_SERVER[REQUEST_URI]";
$page= 'http://www.mydomain.com/page';

if ($url == $page){
    header("HTTP/1.1 301 Moved Permanently");
    header( 'Location: http://www.mydomain.com/folder/page' ) ;
}
?>

和我已经做到了这一点与其他多个页面也是如此。

and I've done this with multiple other pages as well.

感谢您的帮助!

推荐答案

确定

您必须首先从数据库中获取网址 然后使用PHP的头功能

you have to first get the url from the database then redirect it using php's header function

$result = mysql_query('SELECT * FROM `your_table` WHERE `token`= $token');
$row = mysql_fetch_assoc ( $result );
header("Location: page/{$row['url']}");

替换 your_table 与包含重定向规则你的表名

replace your_table with your table name that contains the redirection rules

然而,如果你的重定向规则总是由上加入 /页的URL,然后,再检查其他答案

however if your redirection rules always consist on adding /page before the url, then check the other answers