"Clean" Links

Discussion in 'Scripts, 3rd Party Apps, and Programming' started by ace_case, Nov 8, 2013.

  1. ace_case

    ace_case Member

    Messages:
    217
    Likes Received:
    11
    Trophy Points:
    18
    How would I go about switching to clean links such as domain.com/content/323 instead of domain.com/content.php?ID=323 I think I would be using the .htaccess file, but what would I change?
     
  2. nkranx10

    nkranx10 Member

    Messages:
    62
    Likes Received:
    2
    Trophy Points:
    8
    well not sure if you want to do it, but if you develop web sites with codeigniter all links are clean, although you can tweak it to use get requests like ...php?ID=323

    in codeigniter the sequence is something like baseURL/controller/function/data
     
  3. essellar

    essellar Community Advocate Community Support

    Messages:
    3,295
    Likes Received:
    227
    Trophy Points:
    63
    You don't need to buy into any single framework. All you need is a consistent mapping from the "real" URLs to something "friendlier" and vice versa. (It is very much possible to do arbitrary mapping, but one doesn't normally want one's .htaccess file to be the largest and most complex part of the site, nor does one normally want to have to update .htaccess every time one adds a page/item.)

    You would use mod_rewrite (the name of the Apache URL rewriting module; it also works with other compatible web servers). The Apache documentation is actually pretty good, both the basic documentation and the URL Rewriting Guide. There are other tutorials and so on all over the web, but you probably won't need them. It does involve regular expressions and captures, but it's not mind-numbingly complicated. You just need to be a little careful about the order you put things in and make sure that your rewite conditions are no broader than they ought to be (otherwise earlier rules in your .htaccess will do things you were expecting to be handled by later rules, and much hilarity will ensue).
     
    • Useful Useful x 1
  4. pnk

    pnk New Member

    Messages:
    22
    Likes Received:
    1
    Trophy Points:
    3
    Hi,You can use the code given below in your .htaccess to accomplish what you are seeking. Also, I must confirm that your server needs to be having rewrite engine ON for this to be working. You can enable rewrite engine using command a2enmod rewrite on linux. Also for the files that have .php extensions written in url would not redirect back to filenames automatically, you would have to manually remove .php extensions from urls in your code. I would say first test it and check if it suites your requirements and is compatible with your server configurations then you can continue with .php extension removal.
    #.htaccess contents...
    RewriteEngine on

    # If the request doesn't end in .php (Case insensitive) continue processing rules
    RewriteCond %{REQUEST_URI} !\.php$ [NC]
    # If the request doesn't end in a slash continue processing the rules
    RewriteCond %{REQUEST_URI} [^/]$
    # Rewrite the request with a .php extension. L means this is the 'Last' rule
    RewriteRule ^(.*)$ $1.php [L]
     
    • Informative Informative x 1

Share This Page