0

For the lack of better words, my title is not the best, but I will describe to you more of what I am looking for. I just got my website setup to strip file extensions (website.com/home) and now I just had a few questions on how to incorporate this procedure throughout my whole site.

My main question is, when I view a users profile on my site, instead of calling it to the page by website.com/profile?id=1, I would like to know how I can call it simply by typing website.com/profile/1. If anyone has any advice that would be great!

acme
  • 14,654
  • 7
  • 75
  • 109
  • 3
    Lookup .htaccess and rewrite rules :) plenty of examples online. – Ignas Mar 25 '13 at 11:53
  • You can do this without .htaccess. Search PHP routing system. – user0103 Mar 25 '13 at 11:54
  • 1
    I had it learnt in a matter of hours just by watching a [few videos](http://www.youtube.com/results?search_query=URL+rewriting+php). – George Mar 25 '13 at 11:54
  • @webnoob, I used it only "For the lack of better words". And, If I knew what to search for, I would have. –  Mar 25 '13 at 11:55
  • This question has been asked a thousand times (if not more). e.g. [here](http://stackoverflow.com/questions/930789/htaccess-pretty-user-profile-urls). – acme Mar 25 '13 at 11:56

1 Answers1

0

You'd have to use a rewriteRule like this:

RewriteEngine on
RewriteRule ^profile/([0-9]+)/?$ /profile.php?id=$1 [QSA,L]

A few resources for learning about rewrites:

http://www.askapache.com/htaccess/mod_rewrite-basic-examples.html
http://www.cheatography.com/davechild/cheat-sheets/mod-rewrite/
http://www.branded3.com/blogs/htaccess-mod_rewrite-ultimate-guide/

Nick
  • 6,316
  • 2
  • 29
  • 47
  • I plugged this in and it didn't work, it actually just killed the style sheet. –  Mar 25 '13 at 12:07
  • Well, that should work...so you must be doing something wrong. Make sure you're putting it in the root .htaccess file, and that `RewriteEngine on` is only called once. – Nick Mar 25 '13 at 13:04
  • @Nick, you're forgetting to include the appropriate conditions: `RewriteCond %{REQUEST_FILENAME} !-d` and `RewriteCond %{REQUEST_FILENAME} !-f` (I'll suggest the edit) – Mike Rockétt Mar 25 '13 at 13:09
  • 1
    @MikeAnthony those conditions shouldn't be required, surely there wouldn't be directories called `/profile/1/`, `/profile/2/`, etc already created. – Nick Mar 25 '13 at 13:17
  • @Nick: yes, but if they're not included (in fact, just the `!-f` directive), the style sheets will rewrite as well... Standard rules. – Mike Rockétt Mar 25 '13 at 15:27
  • 1
    If the browser is trying to request the style sheets using the path /profile/[0-9]+/? then something has already gone very wrong. I agree with Nick: I don't think those RewriteCond directives are necessary in this case. – Bobulous Mar 25 '13 at 18:45