SitePoint Sponsor

User Tag List

Results 1 to 10 of 10
  1. #1
    SitePoint Guru phantom007's Avatar
    Join Date
    May 2008
    Posts
    736
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)

    Question .htaccess : Hiding .php extensions in URL

    Hi

    I was wondering if there is anyway we can hide the .php extension in the URL using .htaccess?

    For example

    These URLs:
    Code:
    http://example.com/home.php
    http://example.com/welcome/contact-us.php
    http://example.com/home/support/email_us.php

    Should look like
    Code:
    http://example.com/home
    http://example.com/welcome/contact-us
    http://example.com/home/support/email_us

    Pls let me know if this is possible and how?


    Many thanks

  2. #2
    Follow Me On Twitter: @djg gold trophysilver trophybronze trophy Dan Grossman's Avatar
    Join Date
    Aug 2000
    Location
    Philadephia, PA
    Posts
    20,580
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Try this out:
    Code:
    RewriteEngine On
    
    RewriteCond %{REQUEST_FILENAME}.php -f
    RewriteCond %{REQUEST_URI} !/$
    RewriteRule (.*) $1\.php [L]
    You still have to change all the links on your site to point to the new URLs.

  3. #3
    Certified Ethical Hacker silver trophybronze trophy dklynn's Avatar
    Join Date
    Feb 2002
    Location
    Auckland
    Posts
    14,645
    Mentioned
    19 Post(s)
    Tagged
    3 Thread(s)
    c10,

    Obviously, YOU have to create the links to those pages without the .php extension.

    Dan,

    NICELY DONE!
    Code:
    RewriteEngine On
    # turn on the mod_rewrite engine
    
    RewriteCond %{REQUEST_FILENAME}.php -f
    # IF the request filename with .php extension is a file which exists
    RewriteCond %{REQUEST_URI} !/$
    # AND the request is not for a directory
    RewriteRule (.*) $1\.php [L]
    # redirect to the php script with the requested filename
    Regards,

    DK
    David K. Lynn - Data Koncepts is a long-time WebHostingBuzz (US/UK)
    Client and (unpaid) WHB Ambassador
    mod_rewrite Tutorial Article (setup, config, test & write
    mod_rewrite regex w/sample code) and Code Generator

  4. #4
    SitePoint Guru phantom007's Avatar
    Join Date
    May 2008
    Posts
    736
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)
    Hi Guys

    Thanks for your replies.

    Can we add something to this so that it accepts parameters like http://domain.com/user/name/john

    which is similar to http://domain.com/user.php?name=john

    Thanks in advance

  5. #5
    Certified Ethical Hacker silver trophybronze trophy dklynn's Avatar
    Join Date
    Feb 2002
    Location
    Auckland
    Posts
    14,645
    Mentioned
    19 Post(s)
    Tagged
    3 Thread(s)
    c10,

    If you want something for a generic URI like that (lowercase characters only), try:
    Code:
    RewriteEngine on
    RewriteRule ^user/([a-z]+/([a-z]+)$ user.php?$1=$2 [L]
    It's all very simple when you can specify exactly what you want and understand enough regex to translate it to code. If you can't, try the Article in my signature.

    Regards,

    DK
    David K. Lynn - Data Koncepts is a long-time WebHostingBuzz (US/UK)
    Client and (unpaid) WHB Ambassador
    mod_rewrite Tutorial Article (setup, config, test & write
    mod_rewrite regex w/sample code) and Code Generator

  6. #6
    SitePoint Guru phantom007's Avatar
    Join Date
    May 2008
    Posts
    736
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by dklynn View Post
    c10,

    If you want something for a generic URI like that (lowercase characters only), try:
    Code:
    RewriteEngine on
    RewriteRule ^user/([a-z]+/([a-z]+)$ user.php?$1=$2 [L]
    It's all very simple when you can specify exactly what you want and understand enough regex to translate it to code. If you can't, try the Article in my signature.

    Regards,

    DK
    Hi

    I do not want to hard-code the user.php file in the .htaccess since the page name can be anything, so is it possible to to have /var/value type parameters without hard-coding the file name?


    Thanks

  7. #7
    Follow Me On Twitter: @djg gold trophysilver trophybronze trophy Dan Grossman's Avatar
    Join Date
    Aug 2000
    Location
    Philadephia, PA
    Posts
    20,580
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    What about when you want more than one /var/value on the end of the URL? At some point your rules are going to be too complex to want to manage them as rewrite rules, and you should just edit your .php files to look at the URL and parse them there.

    Most sites with completely arbitrary URLs that can map to many different actions just rewrite all requests to a single file, and that file's job is to parse the URL and invoke the correct code.

    Front Controller pattern - Wikipedia, the free encyclopedia

  8. #8
    Certified Ethical Hacker silver trophybronze trophy dklynn's Avatar
    Join Date
    Feb 2002
    Location
    Auckland
    Posts
    14,645
    Mentioned
    19 Post(s)
    Tagged
    3 Thread(s)
    Dan,

    Too true! However, that's a "cheat" which involves much more sophistication by the member who was only asking a simple mod_rewrite question. If he still wanted to use other scripts, "user" above could have been made a variable, too, in the regex and redirection (and remained a very simple question).

    Regards,

    DK
    David K. Lynn - Data Koncepts is a long-time WebHostingBuzz (US/UK)
    Client and (unpaid) WHB Ambassador
    mod_rewrite Tutorial Article (setup, config, test & write
    mod_rewrite regex w/sample code) and Code Generator

  9. #9
    SitePoint Guru phantom007's Avatar
    Join Date
    May 2008
    Posts
    736
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)
    In continuation to my question, can I use something like this:

    RewriteEngine on
    RewriteRule ^*/([a-z]+/([a-z]+)$ *.php?$1=$2 [L]

  10. #10
    Certified Ethical Hacker silver trophybronze trophy dklynn's Avatar
    Join Date
    Feb 2002
    Location
    Auckland
    Posts
    14,645
    Mentioned
    19 Post(s)
    Tagged
    3 Thread(s)
    c10,

    No. I've never seen either regex or a redirection like that as it makes no sense at all. Try
    Code:
    RewriteEngine on
    RewriteRule ([a-z]+)/([a-z]+)/([a-z]+)$ $1.php?$2=$3 [L]
    which would capture the filename of the php script (without extenstion) as well as the key and value for a query string and redirect properly.

    It would REALLY help if you would provide a verbal description of what YOU think your code does.

    Regards,

    DK
    David K. Lynn - Data Koncepts is a long-time WebHostingBuzz (US/UK)
    Client and (unpaid) WHB Ambassador
    mod_rewrite Tutorial Article (setup, config, test & write
    mod_rewrite regex w/sample code) and Code Generator


Bookmarks

Posting Permissions

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