4

I've been wondering for a long time how people manage to get their URLs to work without file extentions such as '.html' or '.apsx' on the end.

Look at this website for example:

http://www.axongarside.com/Communication
http://www.axongarside.com/Communication/Compleat
http://www.axongarside.com/Brand
http://www.axongarside.com/Brand/K3-Group

How have they accomplished this? The only method I can think of would be to create a new directory for each page and have an index page in each directory but this would be a huge hassle for a larger website. Is there any other way?

Thankyou

user1636130
  • 1,615
  • 5
  • 29
  • 47
  • 2
    Url rewriting, ASP.NET Routing etc. What kind of software are you using for your webserver, application etc? – Onkelborg Jan 08 '13 at 08:56
  • 1
    URL Rewriting - http://en.wikipedia.org/wiki/Rewrite_engine – Tim M. Jan 08 '13 at 08:56
  • 1
    Using the URL Rewrite Module, check out this question: http://stackoverflow.com/questions/3039166/make-friendly-url-in-asp-net – Mario Bellart Jan 08 '13 at 08:57
  • 1
    This is called URL rewriting. It is a process in which you configure your server to fire a, for example, `product.aspx` file when the URL matches a certain pattern, for example, `http://example.com/products/`. And the directory/page does not have to exist physically. – Salman A Jan 08 '13 at 08:57
  • Thanks guys! You've been a real help – user1636130 Jan 08 '13 at 09:17
  • Also you can use a hashtag that will redirect from `example.com#page` -> `example.com/page.html`. check my answer here https://stackoverflow.com/a/66357780/10382407 – LeanKhan Feb 24 '21 at 19:41

3 Answers3

5

The three most common ways are to:

  • Use index pages (i.e. just create a directory and put an index.html file in it).
  • Use a rewrite engine (such as Apache's mod_rewrite) to map the URLs onto different files (this is a common approach in PHP-land).
  • Use a front controller script which processes the URLs for you (this is the usual approach for MVC frameworks).

The latter approach would use something like this in an Apache server configuration:

WSGIDaemonProcess example processes=2 threads=15 display-name=%{GROUP}
WSGIProcessGroup example
WSGIScriptAlias / /hosts/example.com/application/wsgi.py

or

SetHandler fcgid-script
Alias / /hosts/example/application.fcgi/

For scripts using WSGI (Python) or FastCGI (Cross-language, that particular example was cribbed from a Perl application I'm writing) respectively.

The URL format would be handled by the script itself, and different frameworks take different approaches to the problem.

In Catalyst, this is done by providing attributes to subroutine names.

Dancer has its own route handler syntax.

Web::Simple uses subroutine prototypes.

Django uses a separate file containing a list of patterns.

Quentin
  • 914,110
  • 126
  • 1,211
  • 1,335
2

If you are using Apache you can use the .htaccess file to remove the .html extension like so:

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)\.html$ /$1 [L,R=301] 

To remove the .html extension from the url just link to the page without .html

<a href="http://www.mysite.com/page">linkhere</a>
Bart
  • 444
  • 2
  • 6
1

If you are using Apache HTTP Server you should check mod_rewrite capability. IIS also has a similar URL rewrite feature.

Can Guney Aksakalli
  • 1,320
  • 1
  • 11
  • 24