URL Rewriting in a PHP Website

Discussion in 'PHP' started by spicemint, Jan 31, 2011.

  1. #1
    spicemint, Jan 31, 2011 IP
  2. G3n3s!s

    G3n3s!s Active Member

    Messages:
    325
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    80
    #2
    RewriteEngine on
    RewriteRule about _Site/about.php [L]
    RewriteRule contact _Site/contact.php [L]

    and so on :)
     
    G3n3s!s, Jan 31, 2011 IP
  3. spicemint

    spicemint Active Member

    Messages:
    194
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    58
    #3
    Thanks for your help. Rewriting is worked fine but CSS and images in those pages were not displayed correctly. Any clue?

    Also can you please provide a generic rewriting rule for all pages under '_Site' folder?

    Thanks
    SpiceMint
     
    spicemint, Jan 31, 2011 IP
  4. G3n3s!s

    G3n3s!s Active Member

    Messages:
    325
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    80
    #4
    it isn't possible to do it generic... about images, where you got them? If in _Site/images/something.jpg, & CSS in _Site/css/style.css try this

    RewriteEngine on
    RewriteRule (.*)\.css _Site/css/$1.css [L]
    RewriteRule (.*)\.jpg _Site/images/$1.jpg [L]
    RewriteRule (.*)\.png _Site/images/$1.png [L]
    RewriteRule (.*)\.gif _Site/images/$1.gif [L]
    # any other formats of images
    RewriteRule about _Site/about.php [L]
    RewriteRule contact _Site/contact.php [L]
     
    G3n3s!s, Jan 31, 2011 IP
  5. spicemint

    spicemint Active Member

    Messages:
    194
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    58
    #5
    Thanks again for your support. It's working fine. But I need little clarification. If I enter http://www.domainname.com/about, it will goto about page without any problems. Bu if I enter something like "about1", "abouthghgh" etc after domain name, it will also goto about page. Is there any way to strictly specify "about" is the only word for correct redirection. This problem persist in all other pages.

    Thanks
    SpiceMint
     
    spicemint, Feb 4, 2011 IP
  6. gapz101

    gapz101 Well-Known Member

    Messages:
    524
    Likes Received:
    8
    Best Answers:
    2
    Trophy Points:
    150
    #6
    i'm borrowing your code G3n3s!s
    you can optimize here:

    RewriteEngine on

    RewriteRule ^(.*)\.css$ _Site/css/$1.css [L]
    RewriteRule (.*)\.(jpg|jpeg|png|gif|bmp)$ _Site/images/$1.$2 [L]
    # any other formats of images

    RewriteRule ^(about|contact)$ _Site/$1.php [QSA,L]
     
    gapz101, Feb 4, 2011 IP
  7. G3n3s!s

    G3n3s!s Active Member

    Messages:
    325
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    80
    #7
    yup, that's faster :D

    for OP, if you want to do it stricly, do it as gap said or this way

    ($ = end of line (end of link))
     
    G3n3s!s, Feb 5, 2011 IP
  8. spicemint

    spicemint Active Member

    Messages:
    194
    Likes Received:
    4
    Best Answers:
    0
    Trophy Points:
    58
    #8
    Thanks :) for your help. Now it's working perfectly.
     
    spicemint, Feb 9, 2011 IP