Lovac Posted July 1, 2010 Share Posted July 1, 2010 Hello all, i have website with dyn url for example www.domain.com/index.php?str=home www.domain.com/index.php?str=about www.domain.com/index.php?str=gallery i want to rewrite like this: www.domain.com/home/ www.domain.com/about/ www.domain.com/gallery/ and this .htaccess should give that but its not working now i created this .htaccess: Options +FollowSymLinks RewriteEngine On RewriteRule ^([a-zA-Z0-9_-]+)$ index.php?str=$1 RewriteRule ^([a-zA-Z0-9_-]+)/$ index.php?str=$1 i have htaccess for password protect in that folder, so i know .htaccess is working site is in ftp folder like this: /public_html/projects/testsite chmode: 755 im doing something wrong or bad htaccess rewrite? please help Quote Link to comment https://forums.phpfreaks.com/topic/206382-htaccess-rewrite-dont-work/ Share on other sites More sharing options...
cags Posted July 1, 2010 Share Posted July 1, 2010 Define not working. Not doing anything noticable, 404 server error, 500 server error, shows wrong page, something else? How are you testing it? Are you typing in (or clicking on an address) such as www.domain.com/index.php?str=home and expecting it to magically change? Given a request for: www.domain.com/home/ And the following .htaccess: Options +FollowSymLinks RewriteEngine On RewriteRule ^([a-zA-Z0-9_-]+)/?$ index.php?str=$1 You should see the contents of * **: www.domain.com/index.php?str=home *If you receive a '500 Internal Error' then you probably don't have mod_rewrite enabled. **If you receive a '404 Page Not Found' error, then you probably want a forward slash directly before index.php. Quote Link to comment https://forums.phpfreaks.com/topic/206382-htaccess-rewrite-dont-work/#findComment-1079713 Share on other sites More sharing options...
Lovac Posted July 1, 2010 Author Share Posted July 1, 2010 i have menu that generates links like these <?php //check selected page is correct name format if(preg_match('/^[\-_a-z0-9]+$/i', $page)){ $file = "$page.html"; //check page exists if(file_exists($file)){ include($file); exit; } } //any faults display home page include "home.html"; function PageLinks($page, $pages){ $str = ""; foreach($pages as $k => $v){ if($page == $k){ $str .="<li class=\"selected\"><a href=\"index.php?str=$k\">$v</a></li >\n"; }else{ $str .="<li class=\"unselected\"><a href=\"index.php?str=$k\">$v</a></li >\n"; } } return $str; } ?> and navigation <?php //Full page list $pages = array( home => "Home", services => "Services", ); //Get Page selected $page = (empty($_GET['str']))?"home":$_GET['str']; //Display links echo PageLinks($page,$pages); ?> i removed some links cuz they were in my native language nothing happens when i click on menu bar, 2nd code generates it and change page, cuz its one index.php file and changes content, also changes <body id="<?php echo $page ?>"> body id. What should i do to make this to work? nothing happens aka links stays the same, no 404 or 500 errors Quote Link to comment https://forums.phpfreaks.com/topic/206382-htaccess-rewrite-dont-work/#findComment-1079732 Share on other sites More sharing options...
cags Posted July 1, 2010 Share Posted July 1, 2010 You are miss-understand what mod_rewrite does. Type www.domain.com/home/ in your address bar, if you see the page loading then your mod_rewrite is working. You should change the links in your site to point at that address. Quote Link to comment https://forums.phpfreaks.com/topic/206382-htaccess-rewrite-dont-work/#findComment-1079734 Share on other sites More sharing options...
Lovac Posted July 1, 2010 Author Share Posted July 1, 2010 thx alot, i understand now www.domain.com/about works , when typing /about/ it doesnt work, doesent show site propely now i need to figure how to rewrite php to generate links like i want to thx again Quote Link to comment https://forums.phpfreaks.com/topic/206382-htaccess-rewrite-dont-work/#findComment-1079746 Share on other sites More sharing options...
cags Posted July 1, 2010 Share Posted July 1, 2010 In your OP you have two rules, one without a trailing slash and one with. You should note that in my first post I used Regex to make the forward slash optional (that's what the question mark means in that situation). If you use that rule then either should work. Quote Link to comment https://forums.phpfreaks.com/topic/206382-htaccess-rewrite-dont-work/#findComment-1079753 Share on other sites More sharing options...
Lovac Posted July 1, 2010 Author Share Posted July 1, 2010 ok i have made it not sure why when using www.domain.com/about its all ok and when using www.domain.com/about/ only text shows up without any image, on source when its /about/ shows everything identical to one when its loaded propely Quote Link to comment https://forums.phpfreaks.com/topic/206382-htaccess-rewrite-dont-work/#findComment-1079770 Share on other sites More sharing options...
cags Posted July 1, 2010 Share Posted July 1, 2010 That is because with the trailing slash you are essentially in a different 'directory', so relative URLs will not work, therefore if your links for things like CSS, JS and Images are something like 'images/header.jpg' or 'css/style.css' then the browser will look in the wrong location as it will be trying to look in '/about/images/header.jpg' instead of '/images/header.jpg'. The solution is to make all links to external files absolute paths or make them relative to root (begin with a forward slash). Quote Link to comment https://forums.phpfreaks.com/topic/206382-htaccess-rewrite-dont-work/#findComment-1079800 Share on other sites More sharing options...
Lovac Posted July 1, 2010 Author Share Posted July 1, 2010 ok, thanks alot m8 rly appriciate Quote Link to comment https://forums.phpfreaks.com/topic/206382-htaccess-rewrite-dont-work/#findComment-1079803 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.