rewrite problem with query string

This is where you share YOUR scripts with others

Moderators: egami, macek, gesf

Post Reply
User avatar
New php-forum User
New php-forum User
Posts: 156
Joined: Wed Mar 22, 2006 2:22 am
Location: Ahmedabad, Gujarat, India

Thu Nov 12, 2009 4:07 am


I am creating an application with rewirte using .htaccess file.

The rule I have applied is as below
RewriteRule ^(.*)-(.*)\.html$ $1.php?page=$2 [L]

The rule, works like

news-1.html then it will redirect to news.php?page=1

Upto this it works fine.

But when I pass the query string variables like news-1.html?year=2009


its not giving me year param and its value in $_REQUEST['year']

Can anybody help me?

Mukesh Variya
Offshore Web Developer India

New php-forum User
New php-forum User
Posts: 92
Joined: Tue Aug 17, 2010 2:00 am
Location: Ukraine Kharkov

Mon Aug 23, 2010 11:44 pm

You need to understand regExps because mod_rewrite based on PREL regexps.
Here you say to script that he need to search all url which starts with any number of symbols after them it is "-" and after it again the set of symbols at the end of script:

Code: Select all

RewriteRule ^(.*)-(.*)\.html$ $1.php?page=$2 [L]
So your new url news-1.html?year=2009 is parsed by mod_rewrite like:

Code: Select all

news-1.html?year=2009 ->  news.php?page=1
because you said to script that html$ html is the end.
You need something like:

Code: Select all

RewriteRule ^(.*)-(.*)\.html\?(.*)$ $1.php?page=$2&$3 [L]
RewriteRule ^(.*)-(.*)\.html$ $1.php?page=$2 [L]

Post Reply