user947462
user947462

Reputation: 949

how build an url

I have a doubt about how is built a link like that:

http://stackoverflow.com/questions/ask

well, basically i would like to know how make a similar link. Ask is a page ? the extension (php, asp, ...) is removed by mod_rewrite?

normally in my websites i have always same url www.something.com. all calls are made by ajax. Or alternatively something like: www.something.com/?section=contacts

thanks

Upvotes: 1

Views: 112

Answers (6)

evan
evan

Reputation: 12553

There are 2 general ways.

1 - Create a folder and inside of it have index.php - index.php (and index.html) are usually looked for first. This is the most simple way and is not often used on large sites.

http://whatever.com/questions/ask

could be rewritten as:

http://whatever.com/questions/ask/index.php

2 - You can rewrite the url on the server. Example from codeigniter using .htaccess file:

<IfModule mod_rewrite.c>  
RewriteEngine On  
RewriteCond %{REQUEST_URI} !^(/index\.php|/img|/js|/css|/robots\.txt|/favicon\.ico)  
RewriteRule ^(.*)$ /index.php/$1 [L]  
</IfModule>    

The 3rd rule puts index.php before anything else in the url after the domain. Basically it let's you write a url like on stackoverflow and virtually convert it to a url like yours.

Upvotes: 0

paulsm4
paulsm4

Reputation: 121881

The definition of a "URL" is in RFC1738

A "friendlier" explanation of URI's ("Universal Resource Identifiers"), URL's ("Resource Locators") and URN's (Resource Names) can be found many places, including here, on Wikipedia:

http://en.wikipedia.org/wiki/Uniform_Resource_Identifier

From RFC 1738:

http://<host>:<port>/<path>?<searchpart>

PS: As a completely separate issue, here's a good summary of mod_rewrite:

http://en.wikipedia.org/wiki/Mod_rewrite

Upvotes: 0

Philip Schweiger
Philip Schweiger

Reputation: 2734

The term for what you are looking for is "friendly urls"

So, for instance, your link, www.something.com/?section=contact would become www.something.com/section/contact

It is "friendlier" to a human being looking at the address (it makes no difference if you are worrying about SEO - machines don't care).

To make your URLs friendlier, your web server will translate the friendly format into the actual, query-string based one. This way, your scripts still have access to the query string, but a human being reading your URL does not have to see the ugly question marks and ampersands that make up the query string.

You usually accomplish this via a file called .htaccess. You can read a tutorial on using .htaccess to create friendly URLs here: http://net.tutsplus.com/tutorials/other/using-htaccess-files-for-pretty-urls/

Upvotes: 3

Vilx-
Vilx-

Reputation: 107052

Well, StackOverflow ASP.NET MVC, so there's no mod_rewrite there, but that's besides the point. :)

One typical approach for applications like these is that mod_rewrite (or whatever else equivalent there is) routes all the requests to one and the same file (say, a PHP file). That file then takes a look at all the parts ("questions", "ask", etc.) and decides what to do.

But the actual URL has very little to do with which file actually gets to process it. That's also very flexible, because you can then make your urls much "prettier" and easier for humans to read. Google also likes them.

Upvotes: 1

Aman
Aman

Reputation: 548

You have in a way answered your own question. mod_rewrite is the way to go if you want your URLs to be clear and memorable like the one that you have showed in your example. AJAX calls don't need to have URLs so I won't worry about that. Only non-AJAX calls which work with different pages in the website can be handled by mod_rewrite. this process is called URL rewriting which re-writes the URL which internally points to the real page but the URL on the GUI would look like the one you have mentioned in your example.

Upvotes: 0

Christiaan Nieuwlaat
Christiaan Nieuwlaat

Reputation: 1359

Those urls are called 'clean urls', in order to create these clean urls you can use the mod_rewite url rewriting engine.

In a clean url like http://www.somedomain.ext/category/some-page the "category" and "some-page" are usually parameters to a dynamic page. The rewriting engine could rewrite the url above to http://www.somedomain.ext/index.php?cat=category&page=some-page.

Upvotes: 0

Related Questions