00:00
00:00

Our goal is for Newgrounds to be ad free for everyone! Become a Supporter today and help make this dream a reality!

Php: .htaccess "smooth Urls"

3,656 Views | 14 Replies
New Topic Respond to this Topic

Php: .htaccess "smooth Urls" 2008-08-09 12:26:59


If you've been on Newgrounds for any length of time, you've probably noticed that the BBS URLS contain bbs/topic/1. You may be thinking, "Isn't having a directory for each object overkill?" It's not. They don't use it. Instead, they use .htaccess mod_rewrite and PHP to create sexy URLS, and you can too!

Reqs:
PHP
mod_rewrite
.htaccess permissions

OK, now. Create a .htaccess file in the root dir of your site (/) containing:

RewriteEngine on
RewriteRule ^stuff/([0-9]+)/?$ /view.php?type=stuff&id=$1

Explanation: This code first turns the RewriteEngine on. Then, it redirects all requests for stuff/8558 or any number to view.php?type=stuff&id=8558 in your root dir. This works by first saying ^, which indicates the start of a pattern. Then, we say stuff/, which makes it only work in the nonexistant dir "stuff". Now, we enclose the regex [0-9]+ with () to make it so we can access it using $1. Now, we type /?, which tells it to redirect to view.php regardless if it has a ending slash or not. Next, $ signifies the end of the pattern. Now, we tell it to redirect all requests for stuff/# to view.php in the root dir, passing GET variables type and id. After id, note the $1. That means to steal whatever the user passes in [0-9]+ and assign it to id. The next step is to actually create view.php in your root dir, and make it so it can retreive data from a database and display it based the type and id number passed. Example I use on my site:

<?
INITIALIZE DATABASE
$data = mysql_query("SELECT * FROM content WHERE id=$_GET[id ]");
$info = mysql_fetch_array($data);
?>
<?=$info[data]?>

to create a dynamic data display agent (lol). Ex:
http://podtube.co.cc/show/10
http://podtube.co.cc/show/11
http://podtube.co.cc/show/12

If you has question, just ask.


n/a

Response to Php: .htaccess "smooth Urls" 2008-08-09 12:36:51


Don't use mysql_real_escape_string() for numerical values ffs.


"My software never has bugs. It just develops random features. " - Unknown

[ FluxBB developer | Quickmarks 0.5.1 | Strings & Ints - my blog ]

BBS Signature

Response to Php: .htaccess "smooth Urls" 2008-08-09 12:40:38


Don't forget that if you use this you need to hard link everything, you cannot do index.php?id=1 anymore, you need to link to /id/1 (or if you use a sub directory, /sub/id/1)


BBS Signature

Response to Php: .htaccess "smooth Urls" 2008-08-09 12:44:51


At 8/9/08 12:40 PM, DearonElensar wrote: Don't forget that if you use this you need to hard link everything, you cannot do index.php?id=1 anymore, you need to link to /id/1 (or if you use a sub directory, /sub/id/1)

Not nessecarily.
http://podtube.co.cc/newmail.php
http://podtube.co.cc/go.php?to=10
Those should still work.


n/a

Response to Php: .htaccess "smooth Urls" 2008-08-09 13:46:29


At 8/9/08 12:44 PM, ZiggyZack99 wrote:
At 8/9/08 12:40 PM, DearonElensar wrote: Don't forget that if you use this you need to hard link everything, you cannot do index.php?id=1 anymore, you need to link to /id/1 (or if you use a sub directory, /sub/id/1)
Not nessecarily.
http://podtube.co.cc/newmail.php
http://podtube.co.cc/go.php?to=10
Those should still work.

I don't quite believe you understand what he is saying. He is saying by a "hard link," he means instead of linking in your <a> (/my/page), you need to do http://www.mysite.com/my/page. I believe that is what he was saying. I'm a little tired right now, so I might not have made much sense.


BBS Signature

Response to Php: .htaccess "smooth Urls" 2008-08-09 13:53:27


At 8/9/08 12:40 PM, DearonElensar wrote: Don't forget that if you use this you need to hard link everything, you cannot do index.php?id=1 anymore, you need to link to /id/1 (or if you use a sub directory, /sub/id/1)

You know what this might actually be what drove me mad and stopped me from using mod_rewrite cheers for that little gem of info. I spent a day, wrote my rules and just couldnt figure out why nothing was working after another day testing and retesting. And searching for an answer lead to nothing. A might give it a go again, as its a useful thing if you can do it yourself from scratch!


PHP Main :: C++ Main :: Java Main :: Vorsprung durch Technik

