www.webdeveloper.com
Results 1 to 13 of 13

Thread: .htaccess: change from .htm to .php

  1. #1
    Join Date
    Nov 2004
    Location
    Canada
    Posts
    330

    Question .htaccess: change from .htm to .php

    I'm about to change my entire site from .html (my pages are named *.htm) to .php.

    (Why? Robot spammer; more about that later.)

    What kind of statements do I put in my .htaccess file to provide the necessary redirects? Where do they go in the file?

    So far, I have:

    Code:
    UseRealIP on
    GeoIP on
    <Limit GET POST>
    order allow,deny
    (long list of denials here)
    allow from all
    </Limit>
    RewriteEngine on
    RewriteCond %{HTTP_REFERER} !^$
    RewriteCond %{HTTP_REFERER} !^http://(www\.)?coherentdog.org/.*$ [NC]
    RewriteCond %{HTTP_REFERER} !^http://(www\.)?coherentdog.com/.*$ [NC]
    RewriteCond %{HTTP_REFERER} !^http://(www\.)?codog.org/.*$ [NC]
    RewriteCond %{HTTP_REFERER} !^http://(www\.)?~cwhitney/.*$ [NC]
    RewriteRule \.(gif|jpg|png)$ - [F]
    Redirect (long list of redirects)
    ....
    ErrorDocument 410 /gone.htm
    ErrorDocument 404 /notfound.htm
    So obviously, I change gone.htm to gone.php, and notfound.htm to notfound.php.

    I've renamed all my .htm pages to .php (on my own machine, preparing), and edited all the include files (I use PHP SSI on an Apache server), and also, edited every darned page - around 150 of them, to cope with links (thank heavens for NoteTabPro).

    So, finally, I get to .htaccess, before I can upload the new version of my site.

    Help! (Thanks in advance!)

    Sun, 18 Dec 2005 11:49:59
    My site is my home-and-playground
    http://www.coherentdog.org/
    Carol W

  2. #2
    Join Date
    Sep 2004
    Posts
    398
    Putting this:
    Code:
    RewriteRule ^(.*)\.htm$ /$1.php [QSA,L]
    after this line:
    Code:
    RewriteRule \.(gif|jpg|png)$ - [F]
    should work.

  3. #3
    Join Date
    Nov 2004
    Location
    Canada
    Posts
    330

    Question

    Quote Originally Posted by Scleppel
    Putting this:
    Code:
    RewriteRule ^(.*)\.htm$ /$1.php [QSA,L]
    after this line:
    Code:
    RewriteRule \.(gif|jpg|png)$ - [F]
    should work.
    Thanks! I just put that in (haven't uploaded it yet; will do so when I upload the PHP version of my site).

    I forgot to say, though, reading the Islandnet docs, they have this information: that if you're using HTML pages, but naming them with .php extensions, you can put this line into your .htaccess:

    Code:
    AddHandler application/x-httpd-php .html .htm
    I did that, uploaded the .htaccess file that contained that line, and tried accessing my site, which still had all pages named *.htm. Well, the home page came up fine - without any navigation, haha! Nor masthead, and so forth; I do that all with SSI includes, and the includes didn't work. Minor matter; yeah, right! So I hastily put my old .htaccess file back. Then eerything worked, whew!

    But as soon as i've edited a few more pages, I'm going to put the new PHP version up to replace the .HTML version (*.htm), and we'll see how that goes.

    Maybe MisterNasty RobotSpammer can still write to my pages, but at least I have a way to upload the good pages to replace the bad, until I get a little .PHP script in there. For now, I hope to put in a kind of "holding" script that won't allow any guestbook entries, but *will* prevent (I hope?) somebody from writing to the page. I'll ask about that in the .PHP forum.

    Meantime, I think I'd better omit that AddHandler line from my .htocces file; does that sound right?

    Thanks!

    Sun, 18 Dec 2005 14:08:44
    My site is my home-and-playground
    http://www.coherentdog.org/
    Carol W

  4. #4
    Join Date
    Sep 2004
    Posts
    398
    You didn't get the navigation etc. when using the AddHandler line because SSI does not work in PHP files, you have to use PHP includes instead:
    PHP Code:
    <?php

    include($_SERVER['DOCUMENT_ROOT'] . '/absolute/path/to/file.ext');

    // or 

    include('relative/path/to/file.ext');

    ?>
    If you use the AddHandler line, you don't have to change the extension and the pages will become PHP anyway. You will still have to change the SSI to PHP includes either way you do it, though.

  5. #5
    Join Date
    Nov 2004
    Location
    Canada
    Posts
    330

    Exclamation You just saved my site from disaster!

    Dear Scleppel,

    Quote Originally Posted by Scleppel
    You didn't get the navigation etc. when using the AddHandler line because SSI does not work in PHP files, you have to use PHP includes instead:
    PHP Code:
    <?php

    include($_SERVER['DOCUMENT_ROOT'] . '/absolute/path/to/file.ext');

    // or 

    include('relative/path/to/file.ext');

    ?>
    Wow! You just saved my site from disaster, and me, from giant headaches!

    I'm using relative paths throughout the site, with one or two exceptions in subdirectories, where I link back to my home page in the main /www directory (I guess that's the www root).

    So, let me try a mockup here; I'll use what I think would be an actual example for my site, and if I get something wrong here, could you correct it?

    Say, for the main navigation (horizontal) on my home page:

    PHP Code:
    <?php
    include('include/mainnav.txt');
    ?>
    (and you're teaching me also, how to post PHP code!

    That is, I have

    /www/include

    where the /include subdirectory holds all my include files.

    Then something similar for my /www/hilar subdirectory, which holds my hilarious errors (from the past; I'm not guaranteeing whether I'll add present ones to those pages!); thus:

    (relative path; the included files for this set of pages are in /www/hilar/include):

    PHP Code:
    <?php
    include('include/hilarmainnav.txt');
    ?>
    Does that look right?


    The absolute one, I might have trouble with. I will need a few of those, I think.

    When you say

    PHP Code:
    include($_SERVER['DOCUMENT_ROOT'] . '/absolute/path/to/file.ext'); 
    what do I use as the ($_server['document-root'] part of that? would that be my /www directory? If so, would this example work?

    PHP Code:
    include($_YIKES!Server?['www'] . '/www/include/mainnav.txt'); 
    Nooooo; that is nothing like right - does "http.www.coherentdog.org" go in there somewhere?

    You are giving me a tiny bit of confidence; I really don't want to wreck my site and make it inaccessible; I have some steady visitors, but also, I'm not afraid to try, when I have help of the obvious quality you are offering!

    Quote Originally Posted by Scleppel
    If you use the AddHandler line, you don't have to change the extension and the pages will become PHP anyway. You will still have to change the SSI to PHP includes either way you do it, though.
    Oh, my goodness! So all I have to do is the massive, but not too difficult (depending on how cross-eyed I get) job of editing all the includes in all my pages. HAHAHA! Biiiig job. I can - and will - do it. I'd rather be clean about this! I think it just asks for trouble to do patchwork stuff. I've already spent lots and lots of time, often editing every page on the site - to try to keep things consistent and clean, and it's times like this that doing that pays off.

    I'll begin the editing in a few minutes, so I hope my examples are right! If not, I can always edit again; haha!

    Thanks so very, very much; you are a CoDogCarol Lifesaver!

    Sun, 18 Dec 2005 15:17:12
    My site is my home-and-playground
    http://www.coherentdog.org/
    Carol W

  6. #6
    Join Date
    Sep 2004
    Posts
    398
    If you are worried about messing up your site, i suggest you make a backup of the working files if you haven't already, then if you really mess it up, you can upload your backup.

    Your relative includes look OK.
    Quote Originally Posted by CarolW
    PHP Code:
    <?php
    include('include/mainnav.txt');
    ?>
    $_SERVER['DOCUMENT_ROOT'] will probably be "/www". It is the path from / (server root) to the folder you put your "first" index.htm page in.

    If the include page is www.your_domain.com/include/hilarmainnav.txt, then the absolute include would be
    PHP Code:
    <?php
    include($_SERVER['DOCUMENT_ROOT'] . '/include/hilarmainnav.txt');
    ?>

  7. #7
    Join Date
    Nov 2004
    Location
    Canada
    Posts
    330

    Question PHP includes (changing from SSI includes)

    Quote Originally Posted by Scleppel
    If you are worried about messing up your site, i suggest you make a backup of the working files if you haven't already, then if you really mess it up, you can upload your backup.

    Your relative includes look OK.


    $_SERVER['DOCUMENT_ROOT'] will probably be "/www". It is the path from / (server root) to the folder you put your "first" index.htm page in.
    Okay; I'm beginning to get the idea. I'm still confused, though, about what is literal here, and what is an indicator for what I should put in my pages as include statements.


    Quote Originally Posted by Scleppel
    If the include page is www.your_domain.com/include/hilarmainnav.txt, then the absolute include would be
    PHP Code:
    <?php
    include($_SERVER['DOCUMENT_ROOT'] . '/include/hilarmainnav.txt');
    ?>
    Going by what else you say; and adding that indeed, my root for my web site is named /www; would I do this:

    PHP Code:
    include($_SERVER['DOCUMENT_ROOT'] . '/include/hilarmainnav.txt');
    ?> 
    literally?

    Or would I do this:

    PHP Code:
    include($_www.coherentdog.org'] . '/include/hilarmainnav.txt');
    ?> 
    or this:

    PHP Code:
    include($_/www.coherentdog.org/'] . '/include/hilarmainnav.txt');
    ?> 
    I'll also go to the PHP page and have a look there, but it will take me months to begin to understand what I'm seeing there, so meantime, since I want to complete my conversion to PHP, and get my guestbooks back up if I can, I'll be grateful for anything I can just copy. Actually, I learn a lot by copying and then watching how my copy works - I've learned so much that way, from these forums.

    By the way, I'm paranoid about backing up my site; I keep replicas on my machine, except that I have no servers, so can't test anything that depends on server operations or on PHP operations, till I upload the files to my site.

    And I keep backups of the stuff before I made the changes, too, so I could upload my entire previous site, if need be! (And I burn to CD-Rs as well.)

    You are being so helpful; I can start editing soon! Thanks so much!

    Mon, 19 Dec 2005 08:29:41
    My site is my home-and-playground
    http://www.coherentdog.org/
    Carol W

  8. #8
    Join Date
    Sep 2004
    Posts
    398
    Sorry, you don't have to change $_SERVER['DOCUMENT_ROOT'], it is an indicator (variable) that holds the value of /www in your case. You would do this literally:
    PHP Code:
    <?php
    include($_SERVER['DOCUMENT_ROOT'] . '/include/hilarmainnav.txt');
    ?>
    You only have to change the /include/hilarmainnav.txt bit.

    It's good to know you make backups.

  9. #9
    Join Date
    Nov 2004
    Location
    Canada
    Posts
    330
    Quote Originally Posted by Scleppel
    Sorry, you don't have to change $_SERVER['DOCUMENT_ROOT'], it is an indicator (variable) that holds the value of /www in your case. You would do this literally:
    PHP Code:
    <?php
    include($_SERVER['DOCUMENT_ROOT'] . '/include/hilarmainnav.txt');
    ?>
    You only have to change the /include/hilarmainnav.txt bit.

    It's good to know you make backups.
    Great! Okay; I'll do that.

    Now I have another question, related.

    I have a couple of subdirectories under the /www directory (and for my own purposes, used a bad example there, because hilarmainnav.txt is in a subdirectory, /www/hilar )

    So, in my main /www directory, I'd do this:

    PHP Code:
    <?php
    include($_SERVER['DOCUMENT_ROOT'] . '/include/mainnav.txt');
    ?>
    because the included text files are in /www/include.

    But in my Hilarious Errors subdirectory, the directory structure goes like this:

    /www/hilar contains my web pages (now .htm, to be .php after I finish editing, and upload)

    /www/hilar/include holds my text file to be included.

    Therefore, in the include statements in my /www/hilar/*.php pages, I'd put for relative values (example):

    PHP Code:
    <?php
    include('include/mainnav.txt');
    ?>
    but if I were to use an absolute value for an include in the /www/hilar subdirectory, I'd use:

    PHP Code:
    <?php
    include($_SERVER['DOCUMENT_ROOT'] . '/include/hilar/hilarmainnav.txt');
    ?>
    Does that look right?

    Thanks so much for your infinite patience with such a newbie as I! I'm really looking forward now to making this change - and then struggling to learn some PHP coding! That will be another story altogether!

    Mon, 19 Dec 2005 09:21:38
    My site is my home-and-playground
    http://www.coherentdog.org/
    Carol W

  10. #10
    Join Date
    Sep 2004
    Posts
    398
    If it's "/www/hilar/include/file.ext" then it would be this:
    PHP Code:
    <?php
    include($_SERVER['DOCUMENT_ROOT'] . '/hilar/include/file.ext');
    ?>
    In your example, you had "include/hilar" instead of "hilar/include".

  11. #11
    Join Date
    Nov 2004
    Location
    Canada
    Posts
    330
    Quote Originally Posted by Scleppel
    If it's "/www/hilar/include/file.ext" then it would be this:
    PHP Code:
    <?php
    include($_SERVER['DOCUMENT_ROOT'] . '/hilar/include/file.ext');
    ?>
    In your example, you had "include/hilar" instead of "hilar/include".
    That would have been really cute, had I edited a whole bunch of files using MY version and then wondered what happend! Haha!

    Thanks so much. I think I understand enough now to go ahead with the editing. I'll report back when I get it done and try it out!

    Mon, 19 Dec 2005 09:52:33
    My site is my home-and-playground
    http://www.coherentdog.org/
    Carol W

  12. #12
    Join Date
    Sep 2005
    Posts
    3

    redirect all traffics to temporary index page

    well.. how do I redirect all traffics to temporary index page when I doing site maintains. Is there an easy way to redirect any temporarily non-existent links to the index page?

    can .htaccess can help me

  13. #13
    Join Date
    Sep 2004
    Posts
    398
    You really should have started you own thread for this.

    This will redirect EVERY request to /index.html:
    Code:
    RewriteEngine On
    
    # redirect anything that's not /index.html to /index.html
    RewriteRule !^index.html$ /index.html [L]
    Or, if you only want "temporarily non-existent links" to go to index.html, try this:
    Code:
    RewriteEngine On
    
    # redirect pages that don't exist to /index.html
    RewriteCond %{SCRIPT_FILENAME} !-f
    RewriteCond %{SCRIPT_FILENAME} !-d
    RewriteRule !^index.html$ /index.html [L]
    You can change index.html (all the red) to whatever maintainance page you want.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
HTML5 Development Center



Recent Articles