Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do Stack Overflow URLs work?

Tags:

url

php

I have build URLs of my site like Stack Overflow URLs. My URLs are as below.

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

In this URL, 1255544 is the id and this-is-the-url-text is the URL title text, both stored in the database. I have noticed that Stack Overflow URLs work on the base of id. Suppose here is a Stack Overflow URL

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

In this URL, 15679171 is the id of the post. When I changed this id to 15706581 without touching the new-way-to-prevent-xss-attacks and pressed Enter, the URL automatically changed to the following URL:

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

And when I tried to remove some of the part of the URL title text like below

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

It automatically corrects the URL as below:

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

It means the data is being retrieved from the database on the basis of id 15679171 and the relevant URL title text, new-way-to-prevent-xss-attacks, is attached according to the id.

I also want to do this. But the problem is, I am not going to understand that if someone change the id of the URL the title text should automatically changed. How do I do this?

I have thought the following:

$url_id = $_GET["id"];
$url_txt = $_GET["url_txt"];

$qry = "SELECT * FROM mytable WHERE 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 to this URL
}

Is it the proper and efficient way to perform this action or not? Is there a solution?

like image 937
Rashid Farooq Avatar asked Apr 06 '13 04:04

Rashid Farooq


People also ask

What is in URL Stack Overflow?

It specifies an "Anchor", or a position on the page, and allows you to "jump" or "scroll" to that position on the page.

How can I change my Stack Overflow URL?

You can edit your display name by going to your profile on the main Stack Overflow site, and then clicking the "edit" button. From there you will be able to set it to something custom rather than the auto-generated name you have now.

How does Stack Overflow work?

The website serves as a platform for users to ask and answer questions, and, through membership and active participation, to vote questions and answers up or down similar to Reddit and edit questions and answers in a fashion similar to a wiki.

How do I post a link on Stack Overflow?

You can write a link in a post or comment like [this](url here), for example google link. Note that the URL needs the http:// part. E.g. Google vs. [Google](www.google.com).


2 Answers

Your code looks pretty good and in one of my Stack Overflow answers I had also suggested similar approach. However, I suggest just one minor improvement. Instead of:

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

You should do:

header ('HTTP/1.1 301 Moved Permanently');
header("Location: {$rebuilt_url}");

This will let browser cache these URLs aggressively and you code and SQL queries will not be executed every time.

Also check:

  1. .htaccess if URL is bad do something
  2. .htaccess rewrite friendly URLs
like image 91
anubhava Avatar answered Oct 05 '22 17:10

anubhava


If you use Apache, I think you must know about URL rewriting

In URL rewriting you rewrite the URL to a more friendly look. Take a look at the URL below

http://www.downloadsite.com?category=34769845698752354

and when you do a URL rewrite on it, it became like this

http://www.downloadsite.com/Nettools/Messengers

URL rewriting needs to be set on the Apache configuration (rewriting rules) so it will rewrite your URL before request is interpret by PHP so you will get the exact resource you want to acquire

 RewriteRule ^/category$ /content.php  
  RewriteRule ^/category/([0-9]+)$ /.php?id=$1  
  RewriteRule  
    ^/category/([0-9]+),([ad]*),([0-9]{0,3}),([0-9]*),([0-9]*$)  
    cat.php?id=$1&sort=$2&order=$3&start=$4

Well, I can't explain you everything here, but below I provide you some links where you can learn about URL rewriting.

This the best hack you can do to create friendly URLs!

Resources:

  • http://www.cyberdesignz.com/blog/website-design/url-rewriting-top-5-ways-of-php-url-rewriting/

  • http://www.fliquidstudios.com/2009/01/06/url-rewriting-with-apache-and-php-a-simple-example/

  • http://www.sitepoint.com/guide-url-rewriting/

  • .htaccess rewrite friendly URLs

like image 22
Netorica Avatar answered Oct 05 '22 18:10

Netorica