Jump to content

now i want to rewrite a friendly url


deansaddigh

Recommended Posts

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

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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]

Link to comment
Share on other sites

Wow, very very very helpful thank you very much.

its nice of you to take time out to help me.

 

Kind regards

 

Dean :D

 

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

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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;

 

 

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

 

Link to comment
Share on other sites

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

 

Link to comment
Share on other sites

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

 

Link to comment
Share on other sites

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>';

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.