Link to home
Start Free TrialLog in
Avatar of coolispaul
coolispaulFlag for United States of America

asked on

htaccess rewrite URL

Hi

I have a URL that ends  -
resort.php?resort_id= 8

How can i change it to read -
resort/name_of_resort

Thanks
ASKER CERTIFIED SOLUTION
Avatar of sivagnanam chandrakanth
sivagnanam chandrakanth
Flag of India image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of Marco Gasi
You can find your answer here: http://www.sitepoint.com/guide-url-rewriting-4/

The whole article is interestinbg about mod_rewrite and the fourth page tells about replaceing an id with a name. You have to write a txt file called let's say 'resortids.txt' where you list all possible ids of resorts

resort1             1 <br>
resort2             2 <br>
resortWakaWaka            18 <br>
resortHonolulu              35 <br>
...and so on

then write this in your .htaccess file

RewriteMap resorts txt:/path/to/file/resortids.txt    <br>  
 RewriteRule ^(.*)$ resort.php?resort_id=${resorts:$1|0}  

Hope this solves your problem

Cheers
Avatar of coolispaul

ASKER

Hi All

Im a little confused

am i correct in saying with this command:
RewriteRule ^resort-([0-9]+)\.php$ resort.php?resort_id=$1

it wont turn "resort.php?resort_id = 1" into
resort-1.php

instead i need my url to start "resort-1.php" and that will then understand it as if it was "resort.php?resort_id = 1" ?
also the rewrite map causes a server error in htaccess and i dont think you can use that in an htaccess file?

http://httpd.apache.org/docs/current/mod/mod_rewrite.html#rewritemap
RewriteMap can be used in htaccess. Probably there is some error in the path to the map file which causes a server error...
So do I actually have to change all my links now to how I want them to read? ie the htacess doesn't actually translate the url it just understands it?
Why are you saying that? What I mean is that maybe the server error is due to this line

RewriteMap resorts txt:/path/to/file/resortids.txt    <br>  

Maybe '/path/to/file/' contains something wrong: are you sure to have set the correct path to the map file?

Secondly, I understood you want the url displayed in the url bar of the browser were something like resort/name_of_resort, that is: your link is resort.php?resort_id= 8 and the server transform it to resort/name_of_resort. This is what I understood and this should be what you accomplish using map. But I might misunderstood your problem...
ok but forgetting Rewritema

Do i need to chanmge all my links on my website to be say:
<a href="/resort/1/">This is a resort</a>

instead of what it was originally:
<a href="resort.php?resort_id = 1">This is a resort</a>


It seems if i do this it works but i thought the htaccess would just transalet teh URL in the address bar automatically?
I thought that too: doesn't it do? Excuse me, but I'm not a ModRewrite guru (I'm not a guru at all): I tried to find a solution to your question because I remembered to having read something about, but I see the problem is far beyond my knowledge. Maybe hielo (mutch more skilled than me) can help you if you ask him to take a look to this question.

Good luck with your project

Marco