Path/of/my/url

Discussion in 'PHP' started by timallard, Aug 16, 2011.

  1. #1
    Hi All,

    I am making a php website and I would like my template to behave differently depending on the url structre. I believe this would be coded partly in my htaccess but not fully sure.

    I would like this to happen.

    path/of/my/url

    if they land on path/of the user gets one template
    if they land on path/of/my the user would get a different template

    these folders are not physical, it is all coming from the database.

    example...

    url.com/colors/blue

    If I land on /colors i would get a category listing of all colors. If I land on blue I get blue's details.


    Thanks!

    Tim
     
    timallard, Aug 16, 2011 IP
  2. MancuZ28

    MancuZ28 Greenhorn

    Messages:
    18
    Likes Received:
    0
    Best Answers:
    1
    Trophy Points:
    21
    #2
    I just did this in a project of mine. Here's my .htaccess:

    Options +FollowSymlinks
    RewriteEngine on

    #omit 'real' paths from re-write
    RewriteCond %{REQUEST_URI} "/css/" [OR]
    RewriteCond %{REQUEST_URI} "/images/"
    RewriteRule (.*) $1 [L]

    #re-write all other paths to the index script, passing path as parameter
    RewriteRule ^(.*)$ index.php?url=$1 [QSA,L]


    This will send everything except for the omitted directories to the index.php file, with a "url" query parameter containing the path. Then in your php file you can do this:

    
    $vars = array();
    $url = strtolower($_GET["url"]);
    $url = trim($url, "/");
    
    if($url != "" )   
       $vars = explode("/", $url);
    
    PHP:
    $vars is now an array of all the "subfolders" in the url. So, for instance www.yoursite.com/param1/param2, would create a variable array like this:

    $vars[0] = "param1"
    $vars[1] = "param2"

    Hope this helps!
     
    MancuZ28, Aug 16, 2011 IP
  3. MancuZ28

    MancuZ28 Greenhorn

    Messages:
    18
    Likes Received:
    0
    Best Answers:
    1
    Trophy Points:
    21
    #3
    I should add that your other option would be to go with a framework like CakePHP that does all this out of the box, but if you have already started your project, it may not be worth it to migrate to a framework.
     
    MancuZ28, Aug 16, 2011 IP
  4. timallard

    timallard Well-Known Member

    Messages:
    1,634
    Likes Received:
    62
    Best Answers:
    0
    Trophy Points:
    158
    #4
    Thank you so much for your help. I am ALMOST there.

    Here is my issue now. Lets say I have this URL: http://url.com/colors/blue

    I have it successfully pulling in the variables, but it breaks all my pathing to css an js etc. My urls to source scripts display correct, but when I click on them it adds the first path to the link.

    example:

    style.css displays as style.css in the code, but when i click on it, the redirect happes like this: http://url.com/colors/style.css.
    If I add another slash like url.com/colors/light/blue, it will still display correct in the paths but when i click on the source link in the view source it redirects to url.com/colors/light/style.css

    almost there...

    any ideas? I used the above example to create my htaccess.

    Thank you!
     
    timallard, Aug 17, 2011 IP
  5. MancuZ28

    MancuZ28 Greenhorn

    Messages:
    18
    Likes Received:
    0
    Best Answers:
    1
    Trophy Points:
    21
    #5
    Whare are your css and javascript files? Are they just at the webroot, or in a subfolder? The .htaccess rules below, which are designed to prevent the issue you are describing, assume that they are in /css and /js folders within the root directory. If they are somewhere else, you'll need to change the conditions to target them. You'll have to have one of these rules for every "real" path or file that the browser needs to access.

    RewriteCond %{REQUEST_URI} "/css/" [OR]
    RewriteCond %{REQUEST_URI} "/js/"
    RewriteRule (.*) $1 [L]

    Did that make sense?
     
    MancuZ28, Aug 17, 2011 IP
  6. ssmm987

    ssmm987 Member

    Messages:
    180
    Likes Received:
    4
    Best Answers:
    3
    Trophy Points:
    43
    #6
    Just use the full path. I.e. <script src="http://search.url.com/javascripts/prototype.js?1160335826" type="text/javascript"></script>
     
    ssmm987, Aug 17, 2011 IP
  7. timallard

    timallard Well-Known Member

    Messages:
    1,634
    Likes Received:
    62
    Best Answers:
    0
    Trophy Points:
    158
    #7
    Thanks MancuZ28,

    Yes that makes sense.
    I just realized, that it works, only if I have one sub folder AND the last slash is not present.

    example of what does not break my style and js:

    url.com/colors

    example of what does break my style and js:

    url.com/colors/
    url.com/colors/blue
    url.com/colors/blue/


    hmmmm
     
    timallard, Aug 17, 2011 IP
  8. timallard

    timallard Well-Known Member

    Messages:
    1,634
    Likes Received:
    62
    Best Answers:
    0
    Trophy Points:
    158
    #8
    Any ideas? Im going crazy and I know its something simple :/
     
    timallard, Aug 18, 2011 IP
  9. MancuZ28

    MancuZ28 Greenhorn

    Messages:
    18
    Likes Received:
    0
    Best Answers:
    1
    Trophy Points:
    21
    #9
    Can you let me know exactly where the javascript and css files are relative to your web root? I'm pretty sure the .htaccess conditions are just not targeting them properly.

    You can also try to reference them with the full URL as SSMM987 suggested.
     
    MancuZ28, Aug 18, 2011 IP
  10. MyVodaFone

    MyVodaFone Well-Known Member

    Messages:
    1,048
    Likes Received:
    42
    Best Answers:
    10
    Trophy Points:
    195
    #10
    I'm not quite following whats been said here, are you saying you have a different css style in each folder/url ?

    Try this :
    <link rel='stylesheet' type='text/css' href='<?php echo dirname(__FILE__);?>/css/style.css' />
    Code (markup):
    dirname(__FILE__) should follow the url until it reaches /css/style.css etc... the same applies to javascripts/ directory
     
    Last edited: Aug 18, 2011
    MyVodaFone, Aug 18, 2011 IP