Help:how To Do The Redirect

Hello guys,

Will you please help me to solve this problem. How to create a mod rewrite for this problem.

i have the following url

locate.php?a=numbers; //combination of numbers 0-9

Instead of that url, I made it search engine friendly,

the new url now is

location/numbers.html; //combination of numbers 0-9

I then create a mod re-write

RewriteEngine  on
RewriteRule ^location/([0-9-]*).html$  locate.php?a=$1 [L,NC]

The above mod rewrite work fine… but my problem is

When someone access directly the file

locate.php?a=numbers; //combination of numbers 0-9

How can I redirect someone else who open the locate.php to the search engine friendly url?

example locate.php?a=12 -> location/12.html

Thanks in advance

Hi ed!

Welcome to SitePoint!

First, please allow me to comment on your mod_rewrite code:

RewriteEngine  on
RewriteRule ^location/([0-9-]*).html$  locate.php?a=$1 [L,NC]

That allows location/.html so the first question is whether locate.php can handle a null entry.

The second question is whether it can handle location/-.html, too.

The third question is WHY you’re using the No Case flag on a {REQUEST_URI} variable. Please understand that it would cause LoCaTiOn/.HtMl to match and, while that may not cause a problem with $1 (that wasn’t mentioned above), it’s completely and totally inappropriate. Having said, that, though, if you ever try to match the {HTTP_HOST} variable which is NOT case sensitive, that’s where you (logically) MUST use the No Case flag.

My preferred version of your code would be:

RewriteEngine  on
RewriteRule ^location/([0-9-]+).html$  locate.php?a=$1 [L]


RewriteRule ^location/([0-9]+(-[0-9]+)*).html$  locate.php?a=$1 [L]

which would require at least one digit then zero or more -digit sets.

As for creating “loopy code” redirecting from the actual link to your NEW FORMAT then back to the actual link, I’ll let you read the tutorial article linked in my signature for the code. Of course, if you still have problems, post what you have and I’ll help you get to the solution.

Sorry, I do this to prevent having script kiddies come and ask for free coding … and to be sure SitePoint members actually learn something so they can help others in the future.