我的网址是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然后url栏自动更改为http://testurl.com/user/12345/TestUser
编辑
<?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>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-blathen 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>最满意答案
你需要两件事。
1) htaccess规则处理
http://testurl.com/user/12345 http://testurl.com/user/12345/ http://testurl.com/user/12345/xxx以及避免重复内容的相应规则(将旧格式/user.php?xxx重定向到新格式/user/ID/NAME )
为此,您可以将此代码放在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]注意:此时,请确保启用mod_rewrite并允许htaccess (在Apache配置文件中)。
一个简单的测试: http://example.com/user/12345/XXXXX : http://example.com/user/12345/XXXXX应在内部重写为/user.php?id=12345&name=XXXXX 。
2)现在你需要调整user.php逻辑(这里检查你的数据<ID, NAME>对是否存在)
<?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... ?>我故意用“重复”部分编写这段代码,让你理解逻辑。 当然,你可以改进它。
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/xxxand 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.
更多推荐
发布评论