SitePoint Sponsor

User Tag List

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

    Question Apache URL Rewriting

    Hi

    I was wondering if the following are possible with .htaccess?

    1) Hide all .php file extensions. For example http://domain.com/login.php should look http://domain.com/login

    2) When I pass params like
    Code:
    http://domain.com/search?s=abc&d=11&v=hello
    it should look like
    Code:
    http://domain.com/search/s/abc/d/11/v/hello
    Please if someone can help me achieve the above?


    Many thanks in advance

  2. #2
    SitePoint Wizard bronze trophy Jeff Mott's Avatar
    Join Date
    Jul 2009
    Posts
    1,314
    Mentioned
    19 Post(s)
    Tagged
    1 Thread(s)
    Quote Originally Posted by cancer10 View Post
    Hi

    I was wondering if the following are possible with .htaccess?

    1) Hide all .php file extensions. For example http://domain.com/login.php should look http://domain.com/login
    # If the requested URL isn't a real file
    RewriteCond %{REQUEST_FILENAME} !-f

    # Then rewrite to add a .php extension
    RewriteRule (.*) $1.php


    Quote Originally Posted by cancer10 View Post
    2) When I pass params like
    Code:
    http://domain.com/search?s=abc&d=11&v=hello
    it should look like
    Code:
    http://domain.com/search/s/abc/d/11/v/hello
    Off the top of my head, I'm not sure how you would do this one, and frankly, I don't think you should try. Google actually recommends against this kind of URL format. Since /search/s/abc/d/11/v/hello is the same as /search/d/11/v/hello/s/abc, which is the same as /search/v/hello/s/abc/d/11, you're going to end up with an unnecessarily high number of URLs that point to identical content.
    "First make it work. Then make it better."

  3. #3
    SitePoint Guru phantom007's Avatar
    Join Date
    May 2008
    Posts
    752
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Jeff Mott View Post
    [FONT=Courier New]
    Off the top of my head, I'm not sure how you would do this one, and frankly, I don't think you should try. Google actually recommends against this kind of URL format. Since /search/s/abc/d/11/v/hello is the same as /search/d/11/v/hello/s/abc, which is the same as /search/v/hello/s/abc/d/11, you're going to end up with an unnecessarily high number of URLs that point to identical content.
    Hi Thanks for sharing code.

    This will be not for google indexing, it will be shown once user logs in, which has nothing to do with google indexing...


    Thanks

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

    While I would modify Jeff's code to require only lowercase characters, that would have to be dependent upon your file names and whether you want to allow this for subdirectory requests. IMHO, it's better to have mod_rewrite do a little error checking for you so I would also check to see whether the file exists WITH the .php extension before redirecting:

    Code:
    RewriteEngine on
    
    # Check whether file exists
    RewriteCond %{REQUEST_FILENAME} !-f
    # Check whether request is a directory
    RewriteCond %{REQUEST_FILENAME} !-d
    # CHECK WHETHER file exists with .php  extension
    RewriteCond %{REQUEST_FILENAME}\.php !-f
    RewriteRule ^([a-z]+)$ $1.php [L]
    One advantage of this is that the greedy problem disappears when you to do work on your second "problem."

    Code:
    # Convert three key/value pairs in URI to query string
    # Change character range definitions to include digits or CAPS if required (it is in your example)
    RewriteRule ^([a-z]+)/([a-z]+)/([a-z]+)/([a-z]+)/([a-z]+)/([a-z]+)/([a-z]+)$ $1.php?$2=$3&#4=$5&$6=$7 [L]
    Note that, to match d's value of 11, you WILL have to change the value's character range definition (at least once) to [a-z0-9]+ or simply [0-9]+, however, using character range definitions will help you get rid of garbage.

    IF your key value pairs will ALWAYS be search, d and v IN THAT ORDER, then I would remove those character range definitions and replace with those values.

    Alternatively, you could generate the problem Jeff mentioned (different URIs for same content) by simply listing the acceptable key names like (search|d|v). Not enough information to give a recommendation, though.

    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

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

    Thanks for your inputs.

    I am getting 404 Object no found error after I navigate to http://localhost/projects/login

    i have already added the .htaccess file with the content shared by dklynn. .htaccess file is in the root of the "projects" directory

    mod_rewrite is enabled.

    What could be wrong?

  6. #6
    SitePoint Wizard bronze trophy Jeff Mott's Avatar
    Join Date
    Jul 2009
    Posts
    1,314
    Mentioned
    19 Post(s)
    Tagged
    1 Thread(s)
    It's a lot easier to answer that if you show us the code you're using now.
    "First make it work. Then make it better."

  7. #7
    SitePoint Guru phantom007's Avatar
    Join Date
    May 2008
    Posts
    752
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)
    I am using this

    RewriteEngine on
    # Check whether file exists
    RewriteCond %{REQUEST_FILENAME} !-f
    # Check whether request is a directory
    RewriteCond %{REQUEST_FILENAME} !-d
    # CHECK WHETHER file exists with .php extension
    RewriteCond %{REQUEST_FILENAME}\.php !-f
    RewriteRule ^([a-z]+)$ $1.php [L]

  8. #8
    SitePoint Wizard bronze trophy Jeff Mott's Avatar
    Join Date
    Jul 2009
    Posts
    1,314
    Mentioned
    19 Post(s)
    Tagged
    1 Thread(s)
    Quote Originally Posted by cancer10 View Post
    RewriteRule ^([a-z]+)$ $1.php [L]
    This is why. Note that when dklynn recommended matching only letters, he also said it depends on whether you want to allow for subdirectory requests. And it seems you do want to allow for subdirectory requests.

    I'd recommend you change the pattern to (.*). This pattern is standard practice, and it's used even in the Apache documentation for exactly this kind of purpose.
    "First make it work. Then make it better."

  9. #9
    SitePoint Guru phantom007's Avatar
    Join Date
    May 2008
    Posts
    752
    Mentioned
    3 Post(s)
    Tagged
    0 Thread(s)
    I changed the content of .htaccess to the following still no dice


    RewriteEngine on
    # Check whether file exists
    RewriteCond %{REQUEST_FILENAME} !-f
    # Check whether request is a directory
    RewriteCond %{REQUEST_FILENAME} !-d
    # CHECK WHETHER file exists with .php extension
    RewriteCond %{REQUEST_FILENAME}\.php !-f
    RewriteRule ^([.*]+)$ $1.php [L]

  10. #10
    SitePoint Wizard bronze trophy Jeff Mott's Avatar
    Join Date
    Jul 2009
    Posts
    1,314
    Mentioned
    19 Post(s)
    Tagged
    1 Thread(s)
    Quote Originally Posted by cancer10 View Post
    RewriteRule ^([.*]+)$ $1.php [L]
    Remove the square brackets. And the plus sign.

    It should be

    RewriteRule ^(.*)$ $1.php [L]
    "First make it work. Then make it better."


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
  •