Little help with my .htaccess

Discussion in 'Apache' started by risoknop, Nov 30, 2008.

  1. #1
    This is a pretty simple problem, unfortunately I don't understand mod_rewrite syntax so I am unable to solve it.

    My current .htaccess looks like this:

    
    RewriteEngine on
    
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    
    RewriteRule ^(.*)$ index.php?route=$1 [L,QSA]
    
    Code (markup):
    Basically, it is just googled solution that rewrites all arguments after index.php.

    For example:

    
    http://www.example.com/index.php?route=blog/index
    
    Code (markup):
    Wil be rewrited to:

    
    http://www.example.com/blog/index
    
    Code (markup):
    Now my problem: I need to exclude a folder 'public' from rewriting because all my stylesheets an js scripts are located in that folder and I cannot access them.

    Please help.
     
    risoknop, Nov 30, 2008 IP
  2. risoknop

    risoknop Peon

    Messages:
    914
    Likes Received:
    24
    Best Answers:
    0
    Trophy Points:
    0
    #2
    To be more precise:

    CSS works at pages like:

    www.example.com
    Code (markup):
    www.example.com/blog
    Code (markup):
    However it doesn't work when action is specified, e.g.:

    www.example.com/blog/action
    Code (markup):
    will load without CSS styles.
     
    risoknop, Nov 30, 2008 IP
  3. Exa

    Exa Active Member

    Messages:
    471
    Likes Received:
    8
    Best Answers:
    0
    Trophy Points:
    85
    #3
    RewriteEngine On
    RewriteRule ^(.*)/(.*)$ index.php?route=$1/$2
    
    Code (markup):
    The CSS won't work because of the relative file path you set. If your CSS file is located at www.example.com/style.css, you will have to call it (in the index.php file):

    <link rel=stylesheet href="/style.css" type="text/css" media=screen>

    Because at www.example.com/blog/action, if you call the stylesheet with "<link rel=stylesheet href="style.css" type="text/css" media=screen>", it will attempt to find it at http://www.example.com/blog/action/style.css and not http://www.example.com/style.css

    It doesn't matter that the "real" file, index.php, is located in the correct folder, because with the mod_rewrite rules, the file is now thought to be in a subdirectory, and the relative paths have to be altered accordingly.

    Sorry, I can't really make myself clear :eek:
     
    Exa, Nov 30, 2008 IP
  4. risoknop

    risoknop Peon

    Messages:
    914
    Likes Received:
    24
    Best Answers:
    0
    Trophy Points:
    0
    #4
    Hey thanks for help.

    I have actually solved it by using HTML <base> tag:

    <base href="<?php echo $baseURL; ?>" />
    Code (markup):
    The $baseURL is defined in PHP as:

    
    $baseURL = substr_replace('http://'.$_SERVER['HTTP_HOST'].$_SERVER['PHP_SELF'], '', -9, 9).'public/';
    
    PHP:
    That is the only thing that worked (substr_replace is there to remove the index.php).

    Is it good solution?
     
    risoknop, Dec 2, 2008 IP
  5. Exa

    Exa Active Member

    Messages:
    471
    Likes Received:
    8
    Best Answers:
    0
    Trophy Points:
    85
    #5
    Not really; you're just complicating things.

    <base href="http://www.example.com" /> would have sufficed. But personally, I'd just used the method I posted above.
     
    Exa, Dec 2, 2008 IP
  6. risoknop

    risoknop Peon

    Messages:
    914
    Likes Received:
    24
    Best Answers:
    0
    Trophy Points:
    0
    #6
    I know it would have sufficed. But I'm building a simple PHP framework (just for my own purposes) so I want it to work well on different websites. And I don't want to edit the URL each time I upload the framework on different domain.

    That's why I went with the simple PHP script mentioned above.
     
    risoknop, Dec 3, 2008 IP
  7. risoknop

    risoknop Peon

    Messages:
    914
    Likes Received:
    24
    Best Answers:
    0
    Trophy Points:
    0
    #7
    For some weird reason it doesn't work for me :|

    But the base tag works.
     
    risoknop, Dec 3, 2008 IP
  8. Exa

    Exa Active Member

    Messages:
    471
    Likes Received:
    8
    Best Answers:
    0
    Trophy Points:
    85
    #8
    
    <?PHP
        echo '<base href="'.$_SERVER['HTTP_HOST'].'" />';
    ?>
    
    PHP:
    This should do then ;)
     
    Exa, Dec 3, 2008 IP
  9. risoknop

    risoknop Peon

    Messages:
    914
    Likes Received:
    24
    Best Answers:
    0
    Trophy Points:
    0
    #9
    That would be enough most of the time but my version will work also if the whole website is in a subdirectory.
     
    risoknop, Dec 4, 2008 IP