Results 1 to 7 of 7

Thread: Friendly urls

  1. #1
    Join Date
    Dec 2005
    Posts
    326

    Friendly urls

    Hi,

    I want to know how to use these. The simplest way to use them. Example I want to be able to convert:

    myblog.com/blog.php?u=username to
    myblog.com/username

    Please help,

    Thanks

  2. #2
    Join Date
    Jul 2003
    Location
    Kuwait
    Posts
    5,099
    Google for 'mod_rewrite tutorials'. For your specific case:

    RewriteRule ^myblog.com/(.*?)$ myblog.com/blog.php?u=$1
    In order to understand recursion, one must first understand recursion.
    If you feel like it, you can read my blog
    Signal > Noise

  3. #3
    Join Date
    Aug 2005
    Location
    Canada
    Posts
    838
    RewriteRule ^myblog.com/(.*?)$ myblog.com/blog.php?u=$1
    This may go into infinite looping (when used in .htaccess).

    If the username never contains any period, you can use it to stop looping.
    RewriteRule ^myblog.com/([^.]*)$ myblog.com/blog.php?u=$1

    If you need to allow any char in the user name:
    RewriteCond %{REQUEST_URI} !^/+myblog.com/blog\.php
    RewriteRule ^myblog.com/(.*)$ myblog.com/blog.php?u=$1

  4. #4
    Join Date
    Dec 2005
    Posts
    326
    know where I can get a tutorial on this?

  5. #5

  6. #6
    Join Date
    Jul 2003
    Location
    Kuwait
    Posts
    5,099
    Quote Originally Posted by stuffradio
    know where I can get a tutorial on this?
    Try the URL Rewriting Guide from the fine folks at Apache (also available with your local Apache install if you have the documentation installed).
    In order to understand recursion, one must first understand recursion.
    If you feel like it, you can read my blog
    Signal > Noise

  7. #7
    Join Date
    Aug 2005
    Location
    Canada
    Posts
    838
    I think it's much better to do URL parsing in the script, just like some CMS do.

    Badly written long long list of RewriteRules so common to "Short URL/SEO friendly URL" hype
    is a goof way to waste server resource and complicate your life, IMHO.


    Since you are using PHP anyway, why not to use it's facility?
    It's much much easier to do it in PHP (for many people) and it doesn't tax the requests for non-php files.

    Example:

    This goes in myblog.com/.htaccess (or Virtual host section with <directory ... >)
    Code:
    Options +FollowSymlinks
    RewriteEngine On
    RewriteBase /myblog.com/
    RewriteRule !^index.php  index.php [L]
    And in the myblog.com/index.php, you check $_SERVER['REDIRECT_URL'] or
    other env variables to get the URL, and parse it if needed.

    You should be able to find lots of examples in OpenSource CMS/Blog code, too.

Related Posts from theWHIR.com

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •