1. Advertising
    y u no do it?

    Advertising (learn more)

    Advertise virtually anything here, with CPM banner ads, CPM email ads and CPC contextual links. You can target relevant areas of the site and show ads based on geographical location of the user if you wish.

    Starts at just $1 per CPM or $0.10 per CPC.

How to do this simple Mod_Rewrite?

Discussion in 'PHP' started by campolar, Aug 11, 2009.

  1. #1
    Hi, if i have pages as page1.php and page2.php, how can i have them rewrited to /page1 and /page2? I only want the selected pages to, bcz the pages use include function so i dont need that messed up. And i will also make a directory /something and add something else in it so i dont want it to find something.php

    Any help is highly appreciated :D
     
    campolar, Aug 11, 2009 IP
  2. szalinski

    szalinski Peon

    Messages:
    341
    Likes Received:
    5
    Best Answers:
    0
    Trophy Points:
    0
    #2
    RewriteEngine On
    RewriteRule ^page1$ /page1.php [L]
    RewriteRule ^page2$ /page2.php [L]
    Code (markup):
    Not tested, but should work.
     
    szalinski, Aug 11, 2009 IP
    campolar likes this.
  3. campolar

    campolar Peon

    Messages:
    2,683
    Likes Received:
    244
    Best Answers:
    0
    Trophy Points:
    0
    #3
    Cool, it works... But if i use it, i can still access page1.php and page2.php, possible if anyone access page1.php or page2.php, they are redirected to /page1 and /page2?

    Another thing, if i open /page1 it works, but /page1/ doesn't, any fix to this? (i tried adding a slash to the redirect, but then the page isnt reading the css file :confused:
     
    campolar, Aug 12, 2009 IP
  4. kblessinggr

    kblessinggr Peon

    Messages:
    539
    Likes Received:
    13
    Best Answers:
    0
    Trophy Points:
    0
    #4
    In the mod_rewrite he provided, if the pattern didn't contain a / at the end then it won't match /page1/ , ideally you might need to do something like /? on the end meaning with or without /
     
    kblessinggr, Aug 12, 2009 IP
  5. badeyes

    badeyes Active Member

    Messages:
    411
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    55
    #5
    Do not delete old lines, just add these lines in your .htaccess file so you pages will work with slash

    RewriteEngine On
    RewriteRule ^page1/$ /page1.php [L]
    RewriteRule ^page2/$ /page2.php [L]

    Tested and working :) Enjoy!!!!
     
    badeyes, Aug 12, 2009 IP
  6. kblessinggr

    kblessinggr Peon

    Messages:
    539
    Likes Received:
    13
    Best Answers:
    0
    Trophy Points:
    0
    #6

    Or instead of doubling the lines, just do

    RewriteRule ^page([0-9]+)/?$ /page$1.php [L,QSA]

    which would pretty much take any page#/ and send it to page#.php
    Also the QSA flag automatically forwards any attached querystring on the end.

    Basically one line to do the work of 4
     
    kblessinggr, Aug 12, 2009 IP
  7. szalinski

    szalinski Peon

    Messages:
    341
    Likes Received:
    5
    Best Answers:
    0
    Trophy Points:
    0
    #7
    well thanks for that, and i was indeed going to do that but he did say
    so that's why I wrote the two separate lines. However this will help others surely! ;)
     
    szalinski, Aug 12, 2009 IP
  8. kblessinggr

    kblessinggr Peon

    Messages:
    539
    Likes Received:
    13
    Best Answers:
    0
    Trophy Points:
    0
    #8
    In his case he'd just need to remember the /? then which would mean may or may not have a trailing slash.
     
    kblessinggr, Aug 12, 2009 IP
  9. campolar

    campolar Peon

    Messages:
    2,683
    Likes Received:
    244
    Best Answers:
    0
    Trophy Points:
    0
    #9
    yea, i already did that :D but, after doing that, /page1 doesn't work, only /page1/ works, i want it to work either ways, and also want page1.php (if anyone accesses) to redirect to /page1/ or /page1
     
    campolar, Aug 12, 2009 IP
  10. kblessinggr

    kblessinggr Peon

    Messages:
    539
    Likes Received:
    13
    Best Answers:
    0
    Trophy Points:
    0
    #10
    Read a couple more post and you'll see the /?$ method which tells the interpretor that it may or may not have a trailing slash.
     
    kblessinggr, Aug 13, 2009 IP
  11. campolar

    campolar Peon

    Messages:
    2,683
    Likes Received:
    244
    Best Answers:
    0
    Trophy Points:
    0
    #11
    I understood that, i need the .php pages to be redirected :(
     
    campolar, Aug 13, 2009 IP
  12. superdav42

    superdav42 Active Member

    Messages:
    125
    Likes Received:
    3
    Best Answers:
    0
    Trophy Points:
    58
    #12
    Couldn't you just use a redirect?

    Redirect 301 /page1.php /page1
    Redirect 301 /page2.php /page2

    It seems like it might work, but then again it might make an infinite loop.
     
    superdav42, Aug 13, 2009 IP
  13. Gray Fox

    Gray Fox Well-Known Member

    Messages:
    196
    Likes Received:
    8
    Best Answers:
    0
    Trophy Points:
    130
    #13
    First part could be done simpler
    
    RewriteEngine On
    RewriteRule ^page([0-9]+)(/?)$ /page$1.php [L]
    
    Code (markup):
     
    Gray Fox, Aug 14, 2009 IP
  14. kblessinggr

    kblessinggr Peon

    Messages:
    539
    Likes Received:
    13
    Best Answers:
    0
    Trophy Points:
    0
    #14
    I take it you didn't read the thread.
     
    kblessinggr, Aug 14, 2009 IP
  15. campolar

    campolar Peon

    Messages:
    2,683
    Likes Received:
    244
    Best Answers:
    0
    Trophy Points:
    0
    #15
    Hmmm.... I'll have to try this (i have a feeling it will work, but wtf if it just keeps on looping? :p)

    I would have used that, but did you bother reading my reply on that post?
     
    campolar, Aug 15, 2009 IP
  16. Gray Fox

    Gray Fox Well-Known Member

    Messages:
    196
    Likes Received:
    8
    Best Answers:
    0
    Trophy Points:
    130
    #16
    My bad.

    RewriteRule ^page([0-9]+)\.php$ /page$1 [L,R=301]

    or something like that would create an infinite loop with the first rule, so I'm afraid I'm not able to help you with that.
     
    Gray Fox, Aug 15, 2009 IP
  17. campolar

    campolar Peon

    Messages:
    2,683
    Likes Received:
    244
    Best Answers:
    0
    Trophy Points:
    0
    #17
    Just tried that, and it doesn't work :(
     
    campolar, Aug 16, 2009 IP