Mod Rewrite

Need help installing PHP, configuring a script, or configuring a server? Then come on in and post your questions! We'll try to help the best we can!

Moderator: General Moderators

Post Reply
kkonline
Forum Contributor
Posts: 251
Joined: Thu Aug 16, 2007 12:54 am

Mod Rewrite

Post by kkonline »

Currently i have a site structure like http://www.example.com/news.php?cat=1&ID=53
Now i want it to look like
http://www.example.com/news/1/53 or similar

I have heard that this can be performed incase of Apache using modrewrite

My problem is that i have already mentioned http://www.example.com/news.php?cat=1&ID=53 in many scripts and cannot change it... or i don't want to. just want that when user goes to http://www.example.com/news.php?cat=1&ID=53 the url he views on browser be http://www.example.com/news/1/53

And if i want to make my own extension such as .kkonline then that to is possible without changing the links that already exist?

Is that possible? How? give me some tips on modrewrite too if any. Or where can i get easy touse tutorial for modrewrite (.htaccess file i guess has to be changed. Correct?)
miro_igov
Forum Contributor
Posts: 485
Joined: Fri Mar 31, 2006 5:06 am
Location: Bulgaria

Post by miro_igov »

You can use both http://www.example.com/news.php?cat=1&ID=53 and http://www.example.com/news/1/53

But if you have a lot of links with ?cat=1....etc...etc ad you do not want to change them what is the benefit of mod rewrite then?
User avatar
iknownothing
Forum Contributor
Posts: 337
Joined: Sun Dec 17, 2006 11:53 pm
Location: Sunshine Coast, Australia

Post by iknownothing »

This may be of interest.
User avatar
CoderGoblin
DevNet Resident
Posts: 1425
Joined: Tue Mar 16, 2004 10:03 am
Location: Aachen, Germany

Re: Mod Rewrite

Post by CoderGoblin »

Mod rewrite can use a form of regular expression to move things around. Unfortunately I don't have a link to any useful tutorials easily at hand.
kkonline wrote:cannot change it... or i don't want to.
I would guess that this is more a case of don't want to :wink: . Unfortunately this is not always a sensible decision. Surely it is often easier to correct something earlier than continue replicating things which may need changing at a later date or even worse building dependancies on other things to fudge over problems. The only real exception is when a site is running, you need a quick fix now and implement it but then you should still go back and solve the problem properly (with the approriate testing).

You could for instance set up a variable/constant for common links...

Code: Select all

// this is included into every php file where links are needed or defined as a constant
$news_page_link="http://www.example.com/news.php?cat=%d&ID=%d"; 
...
...
// When you need to output (could be in multiple locations/files)
printf($news_page_link,$catid,$id);
If you need to change the link for any reason you only have one place (the definition file) to change it. Of course this may be overkill in your scenario but is just one of a number of possibilities to make your life easier once an initial change is done.
User avatar
pickle
Briney Mod
Posts: 6445
Joined: Mon Jan 19, 2004 6:11 pm
Location: 53.01N x 112.48W
Contact:

Post by pickle »

Search the boards for mod_rewrite. Questions like this are asked weekly.
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
Post Reply