Changing ?n=dog to dog.html

Discussion in 'PHP' started by Magestry, Jan 4, 2009.

  1. #1
    Hey guys,

    I have heard loads about changing php extensions into html extensions but I don't know how to change a variable into an html extension... how would i be able to rewrite http://www.sitename.com/?n=running to http://www.sitename.com/running.html

    If you know, please tell me.

    Also, if you know how to do this. If I make a link to http://www.sitename.com/running.html will it still show the stuff like I will use the variable running to create the whole page so will it know that the variable $n=running?

    Thanks.
     
    Magestry, Jan 4, 2009 IP
  2. Danltn

    Danltn Well-Known Member

    Messages:
    679
    Likes Received:
    36
    Best Answers:
    0
    Trophy Points:
    120
    #2
    .htaccess rule:

    If you don't have them...
    RewriteEngine On
    RewriteBase /
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    Code (markup):
    Specific to this problem:
    RewriteRule   ^running\.(html|php)/?$   index.php?n=running   [L,NC,QSA]
    Code (markup):
    Matches:
    running.html
    running.php
    running.html/
    running.php/
    Code (markup):
    Doesn't match:
    running
    running/run
    running.shtml
    running/folder/subfolder/running.html
    Code (markup):
     
    Danltn, Jan 4, 2009 IP
  3. Magestry

    Magestry Active Member

    Messages:
    822
    Likes Received:
    14
    Best Answers:
    0
    Trophy Points:
    80
    #3
    So the first code will turn anything into .html?
    And if I enter the URL as running.html will it load the same content?
     
    Magestry, Jan 4, 2009 IP
  4. Danltn

    Danltn Well-Known Member

    Messages:
    679
    Likes Received:
    36
    Best Answers:
    0
    Trophy Points:
    120
    #4
    First block of code switches on the RewriteEngine and sets the RewriteBase to / (change this as appropriate.)

    Also stops it matching actual files and directories. (Which is normally what you want, very rare you don't - and certainly not in this case.)

    Dan
     
    Danltn, Jan 4, 2009 IP
    Magestry likes this.
  5. Magestry

    Magestry Active Member

    Messages:
    822
    Likes Received:
    14
    Best Answers:
    0
    Trophy Points:
    80
    #5
    Thanks very much, worked my problem! +rep! :D
     
    Magestry, Jan 4, 2009 IP