Modify this rewrite to be automatic

Discussion in 'Apache' started by mark_s, Apr 16, 2008.

  1. #1
    Hello,

    At the moment I have this...

    RewriteEngine on
    RewriteRule ^(profile|biography|schedule|quotes|career|results|suggestpoll|media|mobile|advertise|about|team|links)(/)?$ $1.php [L]

    Please can someone modify this so that I don't need to specify each page in the code but instead it will automatically know to find the related .php page based on what you type /here/ ?

    Thanks in advance :)
     
    mark_s, Apr 16, 2008 IP
  2. jayshah

    jayshah Peon

    Messages:
    1,126
    Likes Received:
    68
    Best Answers:
    1
    Trophy Points:
    0
    #2
    Perhaps if you use

    ([a-z]+)
    Code (markup):
    in place of (profile|.....|links)

    Jay
     
    jayshah, Apr 16, 2008 IP
  3. mark_s

    mark_s Peon

    Messages:
    497
    Likes Received:
    10
    Best Answers:
    0
    Trophy Points:
    0
    #3
    Thanks a lot but now when you go to a page that is within a real directory, it gives a 404..? So is there a way to exclude a couple of directories?
     
    mark_s, Apr 16, 2008 IP
  4. jayshah

    jayshah Peon

    Messages:
    1,126
    Likes Received:
    68
    Best Answers:
    1
    Trophy Points:
    0
    #4
    Add a RewriteCond to check if the file/dir exists, or use a directory name that has something besides the alphabet (i.e. my-images or _images).

    Jay
     
    jayshah, Apr 17, 2008 IP
  5. mark_s

    mark_s Peon

    Messages:
    497
    Likes Received:
    10
    Best Answers:
    0
    Trophy Points:
    0
    #5
    That sounds like a good solution, how can I do that?
     
    mark_s, Apr 17, 2008 IP
  6. rodney88

    rodney88 Guest

    Messages:
    480
    Likes Received:
    37
    Best Answers:
    0
    Trophy Points:
    0
    #6
    RewriteEngine On
    RewriteCond %{SCRIPT_FILENAME} !-d
    RewriteRule ^([a-z]+)/?$ $1.php [L]
    Code (markup):
     
    rodney88, Apr 20, 2008 IP
  7. mark_s

    mark_s Peon

    Messages:
    497
    Likes Received:
    10
    Best Answers:
    0
    Trophy Points:
    0
    #7
    Thank you but there's a slight problem with the code, when there is a php file with a '-' in it, it will give a 404.

    /page/ works

    /page-page/ 404
     
    mark_s, Apr 20, 2008 IP
  8. rodney88

    rodney88 Guest

    Messages:
    480
    Likes Received:
    37
    Best Answers:
    0
    Trophy Points:
    0
    #8
    The regex only matches a-z. If you want it to include a dash then add it in.

    RewriteRule ^([a-z-]+)/?$ $1.php [L]
    Code (markup):
     
    rodney88, Apr 20, 2008 IP
  9. mark_s

    mark_s Peon

    Messages:
    497
    Likes Received:
    10
    Best Answers:
    0
    Trophy Points:
    0
    #9
    Awesome, thank you!!! :)
     
    mark_s, Apr 20, 2008 IP
  10. mark_s

    mark_s Peon

    Messages:
    497
    Likes Received:
    10
    Best Answers:
    0
    Trophy Points:
    0
    #10
    After I implemented this, within days my sub-pages started completely vanishing from Google when they previously were #1-4. I've now put back my original .htaccess.
     
    mark_s, Apr 24, 2008 IP