deansaddigh Posted September 1, 2010 Share Posted September 1, 2010 Ok so now i want to change this monstrosity http://www.languageschoolsuk.com/school_details.php?id=164 to something url friendly like http://www.languageschoolsuk.com/schools/then the school name here. can someone show me how this is done. as it stands my htaccess file just re-writes. php files to.htm. like so RewriteEngine on RewriteRule ^(.*)\.htm$ $1.php [nc] Kind regards Dean Quote Link to comment https://forums.phpfreaks.com/topic/212267-now-i-want-to-rewrite-a-friendly-url/ Share on other sites More sharing options...
wildteen88 Posted September 1, 2010 Share Posted September 1, 2010 If you want your url to be site.com/schools/school_name/ Then the rewriterule will be rewriteRule school/([a-z_-]+)/? school_details.php?id=$1 This will then map site.com/schools/school_name to site.com/school_details.php?id=school_name However you may need to alter school_details.php so it grabs the school from your databased based on the schools name rather than the schools id. Quote Link to comment https://forums.phpfreaks.com/topic/212267-now-i-want-to-rewrite-a-friendly-url/#findComment-1106039 Share on other sites More sharing options...
deansaddigh Posted September 1, 2010 Author Share Posted September 1, 2010 Hey , thanks again for helping out, first question, can i have 2 re-write rules like so, RewriteEngine on RewriteRule ^(.*)\.htm$ $1.php [nc] RewriteRule school/([a-z_-]+)/? school_details.php?id=$1 Quote Link to comment https://forums.phpfreaks.com/topic/212267-now-i-want-to-rewrite-a-friendly-url/#findComment-1106046 Share on other sites More sharing options...
deansaddigh Posted September 1, 2010 Author Share Posted September 1, 2010 Am i right in thinking aswell i need to change the url , of the querystring which was originally this. <a href="school_details.php?id='.$name.'"> to match the htacess RewriteRule school/([a-z_-]+)/? school_details.php?id=$1 <a href="school_details/school/school_details.php?id='.$name.'"> also i dont want the schools part i just want the link to look like . http://www.languageschoolsuk.com/school_details/schoolname Quote Link to comment https://forums.phpfreaks.com/topic/212267-now-i-want-to-rewrite-a-friendly-url/#findComment-1106065 Share on other sites More sharing options...
wildteen88 Posted September 1, 2010 Share Posted September 1, 2010 Hey , thanks again for helping out, first question, can i have 2 re-write rules like so Yes you can have as many RewriteRules as you like. When having many rewriteRules add the [L] flag after each one. RewriteEngine on RewriteRule ^(.*)\.htm$ $1.php [NC, L] RewriteRule school/([a-z_-]+)/? school_details.php?id=$1 [L] Am i right in thinking aswell i need to change the url , of the querystring which was originally this. <a href="school_details.php?id='.$name.'"> to match the htacess Yes you will need to change your links to <a href="school_details/'.$name.'"> also i dont want the schools part i just want the link to look like . http://www.languageschoolsuk.com/school_details/schoolname Change the rewriteRule to RewriteRule school_details/([a-z_-]+)/? school_details.php?id=$1 [L] Quote Link to comment https://forums.phpfreaks.com/topic/212267-now-i-want-to-rewrite-a-friendly-url/#findComment-1106096 Share on other sites More sharing options...
deansaddigh Posted September 1, 2010 Author Share Posted September 1, 2010 Wow, very very very helpful thank you very much. its nice of you to take time out to help me. Kind regards Dean Im getting an internal server error with this RewriteEngine on RewriteRule ^(.*)\.htm$ $1.php [NC, L] RewriteRule school_details/([a-z_-]+)/? school_details.php?id=$1 [L] Do you know why at all Quote Link to comment https://forums.phpfreaks.com/topic/212267-now-i-want-to-rewrite-a-friendly-url/#findComment-1106121 Share on other sites More sharing options...
deansaddigh Posted September 1, 2010 Author Share Posted September 1, 2010 I think i figured it out, i beleive the problem was [NC, L] ive just changed it to [L] Also i have put ^ on this line RewriteRule ^school_details/([a-z_-]+)/? school_details.php?id=$1 [L] I think you may have just forgot it, after reading a tutorial, can you clarify if this is correct. Kind regards Dean Quote Link to comment https://forums.phpfreaks.com/topic/212267-now-i-want-to-rewrite-a-friendly-url/#findComment-1106144 Share on other sites More sharing options...
wildteen88 Posted September 1, 2010 Share Posted September 1, 2010 The ^ just tells mod_rewrite to match from the start of the url. It wont make any difference with or without it. The reason you got the error before was because of the space between NC, and L. NC stands for No Case (which mean make the match case-insensitive). L stands for Last rule. Have a read of the Apache Documentation on mod_rewrite. It will help you to understand how mod_rewrite functions. For learning regex (regula expressions) you'll find regular-expressions.info a good resource. Quote Link to comment https://forums.phpfreaks.com/topic/212267-now-i-want-to-rewrite-a-friendly-url/#findComment-1106150 Share on other sites More sharing options...
deansaddigh Posted September 1, 2010 Author Share Posted September 1, 2010 Thank you very much, im reading as we speak. My lord its a tad confusing, Thanks again Quote Link to comment https://forums.phpfreaks.com/topic/212267-now-i-want-to-rewrite-a-friendly-url/#findComment-1106161 Share on other sites More sharing options...
deansaddigh Posted September 1, 2010 Author Share Posted September 1, 2010 Ok, so now im getting a page not found error. Ive done abit of reading and am getting to grips more with htaccess. Soooo. im getting an error Not Found The requested URL /school_details/Bath Academy was not found on this server. Im gonna list my code on pages to see if anyone can just point me in the right direction. first the htacess file. ive added a couple of things that stop hotlinking and compress php files so just ignore them. RewriteEngine on #change mask php files RewriteRule ^(.*)\.htm$ $1.php [NC,L] #seo friendly url RewriteRule ^school_details/([a-z_-]+)/? school_details.php?id=$1 [L] #prevent hotlinking RewriteCond %{HTTP_REFERER} !^http://(.+\.)?languageschoolsuk\.com/ [NC] RewriteCond %{HTTP_REFERER} !^$ RewriteRule .*\.(jpe?g|gif|bmp|png)$ http://img251.imageshack.us/img251/4224/logovg.png [L] #compress php files to save bandwidth <ifModule mod_php4.c> php_value zlib.output_compression 16386 </ifModule> view all school.php which passes the name of the school through the url as appose to the id like suggested. $school_id = $row['school_id']; $name = $row['name']; echo '<h2 class ="schoolname"><a href="school_details/'.$name.'">'.$row['name'].' (click for details)</h2></a>'; Which passes the name to the school_details.php And the school_details page. //get the school id $school_id = $_GET['id']; // select school details based on school id $query = "SELECT image_school.image_id, image_school.school_id, school.name AS school_name, school.street, school.town, school.city, school.county, school.region, school.school_facts, school.general_info, school.accreditations, school.garden, school.computer_lab, school.language_lab, school.internet_access, school.self_study_center, school.student_lounge, school.disabled_access, school.bookshop , school.disabled_access_ac , school.library , school.cafecanteen, school.video, image.image_id, image.path, image.name AS image_name FROM image_school JOIN school ON image_school.school_id = school.school_id JOIN image ON image_school.image_id = image.image_id WHERE school.school_id =" .$school_id; Quote Link to comment https://forums.phpfreaks.com/topic/212267-now-i-want-to-rewrite-a-friendly-url/#findComment-1106179 Share on other sites More sharing options...
wildteen88 Posted September 1, 2010 Share Posted September 1, 2010 Ideally it is best to not have any spaces within your urls. When you are creating the link for the school $name = $row['name']; echo '<h2 class ="schoolname"><a href="school_details/'.$name.'">'.$row['name'].' (click for details)</h2></a>'; You should convert the spaces within the school name to underscores $name = str_replace(' ', '_', $row['name']); echo '<h2 class ="schoolname"><a href="school_details/'.$name.'">'.$row['name'].' (click for details)</h2></a>'; Now you urls will be like site.com/school_details/Bath_Academy Now. In school_details.php when use $_GET['id'] you'll need replace the underscores back to spaces. Like so $school_name = str_replace('_', ' ', $_GET['id'] You will also need to change this part of your query WHERE school.school_id =" .$school_id; to WHERE school.name = '$school_id'"; As you are now passing the school name to the query and not the schools id. Quote Link to comment https://forums.phpfreaks.com/topic/212267-now-i-want-to-rewrite-a-friendly-url/#findComment-1106181 Share on other sites More sharing options...
deansaddigh Posted September 1, 2010 Author Share Posted September 1, 2010 Firstly , thanks for how much help you have given i know i keep saying it, but its invaluable. I understand everything you are saying. with regards to what you just wrote. Its nearly there now. If you could kindly check http://www.languageschoolsuk.com/view_all_schools.htm then click a school name youll be taken to the page and the url is perfect. woop woop however, a couple of things one the details are being populated for the school and 2 it looks like the style sheet is missing. On a quick note ,in the db the school names have spaces in them e.g Kings Colleges Bournemouth Is this one of the problems. Quote Link to comment https://forums.phpfreaks.com/topic/212267-now-i-want-to-rewrite-a-friendly-url/#findComment-1106185 Share on other sites More sharing options...
deansaddigh Posted September 1, 2010 Author Share Posted September 1, 2010 hey, ill go from here, thanks very much man Quote Link to comment https://forums.phpfreaks.com/topic/212267-now-i-want-to-rewrite-a-friendly-url/#findComment-1106195 Share on other sites More sharing options...
deansaddigh Posted September 2, 2010 Author Share Posted September 2, 2010 Sorry to be a pin, again been trying to fix something. Basically say i select bath acadamy from the view all schools page, Well the variable only comes through as bah and it forgets the acadamy part, i assume because my htacess files needs tweaking to look for a space. So obviously my sql query doesnt find anything and the variable doesnt match anythingi n the db. you can check this by going here. http://www.languageschoolsuk.com/view_all_schools.htm select bath acadamy. Now on the next page half way down you can see i have echoed out the variable and only bath is in it. So i assume my htacess file is slightly off. #seo friendly url RewriteRule ^school_details/([A-Z][a-z]+)/? school_details.php?id=$1 [L] Can anyone show me whats im doing wrong? Kind regards Dean Quote Link to comment https://forums.phpfreaks.com/topic/212267-now-i-want-to-rewrite-a-friendly-url/#findComment-1106364 Share on other sites More sharing options...
deansaddigh Posted September 2, 2010 Author Share Posted September 2, 2010 Ok so ive got the whole name now i slightly altered my htacess file by adding an _ on ([a-zA-Z_]+) RewriteRule ^school_details/([a-zA-Z_]+)/? school_details.php?id=$1 [L] Quote Link to comment https://forums.phpfreaks.com/topic/212267-now-i-want-to-rewrite-a-friendly-url/#findComment-1106375 Share on other sites More sharing options...
deansaddigh Posted September 2, 2010 Author Share Posted September 2, 2010 Woo hoo ive done it. Ok so last last quiestion. why is none of my styling working. Can anyone shed some light please Quote Link to comment https://forums.phpfreaks.com/topic/212267-now-i-want-to-rewrite-a-friendly-url/#findComment-1106383 Share on other sites More sharing options...
wildteen88 Posted September 2, 2010 Share Posted September 2, 2010 Change this <link href="style.css" rel="stylesheet" type="text/css" media="screen" /> to <link href="/style.css" rel="stylesheet" type="text/css" media="screen" /> When the url is http://www.languageschoolsuk.com/school_details/Bath_Academy the web browser is trying to load the stylesheet in http://www.languageschoolsuk.com/school_details/style.css. It is thinking school_details/ is a directory which we know it isn't. Adding the / in front of style.css tells the browser to load the file from the root of the url eg http://www.languageschoolsuk.com/style.css Quote Link to comment https://forums.phpfreaks.com/topic/212267-now-i-want-to-rewrite-a-friendly-url/#findComment-1106412 Share on other sites More sharing options...
deansaddigh Posted September 2, 2010 Author Share Posted September 2, 2010 Ahhh i see, so what would i do with my links, along the top because they are doing the same thing and they are on an include file Quote Link to comment https://forums.phpfreaks.com/topic/212267-now-i-want-to-rewrite-a-friendly-url/#findComment-1106416 Share on other sites More sharing options...
wildteen88 Posted September 2, 2010 Share Posted September 2, 2010 You will want to begin all your links with a / too. The same applies to your images etc. Quote Link to comment https://forums.phpfreaks.com/topic/212267-now-i-want-to-rewrite-a-friendly-url/#findComment-1106418 Share on other sites More sharing options...
deansaddigh Posted September 2, 2010 Author Share Posted September 2, 2010 So every image and every link on the whole site? Quote Link to comment https://forums.phpfreaks.com/topic/212267-now-i-want-to-rewrite-a-friendly-url/#findComment-1106419 Share on other sites More sharing options...
wildteen88 Posted September 2, 2010 Share Posted September 2, 2010 You should only need to apply this to the links/images in school_details.php. However there is no harm in doing this for all the links on your sites. Quote Link to comment https://forums.phpfreaks.com/topic/212267-now-i-want-to-rewrite-a-friendly-url/#findComment-1106422 Share on other sites More sharing options...
deansaddigh Posted September 2, 2010 Author Share Posted September 2, 2010 Ok ive done that, and theres one last problem, my image for the school isnt displaying and i cant figure out where to put the / heres the bit of code. while($copiedrow = mysql_fetch_array($copiedresult)) { $imagename = $copiedrow ['image_name']; $image = 'admin/'.$copiedrow['path'] . '/' . $copiedrow ['image_name']; echo '<li><img ref="'.$image.'" src="thumb-circle-active.png"/><span><a href="http://www.pikachoose.com">'.$imagename.'</a></span></li>'; Quote Link to comment https://forums.phpfreaks.com/topic/212267-now-i-want-to-rewrite-a-friendly-url/#findComment-1106425 Share on other sites More sharing options...
wildteen88 Posted September 2, 2010 Share Posted September 2, 2010 Add / before thumb-circle-active.png Quote Link to comment https://forums.phpfreaks.com/topic/212267-now-i-want-to-rewrite-a-friendly-url/#findComment-1106461 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.