...

View Full Version : please advise on mod rewrite (i think)



LJackson
04-29-2010, 04:45 PM
Hi All,

not sure if this is mod rewrite or not? :)

i'm sure i've read somewhere that you can display urls in a different 'format' that how it is supposed to be?

for example i have a url which is something like

www.mywebsite.com/results.php?category=dvd&item=item_name&id=item_id

is it possible to have it display something like

www.mywebsite.com/dvd/item_name/item_id.php

basically i have thousands of 'virtual' pages which are all created depending on the product searched for so i dont actually have all these pages saved on the server as they are created from one file. so im not sure if it is possible to display /dvd/item_name/item_id.php because there is no such directory.

hope this makes sence?
cheers
Luke

Nightfire
04-29-2010, 05:14 PM
http://www.workingwith.me.uk/articles/scripting/mod_rewrite

might help. Only glanced at it but seemed to mention everything you need to know from what I saw

LJackson
04-29-2010, 05:19 PM
Hi mate,

so it is called mod_rewrite :D sweet, i wasn't sure...

that looks exactly like what i am looking for thanks so much

Luke

LJackson
04-30-2010, 12:42 AM
ok i have spent a while researching mod_rewrite and have found what i need to do but it doesnt seem to be working :(

i have my url as it is now

http://www.mywebsite.com/price_finder2.php?store=The%20Simpsons:%20Greatest%20Hits

and i want it to be

http://www.mywebsite.com/The%20Simpsons:%20Greatest%20Hits.php

so i have added this to my .htaccess file

RewriteEngine on
RewriteRule ^([^/\.]+).php$ price_finder2.php?store=$1 [L]

but nothing has changed?

have i done it right?
thanks

Luke

_Aerospace_Eng_
04-30-2010, 02:31 PM
What do you mean nothing has changed? If you go to this url: http://www.mywebsite.com/The%20Simpsons:%20Greatest%20Hits.php does it work? What you won't automatically direct users to the new url if that is what you were wanting.

LJackson
04-30-2010, 02:42 PM
Hi Mate,

ok i obviously dont understand this mod_rewrite lark :D.

lets say i have a list of products on a page each with a link to the price_results2.php page, but also the name and category are being passed in the url so the page knows which info to display so here is one of those urls

http://www.mysite.com/price_finder2.php?store=The%20Hurt%20Locker

now what i am trying to acheive is when the user clicks on the link for this item the url should show http://www.mysite.com/The%20Hurt%20Locker.php

but with my current code it doesnt it still shows the original url?

hope that explains it a bit better? not sure why its not changing the url though.

here is the tutorial i followed

5. Description – Your actual URL is http://example.com/index.php?page=hello which you want to see as http://example.com/hello.htm
Solution – Place the following lines in your .htaccess file

RewriteEngine on
RewriteRule ^([^/\.]+).htm$ index.php?page=$1 [L]

cheers
Luke

LJackson
05-01-2010, 12:28 AM
still unable to solve this. All the tutorials i've followed dont seem to work :(

anyone have any ideas please?
thanks

MattF
05-01-2010, 01:45 PM
still unable to solve this. All the tutorials i've followed dont seem to work :(

anyone have any ideas please?
thanks

The links you see in the output generated by any scripts will remain as they always were, unless you rewrite any local, (from your servers viewpoint), links to the new format. The htaccess side of things is only interpreting those links from the perspective of an incoming request from a client.The links in your page source require a PHP solution. You need to bollocks the links up via some function or suchlike before you output the page source to the client.


Edit: What reason are you wanting this for, btw? Is it because you do like the other link format or is it for some supposed S.E.O benefit, (of which there is none).

LJackson
05-01-2010, 07:32 PM
Hi Mate, thanks for your reply,

just think the url looks much cleaner doing it this way, im sure the user would much prefere to see a url like

www.mywebsite.com/dvd/the_hurt_locker.php

instead of

www.mywebsite.com/price_results2.php?category=dvd&product=the_hurt_locker

then users would then beable to just change the film name to what ever and it should load up that page? i see many sites including twitter, and facebook to name a few that do it. so are they using some php script? or going via the .htaccess route? sorry still dont really understand it totally :(

many thanks
Luke

MattF
05-01-2010, 07:54 PM
www.kernow-connect.com/dvd/the_hurt_locker.php

instead of

www.kernow-connect.com/price_results2.php?category=dvd&product=the_hurt_locker

then users would then beable to just change the film name to what ever and it should load up that page? i see many sites including twitter, and facebook to name a few that do it. so are they using some php script? or going via the .htaccess route? sorry still dont really understand it totally :(

Pretty much what happens is that the first link above doesn't actually work other than in a virtual sense. You need to rewrite your scripts, somehow, to output that link though, even though it leads to nothing as such. That's where the htaccess rewrite rules come in. When someone uses that link, it rewrites it internally to point to the correct link, which is the second link above. So, the user sees the format in the first link but is actually using the second link, (the valid one where your scripts are concerned), as far as the server is concerned.

LJackson
05-01-2010, 08:40 PM
ok i understand what you mean now :D

is it a difficult job to do?
i know of a php script which i might beable to edit for my needs to fix the virtual urls but i have no idea about the htaccess rewrite. a nice little job for me to start :D

thanks for the help and info mate
appreciate it

Luke

MattF
05-01-2010, 09:10 PM
The hardest part is changing the link structure in your scripts. If it has a templating engine, you have a central point for changing them. If not, depends on how your software is structured. The rewriting part is merely regex matching, so should be the easiest part. If it's an off the shelf type system you're using, I'd check to see if there's already a URL prettifying, (the person who coined that term ought be shot, honestly), mod out there for it.

LJackson
05-05-2010, 04:50 PM
Hi Mate,

ive spent a bit of time chaniging the format of my links and i have it setup so that it will send users to a virtual url e.g


http://www.kernow-connect.com/dvd/the_hurt_locker.php

which doesnt exist but the url which is actually valid is this


http://www.kernow-connect.com/price_finder2.php?category=dvd&product=The%20Hurt%20Locker

but im not sure how to setup the mod rewrite so that all virtual urls are linked to a url like above.

not sure how it works but there are thousands of products which could be requested and each one needs to redirect to the valid url ???

again not sure how this all works mate, sorry
Cheers
Luke

MattF
05-05-2010, 05:07 PM
Regex.

LJackson
05-05-2010, 05:54 PM
hi mate, unfortunatly that is something i know nothing about :( have looked on google but find it very difficult to follow :(

i have read a tutorial type thing which gives this code to put into htaccess


RewriteEngine On

RewriteCond %{SCRIPT_FILENAME} !-d
RewriteCond %{SCRIPT_FILENAME} !-f

RewriteRule ^users/(\d+)*$ ./profile.php?id=$1
RewriteRule ^threads/(\d+)*$ ./price_finder2.php?id=$1

RewriteRule ^search/(.*)$ ./search.php?query=$1

i have tried changing it but i dont really know what im doing?

any ideas please
thanks
Luke

LJackson
05-06-2010, 01:11 AM
ok im getting somewhere :D

found a generator online and came up with this

RewriteEngine On
RewriteRule ^([^/]*)/([^/]*)$ /price_finder2.php?category=$1&product=$2 [L]

and now when i type in this for example
http://www.kernow-connect.com/dvd/the%20hurt%20locker doesnt currently work removed the rerite code as it disables all css...

it loads up a page with the correct data but there is no css formatting throughout the whole site???, what do i have to do get this to work?

thanks
Luke

_Aerospace_Eng_
05-06-2010, 04:38 PM
Give us the url to one that actually loads a page. Its likely the CSS path is wrong. Try using an absolute path e.g. / start from the root.

LJackson
05-06-2010, 05:14 PM
hi mate,

the above link now works, but still no css?

here are my css, js links

<link rel="stylesheet" type="text/css" href="/autosuggest/css/style.css" />
<link rel="stylesheet" type="text/css" href="/css/rating.css" />
<link rel="stylesheet" type="text/css" href="/css/newcss.css" />

<script type="text/javascript" src="/autosuggest/js/jquery.js"></script>
<script type="text/javascript" src="/autosuggest/js/autosuggest_script.js"></script>


have tried using the full urls too e,g


<link rel="stylesheet" type="text/css" href="http://www.kernow-connect.com/css/newcss.css" />


also its effecting my whole site? which to me seems odd?

thanks for your help
Luke

LJackson
05-07-2010, 02:31 PM
ok have sorted it :D

had to add these lines to my htaccess

RewriteCond %{SCRIPT_FILENAME} !-d
RewriteCond %{SCRIPT_FILENAME} !-f

then add a / to all my links and it works. so chuffed :)

cheers
Luke



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum