php - How do URLs work?


i have build urls of site stack overflow urls. urls below.

http://www.example.com/1255544/this-is-the-url-text 

in url, 1255544 id , this-is-the-url-text url title text, both stored in database. have noticed stack overflow urls work on base of id. suppose here stack overflow url

http://stackoverflow.com/questions/15679171/new-way-to-prevent-xss-attacks 

in url, 15679171 id of post. when changed id 15706581 without touching new-way-to-prevent-xss-attacks , pressed enter, url automatically changed following url:

http://stackoverflow.com/questions/15706581/simplecursoradapter-does-not-load- external-sqlite-database-id-error 

and when tried remove of part of url title text below

http://stackoverflow.com/questions/15679171/new-way-to-preve 

it automatically corrects url below:

http://stackoverflow.com/questions/15679171/new-way-to-prevent-xss-attacks 

it means data being retrieved database on basis of id 15679171 , relevant url title text, new-way-to-prevent-xss-attacks, attached according id.

i want this. problem is, not going understand if change id of url title text should automatically changed. how do this?

i have thought following:

$url_id = $_get["id"]; $url_txt = $_get["url_txt"];  $qry = "select * mytable url_id = '{$url_id}' limit 1"; $data = mysql_query($qry); $data_set = mysql_fetch_array($data);  if($url_txt== $data_set["url_txt"]) {     // proceed further } else {      $rebuilt_url = "http://www.example.com/" . $url_id . "/" . $data_set["url_txt"];      header("location: {$rebuilt_url}")  // redirect url } 

is proper , efficient way perform action or not? there solution?

your code looks pretty , in 1 of stack overflow answers had suggested similar approach. however, suggest 1 minor improvement. instead of:

header("location: {$rebuilt_url}"); 

you should do:

header ('http/1.1 301 moved permanently'); header("location: {$rebuilt_url}"); 

this let browser cache these urls aggressively , code , sql queries not executed every time.

also check:

  1. .htaccess if url bad something
  2. .htaccess rewrite friendly urls

Comments

Popular posts from this blog

javascript - jQuery: Add class depending on URL in the best way -

caching - How to check if a url path exists in the service worker cache -

Redirect to a HTTPS version using .htaccess -