Help with Mod Rewrite

Hi !

I am stuck at a situation, and have been trying hard, but nothing worked.

Let see if some on can help me here ASAP.

Ok, there is the scene.

I have a site, and I want the urls to be SEO friendly. There are 3 scripts, like

show_location.php
show_bank.php
show_branch.php

The problem is that, in all these, the inputs are all text and like each other.

so if I have :

Options +FollowSymLinks
RewriteEngine on

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([-a-zA-Z-0-9.,+'()]+)$ show_br.php?branch=$1 [L]
RewriteRule ^([-a-zA-Z-0-9.,+'()]+)$ show_bank.php?bank=$1 [L]
RewriteRule ^([-a-zA-Z-0-9.,+'()]+)$ show_location.php?location=$1 [L]

its not working.
Please support me with this !

Thanks
Zeeshan

Someone please reply !

You need to rewrite all requests to one PHP scripts that detects what the input is and reacts accordingly. Example (pseudo code)


if (input is a branch name)
{
   bankname=input;
   include("show_br.php");
}
(etc)

means it can not be done via .htaccess file ?

Yes, it can be done via a .htaccess file.

If you create a htaccess file as follows:


RewriteEngine On
RewriteRule ^([-a-zA-Z-0-9.,+'()]+)$ distribute_request.php?request=$1

And then you create a distribute_request.php as follows:


if ($_GET['request'] is a branch name)
{
  $_GET['branch'] = $_GET['request'];
  include('show_br.php');
}
else if ($_GET['request'] is a bank name)
{
  // ...
}
// etc, etc, etc

Where of course ‘if ($_GET[‘request’] is a branch name)’ should be replaced by application logic that checks if the given $_GET[‘request’] is a branch name. Possibly by searching for the name in your database or some other way of how you set this up.

Scallio is correct with the VERY LIMITED information you’ve provided. There is no way for Apache to GUESS which of the three scripts you want to redirect to!

Moreover, because your regex contains ', it’s liable to BREAK the mod_rewrite because the ’ is an illegal character within a URL (see Uniform Resource Identifiers (URI): Generic Syntax.

mod_rewrite CAN use the same “smarts” that you can build into your “guess.php” script if - and ONLY IF - you have access to the httpd.conf so that you can use a RewriteMap. I suspect this is not the case (it generally isn’t).

What you need to do (in order to use mod_rewrite - or just to simplify the problem for your “guess” script) is to change your URI format, i.e., use branch/{whatever}, bank/{whatever} or location/{whatever} and change your regex accordingly to differentiate the redirections.

In other words, the whole “trick” to mod_rewrite (or any coding situation) is to write down the intent of your code (I call it my “specification”) so you can see the problems (three cases without differentiation, in your case) and deal with those before dealing with the coding.

Regards,

DK

@ScallioXTX

Thanks for your reply and suggestion that will really help me a lot. Thanks for your time.