Panagiotis
Panagiotis

Reputation: 1567

Rewrite rule with redirect in php

I seem to have problem with an .htaccess rewriterule.

What I want to do is a dispatcher of this form:

[website]/[parameter1-text]/[parameter2-text]

I have in my .htaccess the following:

RewriteRule ^(.*)/(.*)$ dispatcher.php?s1=$1&s2=$2

So far so good. But s1 and s2 are some variables that need to be matched in a database (mysql) and return the true variables that feed the script view.php in the following format:

view.php?category=[s1]&subcategory=[s2]&objects=5

How can I redirect this properly using header("Location: /view.php[...]"); and having the url unchanged (for example: http://example.com/CARS/BLUE http://example.com/SLIPPERS/RED/ )

Upvotes: 0

Views: 329

Answers (1)

anubhava
anubhava

Reputation: 785541

First of all fix some minor issues with your .htaccess:

RewriteRule ^([^/]+)/([^/]*)/?$ dispatcher.php?s1=$1&s2=$2 [L,QSA]

Now inside dispatcher.php do your Mysql stuff and fetch values of s1 and s2 from database. Once you're done and want to internally forward your request to: view.php?category=[s1]&subcategory=[s2]&objects=5 have this type of PHP code:

$_GET['category'] = $value-of-s1-from-DB;
$_REQUEST['category'] = $_GET['category'];
$_GET['subcategory'] = $value-of-s2-from-DB;
$_REQUEST['subcategory'] = $_GET['subcategory'];
$_GET['objects'] = '5';
$_REQUEST['objects'] = $_GET['objects'];
// now include view.php
// all your parameters are available in view.php in $_GET array
require_once("view.php");

Upvotes: 1

Related Questions