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
Share on other sites

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

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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]

 

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.