PHP/ Apache: URL rewrite

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
lauthiamkok
Forum Contributor
Posts: 153
Joined: Wed Apr 01, 2009 2:23 pm
Location: Plymouth, United Kingdom

PHP/ Apache: URL rewrite

Post by lauthiamkok »

Hi,
How can I rewrite the URL from this,
mysite.com/index.php?pg=news&current_page=1

to,
mysite.com/news?page=1


I have tried with this, but it doesnt work - something I have missed or it is written wrong completely?

RewriteEngine on

Code: Select all

RewriteRule 
^news?page=([a-zA-Z0-9\-]+)/?$  index.php?pg=news&current_page=$1 [QSA]
Thanks if you know how to correct it...

Cheers,
Lau
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: PHP/ Apache: URL rewrite

Post by requinix »

Make your index.php use page= instead of current_page=, then

Code: Select all

RewriteRule ^/?news index.php?pg=news [L,QSA]
The QSA means the page=1 will carry across to the rewritten URL.

By the way, a common solution to using a page number is something like /news/1. Then

Code: Select all

RewriteRule ^/?news(/(\d+))? index.php?pg=news&page=$2 [L]
lauthiamkok
Forum Contributor
Posts: 153
Joined: Wed Apr 01, 2009 2:23 pm
Location: Plymouth, United Kingdom

Re: PHP/ Apache: URL rewrite

Post by lauthiamkok »

tasairis wrote:Make your index.php use page= instead of current_page=, then

Code: Select all

RewriteRule ^/?news index.php?pg=news [L,QSA]
The QSA means the page=1 will carry across to the rewritten URL.

By the way, a common solution to using a page number is something like /news/1. Then

Code: Select all

RewriteRule ^/?news(/(\d+))? index.php?pg=news&page=$2 [L]
thank you. i works now when i use page=
thanks :D
Post Reply