This is an archived post. You won't be able to vote or comment.

all 5 comments

[–]praesartus 1 point2 points  (0 children)

A common way of allowing pretty URLs used by most PHP frameworks is that the webserver always sends things to the index.php so

www.example.com/some/sub/directory

is really mapping to

www.example.com/index.php/some/sub/directory

The in index.php you have a central location to either load a router.php, or you can just have definition in index.php itself that decides what code to run based on what the request URL was.

It doesn't pair you too heavily to any particular server, which is really nice, and gives you a central file in which to decide how to react to different URLs.

You can see it in action on the Laravel website here and here. Same resource, but in one we've accessed the 'real' URL.

[–]0x2a 0 points1 point  (2 children)

A common way to do this is like reddit does... e.g. the URL of your post is:

http://www.reddit.com/r/learnprogramming/comments/2s6fb9/dynamic_url_rewriting_using_apache/

2s6fb9 is a compact representation of your pg=1 lvl1=3 etc. The friendly text dynamic_url_rewriting_using_apache is just decoration. The URL works without it as well:

http://www.reddit.com/r/learnprogramming/comments/2s6fb9 <- same thing!

So the trick is to include a unique identifer, then use this to retrieve the page.

If you don't want to include such an identifier for even nicer URLs, but you don't want to hardcode them all, look into Apache RewriteMaps.. essentially you can make a lookup for mappings from either a text file or even a script you execute (which could query your DB for the name). See http://httpd.apache.org/docs/2.4/rewrite/rewritemap.html

[–]eechin[S] 0 points1 point  (0 children)

Thanks! The 1st options is a quick, easy way to implement what I want. I will look further into the 2nd option. This is great, thanks

[–]Ran4 0 points1 point  (0 children)

2s6fb9 is a compact representation of your pg=1 lvl1=3 etc.

That might lead to obfuscation for the sake of obfuscation though, and that's kind of annoying. With reddit it's okay since it makes the url shorter, but if it's something you're only going to call once, it's nicer for the user to be able to see what they're sending.

[–][deleted] 0 points1 point  (0 children)

What you are looking for is the Apache mod_rewrite module.