Extentionless URLs

garrensilverwing

New Member
Messages
148
Reaction score
0
Points
0
I searched the forums and could not find any tutorials or information on extentionless URLs if someone can please do a tutorial on it I would be very grateful or link a tutorial (that includes cPanel) that would be just as appreciated.
 

tittat

Active Member
Messages
2,478
Reaction score
1
Points
38
Tutorial section is not for asking question/help.No problem, i have moved this thread to programing help :)
 

garrettroyce

Community Support
Community Support
Messages
5,609
Reaction score
250
Points
63
This is achieved through .htaccess and mod_rewrite.

http://en.wikipedia.org/wiki/Mod_rewrite (mainly the links at the bottom)

I think this is right:

RewriteEngine on
RewriteRule ^/([a-zA-Z0-9_-]+$ /$1.php

This means, take all characters a to z A to Z and '-' and '_' between the first '/' (after http://www.yoursite.com) and the end of the url and rewrite it as those same characters with .php at the end.

Don't give up, mod_rewrite is really really tricky :)
 
Last edited:

garrensilverwing

New Member
Messages
148
Reaction score
0
Points
0
sorry, titat, i thought i could ask for a tutorial guess not :D, garrettroyce, thanks so much you're so helpful, i would give you more reputation but i have to spread it around a bit lol

ok i found the htaccess file but i dont know how to edit it to insert that code :( is there a way to add that through cpanel? or some php script?
 
Last edited:

misson

Community Paragon
Community Support
Messages
2,572
Reaction score
72
Points
48
Content negotiation is designed to do exactly what you want. Add:
Code:
Options +MultiViews
to your .htaccess (the "+" means merge the options rather than replace them; read about the Options directive for details).
 

Teensweb

New Member
Messages
352
Reaction score
1
Points
0
"ok i found the htaccess file but i dont know how to edit it to insert that code :( is there a way to add that through cpanel? or some php script?"

Download it to your pc and use text editors like notepad or something...

For extension less urls, can't you just create a directory and put an index.html in it?
Then when you goto http://www.yoursite.com/nameofyourdirectory
you'll get to see the contents of index.html
Why do you like to have extension less urls?
 
Last edited:

garrensilverwing

New Member
Messages
148
Reaction score
0
Points
0
Content negotiation is designed to do exactly what you want. Add:
Code:

Code:
Options +MultiViews

to your .htaccess (the "+" means merge the options rather than replace them; read about the Options directive for details).

ok but i dont know how to edit the .htaccess file

"ok i found the htaccess file but i dont know how to edit it to insert that code :( is there a way to add that through cpanel? or some php script?"

Download it to your pc and use text editors like notepad or something...

For extension less urls, can't you just create a directory and put an index.html in it?
Then when you goto http://www.yoursite.com/nameofyourdirectory
you'll get to see the contents of index.html
Why do you like to have extension less urls?

i tried that but no program that i have displays it so i can read it too many special characters or something
 
Last edited:

garrettroyce

Community Support
Community Support
Messages
5,609
Reaction score
250
Points
63
cPanel editor should work as well. Sometimes there's a problem with the differences in the way that *nix and Windows encode files, especially newlines.

@mission I've never heard of using this before, thanks for the tip :)

I prefer mod_rewrite simply because it has many other functionalities and learning regex is never bad :p

For example you can make index.php?view=home&userid=3423 look like home/user3423
 
Last edited:

misson

Community Paragon
Community Support
Messages
2,572
Reaction score
72
Points
48
I prefer mod_rewrite simply because it has many other functionalities and learning regex is never bad :p

Each does what it was designed to do fairly well. mod_rewrite and content negotiation have some overlap of function, but each for a different purpose. When it comes to type-via-file-extension agnosticism (which is the Right Thing to Do© in URIs), only content negotiation cuts the mustard. There's no longer a need to specify in a URI whether each page is stored as static HTML, a script or something else entirely, which is not easy to do with mod_rewrite. You can change the file type of a resource (page, image &c) without having to make any other changes. It also obviates the need for "choose a language" links in many cases.

If you want your URI and filesystem hierarchies to be different from each other, however, only mod_rewrite will do.
 

garrettroyce

Community Support
Community Support
Messages
5,609
Reaction score
250
Points
63
Yes, but where content negotiation lacks is in query strings, which would be the next logical step in development of a user friendly URI. Even if your user is savvy enough to realize your site is using PHP or ASP.NET or any other technology, they probably won't remember site?this=123423&that=324&next=4212314 and unless you want to make one file for every single page you will ever have, mod_rewrite is the only tool to properly accomplish this. And why set up your site one way, knowing full well that you will probably have to change it later?
 

misson

Community Paragon
Community Support
Messages
2,572
Reaction score
72
Points
48
Yes, but where content negotiation lacks is in query strings, which would be the next logical step in development of a user friendly URI. Even if your user is savvy enough to realize your site is using PHP or ASP.NET or any other technology, they probably won't remember site?this=123423&that=324&next=4212314 and unless you want to make one file for every single page you will ever have, mod_rewrite is the only tool to properly accomplish this.
All of that is covered under my statement about different URI and file hierarchies. We're in complete agreement here.

And why set up your site one way, knowing full well that you will probably have to change it later?
By "set up", are you referring to configuration? Or do you refer to site layout (the URI & filesystem hierarchies)? For both cases, it's because you have to set the site up some way for the present and you don't yet know what the final form will be. Both content negotiation and mod_rewrite (along with the 301 HTTP status code) help you divorce external site layout from internal so that the external view can remain consistent as you change the implementation.
 

garrettroyce

Community Support
Community Support
Messages
5,609
Reaction score
250
Points
63
I thought it would be beneficial to specifically mention query strings, since they are an integral part of dynamic website design and you hadn't mentioned them previously. I don't claim to be an expert on content negotiation, but from my own point of view, a site utilizing mod_rewrite and a single script to output pages is a more secure, robust, and future proof way of web design, though it takes more planning and a more thorough knowledge of both the scripting language and regex, both of which can be difficult. Whereas content negotiation seems to be directed at an audience that is content with a more static solution that is easier to master.

I don't think yours is a bad solution by any means. I disagree that it is the only solution "that cuts the mustard."
Edit:
I bet you didn't think your question was a controversy, garrensilverwing :p
 
Last edited:

misson

Community Paragon
Community Support
Messages
2,572
Reaction score
72
Points
48
I thought it would be beneficial to specifically mention query strings, since they are an integral part of dynamic website design and you hadn't mentioned them previously.
Expanding on a point is appreciated.

I don't claim to be an expert on content negotiation, but from my own point of view, a site utilizing mod_rewrite and a single script to output pages is a more secure, robust, and future proof way of web design, though it takes more planning and a more thorough knowledge of both the scripting language and regex, both of which can be difficult. Whereas content negotiation seems to be directed at an audience that is content with a more static solution that is easier to master.
The purpose of content negotiation is to let the browser and server pick from among multiple content representations, differentiated by file extensions. mod_rewrite lets you use a non-identity mapping from URIs to content. Neither is targeted towards a specific internal site layout, though content negotiation does target filesystem based storage. If you're implementing a CMS, which stores many resources in a DB, you'll need the non-identity mapping of URIs to resources of mod_rewrite, but you can also use the extension agnosticism of content negotiation to hide the file types of the script(s), images &c. A complex and well designed site may use both rewriting and content negotiation for different purposes.

As for a single script being an overall better site design, it's highly dependent on the site needs and internal design. If you don't know what you're doing, it's easy to use a single monolithic script that treats everything a special case; each of these two flaws make for a decidedly non-robust and non-future proof (in that it's not very maintainable) design. The single script should mean a single entry point; multiple scripts, broken down into modules, would actually be used (which, I assume, is what you're talking about). At the highest level, the entry point functions as a dispatcher, nothing more (though other patterns might be appropriate for specific problem domains). Dispatching is a also task for web servers. In some cases, it's better to have a script handle the dispatch; in others, it's better to leave it to the server.

For a CMS, you could also have a main entry point on the frontend that's a template processor with a few other entry points for form handlers. Depending on the template capabilities, the other entry points may not be needed.

This analysis is incomplete because I'm straying into territory that's hard to analyze without specific test cases. I'm also still formulating a conceptual framework that could cover the multitude of solution domains. In addition, I haven't yet taken performance into account. This is all far beyond this thread's topic. Do you want to discuss this in a new thread? I bet we could come up with a useful theoretical design framework that would apply to extant web technologies and perhaps determine which are suitable to use when solving a given problem.

I don't think yours is a bad solution by any means. I disagree that it is the only solution "that cuts the mustard."
The mustard I mentioned is type-via-file-extension agnosticism. For example, suppose you want to refer to /images/foo.jpg and /images/bar.png, but without the file extension. You can do it with mod_rewrite by adding a rewrite rule for each image, but that scales terribly. You could also rewrite the URL to a script that scans the image directory for the image and either returns the content of the image file or redirects to the image, but that's just duplicating the functionality of content negotiation. Third option (which you see in the wild) is to rewrite the URL to point to a script that fetches the image from a DB; viable, but slower than using the filesystem. DB storage of images is also more complex with no real benefit, generally speaking.

Once you require other functionality, such as fine grain access control, other tools will come in to play to allow you to spread the ketchup, sauerkraut and other condiments.
 

Teensweb

New Member
Messages
352
Reaction score
1
Points
0
"i tried that but no program that i have displays it so i can read it too many special characters or something"

Which program did you use? (notepad should work fine)
Did you consider my other suggestion?
 

garrensilverwing

New Member
Messages
148
Reaction score
0
Points
0
"i tried that but no program that i have displays it so i can read it too many special characters or something"

Which program did you use? (notepad should work fine)
Did you consider my other suggestion?

i can open it up fine in the cpanel editing thing (i couldnt find it there before because it was a hidding . file) but if i try to open it with a program on my computer like notepad i get an error "the filename, directory name or syntax volume is incorrect" but its ok cause i can open it in the cpanel editing program
 

garrettroyce

Community Support
Community Support
Messages
5,609
Reaction score
250
Points
63
windows doesn't like files without a name (technically, in windows there's no name and the file type is "htaccess"). if you rename ht.access or something else, it'll probably work. It might look funny because *nix and windows interpret new lines differently
 

Teensweb

New Member
Messages
352
Reaction score
1
Points
0
still Did you consider my other suggestion, garrensilverwing?
 
Top