Page 2 of 2 FirstFirst 12
Results 11 to 19 of 19

Thread: .htaccess rewrite problem

  1. #11
    Join Date
    Jul 2010
    Location
    Minnesota
    Posts
    256
    Thanks
    1
    Thanked 21 Times in 21 Posts

    Default

    Windows 7 and wamp

  2. #12
    Join Date
    Jul 2008
    Location
    Derbyshire, UK
    Posts
    1,970
    Thanks
    16
    Thanked 307 Times in 305 Posts
    Blog Entries
    11

    Default

    If you want to side-step .htaccess altogether, you *could* set a global path variable in a common file.

    I do something similar to you (each website in a sub-folder) but in my main header.php file for each website ,I include a $myRoot variable - something like;
    Code:
    <?php $myRoot = 'http://localhost/mywebsite/'; ?>
    And then prefix the paths in my HTML markup with the variable, like this (all of my website pages are .php so this works for me);
    Code:
    <script src="<?php echo $myRoot;?>scripts/general.js"></script>
    The output will be: <script src="http://localhost/mywebsite/scripts/general.js"></script>

    Then when you move your website to the live server, you only change your $myRoot variable in the common file and the change filters down to all the paths in your markup;
    Code:
    <?php $myRoot = 'http://www.mywebsite.com/'; ?>
    OR just;
    Code:
    <?php $myRoot = '/'; ?>
    so the output would be; <script src="http://www.mywebsite.com/scripts/general.js"></script> or <script src="/scripts/general.js"></script>

    You wouldn't be able to do this in all cases but it *might* work for you.
    Focus on Function Web Design | Latest News RSS | Facebook | Twitter |
    Fast Edit (A flat file, PHP web page editor & CMS. Small, FREE, no database!) | Fast Edit BE (Snippet Manager) (Web content editor for multiple editable regions!) | Fast Apps |
    The only limit to creativity is imagination: JemCon.org

  3. #13
    Join Date
    Jul 2010
    Location
    Minnesota
    Posts
    256
    Thanks
    1
    Thanked 21 Times in 21 Posts

    Default

    I have thought of your concept to Bev. Am I correct when saying that the / does not work as expected for php with include() and require(), I seem to get a fatal error when using it on a live site. Does php look from the root already or what?

  4. #14
    Join Date
    Jul 2008
    Location
    Derbyshire, UK
    Posts
    1,970
    Thanks
    16
    Thanked 307 Times in 305 Posts
    Blog Entries
    11

    Default

    Yes, if you use "/" in a php include() its trying to look from a place even deeper than the root of your wamp-dwelling website - ie. the root of your C drive instead.

    But, you can create a variable for your php includes using the same idea though;

    Code:
    $myRootURL = 'http://localhost/mywebsite/';
    $myRootPATH = 'C:\wamp\www\mywebsite\';
    There are other ways to define a root but this is the one I use (old habits and all)

    EDIT: some other ways to define a root path for php includes: http://stackoverflow.com/questions/3...de-path-in-php
    Last edited by Beverleyh; 07-01-2013 at 05:15 PM. Reason: More ways to define root
    Focus on Function Web Design | Latest News RSS | Facebook | Twitter |
    Fast Edit (A flat file, PHP web page editor & CMS. Small, FREE, no database!) | Fast Edit BE (Snippet Manager) (Web content editor for multiple editable regions!) | Fast Apps |
    The only limit to creativity is imagination: JemCon.org

  5. #15
    Join Date
    Apr 2008
    Location
    So.Cal
    Posts
    3,643
    Thanks
    63
    Thanked 517 Times in 503 Posts
    Blog Entries
    5

    Default

    That's a great solution and totally workable/sufficient in many cases. I'd only suggest using constants instead of variables, so you can't accidentally modify/overwrite them (and they will always be available globally, regardless of scope):
    PHP Code:
    <?php

    define
    'ROOT_URL','/localhost/sites/example' );
    define'ROOT_PATH','C:\wamp\www\sites\example' );
    You could even do some switching logic so you wouldn't need to change them all manually when you uploaded to your server:
    PHP Code:
    <?php

    if( $_SERVER['SERVER_NAME'] === 'localhost' ){
        require 
    file/that/defines/local/constants.php;
    }
    else{
        require 
    file/that/defines/production/constants.php;
    }

  6. #16
    Join Date
    Jul 2008
    Location
    Derbyshire, UK
    Posts
    1,970
    Thanks
    16
    Thanked 307 Times in 305 Posts
    Blog Entries
    11

    Default

    Nice - I'll have to remember that switcharoo logic traq
    Focus on Function Web Design | Latest News RSS | Facebook | Twitter |
    Fast Edit (A flat file, PHP web page editor & CMS. Small, FREE, no database!) | Fast Edit BE (Snippet Manager) (Web content editor for multiple editable regions!) | Fast Apps |
    The only limit to creativity is imagination: JemCon.org

  7. #17
    Join Date
    Jul 2010
    Location
    Minnesota
    Posts
    256
    Thanks
    1
    Thanked 21 Times in 21 Posts

    Default

    Thanks for all the input. I have taken the time and converted my cms to use / on most links and image paths. It wasn't that terrible to do really. I made a few define() to help, although a couple of these I already had but mostly for localhost use. I have a few different versions so that I have options of when and where I need to use them depending on localhost or online and php includes or html links.
    PHP Code:
    // Strips the SERVER HOST to main domain name
    function stripHost()
    {
        if(
    substr($_SERVER['HTTP_HOST'], 04) == "www.")
        {
    $server substr($_SERVER['HTTP_HOST'], 4);}
        else {
    $server $_SERVER['HTTP_HOST'];}

        return 
    $server;
    }

    define("SITE_SERVER"$site_server stripHost());

    define("CURRENT_DIR", (dirname($_SERVER['PHP_SELF']) == '/') ? '/' dirname($_SERVER['PHP_SELF']).'/');

    define("BASE_URL", (SITE_SERVER == 'localhost') ? 'http://'.SITE_SERVER.CURRENT_DIR '/');

    define("ROOT"dirname(dirname(__FILE__)).'/'); 

  8. #18
    Join Date
    Jul 2010
    Location
    Minnesota
    Posts
    256
    Thanks
    1
    Thanked 21 Times in 21 Posts

    Default

    I also just figured out how to setup virtual host on my localhost, so that's really cool!
    You never know everything, I learn everyday!

  9. #19
    Join Date
    Apr 2008
    Location
    So.Cal
    Posts
    3,643
    Thanks
    63
    Thanked 517 Times in 503 Posts
    Blog Entries
    5

Similar Threads

  1. rewrite single url via htaccess
    By chetanmadaan in forum Other
    Replies: 0
    Last Post: 10-03-2011, 06:56 PM
  2. .htaccess rewrite url problem
    By robert_gsfame in forum Other
    Replies: 3
    Last Post: 08-27-2010, 05:07 AM
  3. .htaccess help to rewrite URLs
    By Beverleyh in forum Looking for such a script or service
    Replies: 3
    Last Post: 06-11-2009, 12:18 PM
  4. htaccess mod rewrite
    By vineet in forum PHP
    Replies: 1
    Last Post: 12-15-2008, 11:06 AM
  5. .htaccess Rewrite URL
    By thanhnguyen in forum Other
    Replies: 2
    Last Post: 05-09-2007, 02:45 AM

Bookmarks

Posting Permissions

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