Page 1 of 1

Mod Rewrite

Posted: Tue Aug 21, 2007 2:46 am
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?)

Posted: Tue Aug 21, 2007 9:07 am
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?

Posted: Tue Aug 21, 2007 9:13 am
by iknownothing
This may be of interest.

Re: Mod Rewrite

Posted: Tue Aug 21, 2007 9:26 am
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.

Posted: Tue Aug 21, 2007 10:10 am
by pickle
Search the boards for mod_rewrite. Questions like this are asked weekly.