views:

107

answers:

3

Hey guys,

I've seen URL's like this around and I'm just wondering how it is they are used.

Until now I've been using www.mysite.com/users/?id=33

How can I use the other format?

+1  A: 

You will have to use the "mod-rewrite" and e.g. an .htaccess file. Apache will then send the urls according to your settings in the .htaccess file.

E.g.:

Various rewrite rules.

<IfModule mod_rewrite.c>
  RewriteEngine on

  # If your site can be accessed both with and without the 'www.' prefix, you
  # can use one of the following settings to redirect users to your preferred
  # URL, either WITH or WITHOUT the 'www.' prefix. Choose ONLY one option:
  #
  # To redirect all users to access the site WITH the 'www.' prefix,
  # (http://example.com/... will be redirected to http://www.example.com/...)
  # adapt and uncomment the following:
  # RewriteCond %{HTTP_HOST} ^example\.com$ [NC]
  # RewriteRule ^(.*)$ http://www.example.com/$1 [L,R=301]
  #
  # To redirect all users to access the site WITHOUT the 'www.' prefix,
  # (http://www.example.com/... will be redirected to http://example.com/...)
  # uncomment and adapt the following:
  # RewriteCond %{HTTP_HOST} ^www\.example\.com$ [NC]
  # RewriteRule ^(.*)$ http://example.com/$1 [L,R=301]

  # Modify the RewriteBase if you are using Drupal in a subdirectory or in a
  # VirtualDocumentRoot and the rewrite rules are not working properly.
  # For example if your site is at http://example.com/drupal uncomment and
  # modify the following line:
  # RewriteBase /drupal
  #
  # If your site is running in a VirtualDocumentRoot at http://example.com/,
  # uncomment the following line:
  # RewriteBase /

  # Rewrite URLs of the form 'index.php?q=x'.
  RewriteCond %{REQUEST_FILENAME} !-f
  RewriteCond %{REQUEST_FILENAME} !-d
  RewriteRule ^(.*)$ index.php?q=$1 [L,QSA]
</IfModule>

Regards, Paul

Paul Peelen
By the way, the example above is taken from Drupal!
Paul Peelen
thanks paul, found a good article at http://www.phpriot.com/articles/search-engine-urls/2
iamjonesy
Great! Glad I could help! /Paul
Paul Peelen
+3  A: 

Paul Peelan's answer is correct if a little verbose :-) Put this in your .htaccess file in the root of your site:

RewriteEngine On
RewriteRule ^users/(\d+)$ /users/?id=$1

This will match /users/33, /users/1, /users/12345 etc and redirect to /users/?id=12345.

This requires that your Apache configuration has the mod_rewrite engine enabled. See the mod_rewrite docs for further information.

richsage
Thanks richsage, just what i needed!
iamjonesy
i think i need to enable the RewriteEngine in httpd.conf but I'm not sure where to find it. I use a Mac btw. thanks
iamjonesy
Not sure where it is on the Mac, but something like `LoadModule rewrite_module /usr/lib/apache2/modules/mod_rewrite.so` is needed. You might need to enable the Rewrite module specifically - if you get stuck, http://serverfault.com/ might be the better place to ask for server config probs :-)
richsage
cheers! this works for numbers but if i want to replace with text it doesn't. is this due to the \d+ in the regex?
iamjonesy
Yes. `\d+` matches 1 or more numbers. If you want strings or anything else, you'll need to replace `\d+` with an appropriate regex :-)
richsage
cheers, used RewriteRule ^Profile/([a-z]+)$ Profile/?id=$1 but it only works on lowercase. not quite sure how to do caps as well
iamjonesy
^Profile/([a-zA-Z0-9]+)$ Profile/?id=$1
iamjonesy
A: 

I know you have already selected an answer for this question, but this is quite useful to know. The PHP framework Codeigniter allows you to use URLs like this by default. I found it a lot easier to do URL rewriting this way. Further information about this, including code samples, can be found at http://codeigniter.com/user_guide/general/urls.html