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
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!
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.
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!
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?
Just use the full path. I.e. <script src="http://search.url.com/javascripts/prototype.js?1160335826" type="text/javascript"></script>
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
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.
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