URL重写和重定向(与数据库中的数据) [英] URL rewriting and redirection (with database data)
问题描述
我的网址是 http://testurl.com/user.php?id=1&name=TestUser
我的默认网址上面的链接将需要像 http://testurl.com/user/12345/TestUser
如果用户试图改变它在自己的浏览器链接中包含
http://testurl.com/user/12345
http://testurl.com/user/12345/
http://testurl.com/user/12345/TestUsers_bla-bla
然后地址栏会自动更改为 http://testurl.com/user/12345/TestUser
修改
< PHP
$ NAME =这 - 是 - 一 - 测试 - 用户;
$ n = 1;
$领域=阵列('身份证'=> $ ID,
'名'=> $名);
$ URL =HTTP://localhost/view_user.php? 。 http_build_query($字段,'',与&);
?>
< A HREF =< PHP的echo $网址;>中>查看用户LT; / A>
您需要两样东西在这里。
1) htaccess的规则来处理。
-
http://testurl.com/user/12345
-
http://testurl.com/user/12345/
-
http://testurl.com/user/12345/xxx
和相应的操作规程,以避免重复的内容(重定向旧格式 /user.php?xxx
来新格式 /用户/ ID / NAME
)
要做到这一点,你可以把这个code在根的htaccess
选项-MultiViews
RewriteEngine叙述上
的RewriteBase /
的RewriteCond%{THE_REQUEST} \ S /用户\ .PHP \?ID =([0-9] +)\ S [NC]
重写规则^用户/%1? [R = 301,L]
的RewriteCond%{THE_REQUEST} \ S /用户\ .PHP \ ID =([0-9] +)及?名=([^&放大器; \ S] +)\ S [NC]
重写规则^用户/ 1%/ 2%? [R = 301,L]
重写规则^用户/([0-9] +)/ $ user.php的?ID = $ 1 [L]
重写规则^用户/([0-9] +)/([^ /] +)$ user.php的ID = $ 1安培;名称= $ 2 [L]
注:在这一点上,确保的的mod_rewrite 的启用和的htaccess
允许(Apache的配置文件)。
一个简单的测试: http://example.com/user/12345/XXXXX
应在内部重写 /user.php?id= 12345安培; NAME = XXXXX
。
2)现在你需要适应 user.php的
逻辑(这是在这里你检查你的数据&LT ID,名称>
对存在)
< PHP
如果(!使用isset($ _ GET ['身份证'])||空($ _ GET ['身份证']))
{
//未找到错误页面(因为没有ID)
标题(HTTP / 1.1 404未找到);
返回;
}
如果(!使用isset($ _ GET ['名称'])||空($ _ GET ['名称']))
{
//没有名字 - >通过ID得到它
$名称= getNameByID($ _ GET ['身份证']); //在数据库中该功能检查
如果($名称=== NULL)
{
//错误:ID未知 - >找不到网页
标题(HTTP / 1.1 404未找到);
返回;
}
// ID存在,我们现在有它的名字 - >重定向到/用户/ ID / NAME(而不是/用户/ ID)
标题(HTTP / 1.1 301永久移动);
标题(位置:/user/".$_GET['id']."/".$name);
返回;
}
//如果我们到达这里,我们在URL中的ID和名称
//我们要检查,如果名称对应的ID(如果ID存在)
$名称= getNameByID($ _ GET ['身份证']); //在数据库中该功能检查
如果($名称=== NULL)
{
//错误:ID未知 - >找不到网页
标题(HTTP / 1.1 404未找到);
返回;
}
//现在,检查名称中的URL对应于我们从数据库中得到了一
如果($名字!== $ _GET ['名称'])
{
//它不会 - >重定向到好名字
标题(HTTP / 1.1 301永久移动);
标题(位置:/user/".$_GET['id']."/".$name);
返回;
}
//最后,在这里我们很好。
//做什么,你就必须做...
?>
我故意这样写code与重复的部分,让你明白的逻辑。
当然,你可以改善它。
My url is http://testurl.com/user.php?id=1&name=TestUser
My default url for above link would need to be like http://testurl.com/user/12345/TestUser
If user try to change it in own browser link like below
http://testurl.com/user/12345
http://testurl.com/user/12345/
http://testurl.com/user/12345/TestUsers_bla-bla
then url bar automatically changes to http://testurl.com/user/12345/TestUser
Edit
<?php
$name = "This-is-a-test-user";
$id = 1;
$fields = array('id' => $id,
'name' => $name);
$url = "http://localhost/view_user.php?" . http_build_query($fields, '', "&");
?>
<a href = "<?php echo $url; ?>"> View User</a>
You need two things here.
1) htaccess rules to handle
http://testurl.com/user/12345
http://testurl.com/user/12345/
http://testurl.com/user/12345/xxx
and corresponding rules to avoid duplicate content (redirect old format /user.php?xxx
to new format /user/ID/NAME
)
To do so, you can put this code in your root htaccess
Options -MultiViews
RewriteEngine On
RewriteBase /
RewriteCond %{THE_REQUEST} \s/user\.php\?id=([0-9]+)\s [NC]
RewriteRule ^ user/%1? [R=301,L]
RewriteCond %{THE_REQUEST} \s/user\.php\?id=([0-9]+)&name=([^&\s]+)\s [NC]
RewriteRule ^ user/%1/%2? [R=301,L]
RewriteRule ^user/([0-9]+)/?$ user.php?id=$1 [L]
RewriteRule ^user/([0-9]+)/([^/]+)$ user.php?id=$1&name=$2 [L]
Note: at this point, make sure mod_rewrite is enabled and htaccess
allowed (in Apache configuration file).
A simple test: http://example.com/user/12345/XXXXX
should internally rewrite to /user.php?id=12345&name=XXXXX
.
2) Now you need to adapt user.php
logic (this is here you check for your data <ID, NAME>
pair existence)
<?php
if (!isset($_GET['id']) || empty($_GET['id']))
{
// error page not found (since there is no ID)
header("HTTP/1.1 404 Not Found");
return;
}
if (!isset($_GET['name']) || empty($_GET['name']))
{
// no name -> get it by its ID
$name = getNameByID($_GET['id']); // this function checks in the database
if ($name === NULL)
{
// error: ID is unknown -> page not found
header("HTTP/1.1 404 Not Found");
return;
}
// ID exists, we now have its name -> redirect to /user/ID/NAME (instead of /user/ID)
header("HTTP/1.1 301 Moved Permanently");
header("Location: /user/".$_GET['id']."/".$name);
return;
}
// if we reach here, we have an ID and a name in the url
// we have to check if NAME corresponds to ID (and if ID exists)
$name = getNameByID($_GET['id']); // this function checks in the database
if ($name === NULL)
{
// error: ID is unknown -> page not found
header("HTTP/1.1 404 Not Found");
return;
}
// now, check if NAME in the url corresponds to the one we got from database
if ($name !== $_GET['name'])
{
// it doesn't -> redirect to good NAME
header("HTTP/1.1 301 Moved Permanently");
header("Location: /user/".$_GET['id']."/".$name);
return;
}
// finally, here we're fine.
// do what you then have to do...
?>
I intentionally write this code with "duplicate" parts to let you understand the logic.
Of course, you can improve it.
这篇关于URL重写和重定向(与数据库中的数据)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!