irc.freenode.net #ngprogramming

BBS Signature

Response to Php: .htaccess "smooth Urls" 2008-08-09 14:01:21


At 8/9/08 01:46 PM, blah569 wrote:
At 8/9/08 12:44 PM, ZiggyZack99 wrote:
At 8/9/08 12:40 PM, DearonElensar wrote: Don't forget that if you use this you need to hard link everything, you cannot do index.php?id=1 anymore, you need to link to /id/1 (or if you use a sub directory, /sub/id/1)
Not nessecarily.
http://podtube.co.cc/newmail.php
http://podtube.co.cc/go.php?to=10
Those should still work.
I don't quite believe you understand what he is saying. He is saying by a "hard link," he means instead of linking in your <a> (/my/page), you need to do http://www.mysite.com/my/page. I believe that is what he was saying. I'm a little tired right now, so I might not have made much sense.

Yes, I see now. Thanks. So, I made http://podtube.co.cc/show/15 . Let's check it out. Srsly. It should work, I don't see why not... actually, I do. But still, you could use ../../bleck.htm


n/a

Response to Php: .htaccess "smooth Urls" 2008-08-09 15:19:00


At 8/9/08 01:53 PM, Jon-86 wrote: You know what this might actually be what drove me mad and stopped me from using mod_rewrite cheers for that little gem of info. I spent a day, wrote my rules and just couldnt figure out why nothing was working after another day testing and retesting. And searching for an answer lead to nothing. A might give it a go again, as its a useful thing if you can do it yourself from scratch!

That's why i mentioned it, had the exact same problem ;)
Added the rewrite and all but kept using the same way of linking, that is relatively.
So where i once linked to domain.com/index.php?id=2 from domain.com/index.php?id=1 i know had the pleasure of getting the url domain.com/id/1/id/2 (or something to that effect), and once you realize it you hate yourself for not thinking of that.


BBS Signature

Response to Php: .htaccess "smooth Urls" 2008-08-09 15:24:23


Yeah your right, its such an obvious thing to try, I dont know why I didnt. But more people who write mod_rewite tutorials should mention it, its not as if using relative paths is unusual or anything, I would say even more common than full paths because most people find them easier to use then having to type in the full URL


PHP Main :: C++ Main :: Java Main :: Vorsprung durch Technik

irc.freenode.net #ngprogramming

BBS Signature

Response to Php: .htaccess "smooth Urls" 2008-08-09 15:38:31


At 8/9/08 03:24 PM, Jon-86 wrote: Yeah your right, its such an obvious thing to try, I dont know why I didnt. But more people who write mod_rewite tutorials should mention it, its not as if using relative paths is unusual or anything, I would say even more common than full paths because most people find them easier to use then having to type in the full URL

Most people use relative paths since you don't have to worry if you are working on the "root" of a domain, or a subdirectory (or directory under that etc.)
Especially if you want your code somewhat portable relative url's become quite handy.


BBS Signature

Response to Php: .htaccess "smooth Urls" 2008-08-09 19:36:58


Yay for relative URLs.
Also, on my site, running the mod_rewrite code for /show/12 and etc. lets me relatively access stuff on root using / before the relative url. This is a small solution to the problem.


n/a

Response to Php: .htaccess "smooth Urls" 2008-08-09 19:43:17


Yes, that is hard linking stuff, you no longer have things relative thus your urls point to a specific place (much worse if you work with sub directories) and your code is not portable without adjustments anymore.


BBS Signature

Response to Php: .htaccess "smooth Urls" 2008-08-09 19:49:41


At 8/9/08 07:43 PM, DearonElensar wrote: Yes, that is hard linking stuff, you no longer have things relative thus your urls point to a specific place (much worse if you work with sub directories) and your code is not portable without adjustments anymore.

True. But it is relative to an extent. Assume I have ex.com parked on ex.org. I would like the user to stay on ex.org or ex.com all the way throughout their visit, so /bleck.html is useful. Also, remember, /stuff is not a directory. It's a virtual directory. So, it wouldn't make much sense to have subdirectories and shiz, would it? Plus, there was something about excluding file types and forcing them to /. Can;t remember where I saw that. Something like

RewriteRule ^([a-z,0-9+]).gif$ /$1.gif

That code is incorrect :P


n/a

Response to Php: .htaccess "smooth Urls" 2008-08-09 20:56:05


There are multiple reasons why you would want your website you work under a sub directory, and to the server /stuff/ is a sub directory if you visit it through an url.


BBS Signature