Web Cleaner URLs

Skilled Illusionist
Joined
Nov 12, 2011
Messages
360
Reaction score
93
Wow I have been wondering how that's done for a while and I guess I just learned. Thanks for a nice and short tutorial!
 
Have Fun!
Joined
Nov 2, 2008
Messages
481
Reaction score
70
I was wondering for some time how was this done. Thanks for sharing this!

2
 
Skilled Illusionist
Joined
Apr 21, 2012
Messages
337
Reaction score
144
You can also do something similar by having a file such as "site" that htaccess forces to run as a php script that then includes the index.php and then instead of using $_GET, you could get the current page uri, explode by /'s, take the last section and load the page like that.
Would make Urls look like:
Code:
website.com/site/home
 
Skilled Illusionist
Joined
Apr 21, 2012
Messages
337
Reaction score
144
Or as Ian (MrSaints) said to me on IRC, you can directly use index.php and have .htaccess rules to redirect all non-existent files/directories straight to index.php.
 
Divine Celestial
Loyal Member
Joined
Sep 29, 2008
Messages
804
Reaction score
219
Code:
RewriteEngine on 
RewriteCond %{REQUEST_FILENAME} !-d 
RewriteCond %{REQUEST_FILENAME}\.php -f 
RewriteRule ^(.*)$ $1.php

If you don't have any URL structure on your CMS (most older CMS' and some of the newer CMS'), use the above on your htaccess file to hide the php extension to have the same visual effect as greenelfx's thingymabob.
 
Last edited:
Mother effin' clouds
Loyal Member
Joined
Apr 13, 2008
Messages
1,534
Reaction score
448

Your .htaccess does indeed generate "cleaner URLs" but in my opinion, there are quite a few problems with the above .htaccess code.

Firstly, there is an issue with maintaining such routes. If for example you are building a content management system, you can not expect your clients (aka the users) to modify the .htaccess code. They require a much more user-friendly approach when it comes to let's say... adding pages or removing them. The method you provide for "cleaner URLs" is an oversimplification of route handling and makes your source code dependent on the .htaccess.

The second problem is that ALL HTTP GET query strings will be omitted besides "?page=...". Now, this can come as a benefit and as a disadvantage. The plus side is that you are practising security through obscurity in that you are closing off all potential loopholes that GET query strings may present - they may be modified by the user or may even contain user submitted data (potential MySQL injection, directory traversing, etc). On the contrary, you are now only limited to using POST requests when dealing with the URIs listed in your .htaccess. How will you go about carrying out pagination in your rankings page? Sure, you can submit a POST request each time a user selects a different page or filter type, but that is not exactly RESTful and the pages generated based on the POST payload will not be cached in comparison to those generated by GET requests and thus there will be a decreased performance. I quote from RFC 2616 Fielding, et al:



Essentially:


Hypothetically speaking, if I attack a page with GET requests, nothing on the server should change. The same result should be yielded for a certain operation - correct me if I am wrong, my explanation is not terrific.

There are a few solutions you can take to address the above problems. The latter can be easily resolved through the addition of a QSA flag which:
Appends any query string from the original request URL to any query string created in the rewrite target
But that would mean goodbye to security through obscurity...

Taken together, I believe aaronweiss' method is the best and personally, the recommended approach I would say as it addresses both problems I highlighted above. His method will allow routes to added or removed on the go without any changes being made to the .htaccess. You can even use regular expression to match the parts of the URI and route the request to its assigned controller (if you are following an MVC approach) or its assigned resource and/or method (if you are following the RMR approach). The second issue can also be addressed as once again, you can use regular expression to match for page numbers of filter types, for example: /ranking/all/1/ or... /ranking/job/thief/1 and so on.

Consequently, you will gain all benefits. Security through obscurity, caching (performance) and maintainability. This method goes hand in hand with the MVC and RMR architecture so your end result will most likely be a well organised and structured code that is easy to work with.


Either way would work, I suppose. But pretty much all CMS's that have been released here use the $_GET model.

Yours does too... you are passing the page name into $_GET['page'] so you will probably end up with the usual switch case routing system which is extremely redundant and if not, you will possibly end up autoloading with little room to expand on the routes (e.g. pagination).

Just a side note, you could have simplified your code to:
Code:
RewriteRule ^(.*)$ index.php?page=$1

Or you could replace (.*) with a list of accepted pages separated by |.
Also, I just noticed a third problem in your code. You did not specify the "end of string" through the use of $. So even if I visit: domain.com/registerlololherpderp123 I will be redirected to the registration page. Forgive me if this is what you intended on doing... it just doesn't seem right.



Uhm no. I don't see any point in suppressing the PHP extension in your case. There may be potential collisions in the directory URL and the URI. Also, it does not really have the same effect as greenelfx's .htaccess as his code suggests a centralised routing system while yours suggest a decentralised one - each route has its own file.

--

I apologise for the lengthy post. I may have made a few mistakes here and there (both grammatical and web development wise) but I'll double check when I wake up as I've been awake for some time now :\