Jump to content

.htaccess ReWrite with PHP


sblake161189

Recommended Posts

Hi Guys,

 

I no its only a partial PHP question but its still related to PHP...

 

I have a URL  .com/sites/0.php?Ter=144      and it takes you to a page and loads details with the id (ter) equalling 144 and displays all of 144's information... normal PHP stuff  :)

 

However I want to be able to put a Rewrite rule using a .htaccess (unless there is a better method!?) to get search engine frienly results.

 

However I dont want the user to type  .com/144 to bring up the Leeds branch. I want them to type .com/leeds and it bring up 144 (Leeds Branch).

 

BranchName is the field I use for the branch name in my MySQL table.

 

Is this possible? Also when I enter    .com/sites/0.php?BranchName=Leeds    that doesnt work either...

 

Cheers, Sam

Link to comment
https://forums.phpfreaks.com/topic/228713-htaccess-rewrite-with-php/
Share on other sites

Get the PHP working first.  The problem is that on 0.php you are querying for Ter, something like:

 

WHERE id = $_GET['Ter']

 

But you need to query on BranchName, something like:

 

WHERE branch_name = $_GET['BranchName']

Thanks for that :)

 

My PHP script at the top of my page (0.php) is as follows:

 

<?php
include('../admin/config.php'); 
if (isset($_GET['Ter']) ) { 
$ter = (int) $_GET['Ter'];
$row = mysql_fetch_array ( mysql_query("SELECT * FROM `ter` WHERE `Ter` = '$ter' "));
?>

 

Can I say in PHP: SELECT * FROM `ter` WHERE `Ter` = '$ter' or `BranchName` = '$branchname'

 

Does that make sense?

 

Cheers

Ignore my last post...

 

I have now changed it so it works off BranchName

 

<?php
include('../admin/config.php'); 
if (isset($_GET['BranchName'])) { 
$ter = (int) $_GET['Ter'];
$branchname = $_GET['BranchName'];
$row = mysql_fetch_array ( mysql_query("SELECT * FROM `ter` WHERE `BranchName` = '$branchname' "));
?>

 

So any clues on how to modify the .htaccess so the user can type .com/leeds and it takes you to .com/sites/0.php?BranchName=Leeds but remains as .com/leeds in the address bar? :)

 

Thanks, Sam

This isn't very flexible because everything will be considered a branch name which is probably not all your site does or will do (not tested, but something like):

 

RewriteRule ^(.*)$ /sites/0.php?BranchName=$1 [L]

 

It would be better to do something like:

 

RewriteRule ^BranchName/(.*)$ /sites/0.php?BranchName=$1 [L]

 

Cheers fella,

 

That kind of on the right lines of what I'm looking far so thanks :)

 

RewriteRule ^BranchName/(.*)$ /sites/0.php?BranchName=$1 [L]

 

But that means I have to type  .com/BranchName/leeds    for it come up

 

Any way of modifying it so its just  .com/leeds  :)

 

Cheers

 

I have also just tried

RewriteRule ^(.*)$ /sites/0.php?BranchName=$1 [L]

 

and I got a 404 error for .com/leeds

 

hmmmm

Cheers fella,

 

That kind of on the right lines of what I'm looking far so thanks :)

 

RewriteRule ^BranchName/(.*)$ /sites/0.php?BranchName=$1 [L]

 

But that means I have to type  .com/BranchName/leeds    for it come up

 

Any way of modifying it so its just  .com/leeds  :)

 

Cheers

 

Yes, the first example, but like I said, if your site also has other functionality other than just displaing branches, like mortgages.php or something, then the first rule will rewrite it into:

 

/sites/0.php?BranchName=mortgages.php

 

So it is most likely problematic.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.