Solved

htaccess rewrite rule

Posted on 2010-11-18
7
300 Views
Last Modified: 2012-05-10
I have a fashion site and want to create seo friendly urls for all the brand pages. I have a dynamically generated central brand page which pulls all the active brands out of the database. The brand listings then link to the product.php page as follows:
<li><a href="product.php?BrandName=<?php echo $row_rsDesigners['BrandName']; ?>" target="_self"><?php echo $row_rsDesigners['BrandName']; ?></a></li>

If the brand is james-jeans, I want the url for that page to be simply james-jeans.php. I realise I need to rewrite my html link, but can someone give me the corrent htaccess code for achieving this bearing in mind that the items on the product page are filtered by the BrandName passed by the URL.

I just can't seem to get my head around this!! Thanks in advance.


0
Comment
Question by:DallySP
7 Comments
 
LVL 6

Assisted Solution

by:Ludwig Diehl
Ludwig Diehl earned 250 total points
ID: 34167111
Options +FollowSymlinks
RewriteEngine on
RewriteRule ^.+BrandName=([A-Za-z0-9\-\_\.]+).*$ http://$1.php [R,NC]


This will, in case it matches, rewrite ur url to BrandNames' value-

Lets say you have this url:   "product.php?BrandName=james-jeans"
then it should be redirected (rewritten) to  "http://james-jeans.php"
0
 
LVL 10

Accepted Solution

by:
TRW-Consulting earned 250 total points
ID: 34170548
Based on your sample, why even bother using rewrite?  Just have your product.php file force the redirect for you, and then you would have even more control, like using addition parameters, session variables, etc.

<?php
  if (isset($_GET['BrandName'])) {
    header("Location: /" . $_GET['BrandName'] . ".php");
  } else {
    header("Location: /default_location.php");
  }
?>
0
 

Author Comment

by:DallySP
ID: 34172065
Ok - thank you for both answers. Very helpful - although, I'm being terribly thick here - I still can't get either to work, probably because I'm writing the paths wrong. I like the idea of rewriting the headers as this site is already live and presumably this will save me having to rewrite all my html links? Although I would like to crack this htaccess thing too as its been a bit of a brick wall for a while now and would I be right in thinking its a bit more secure?
So to clarify the .htaccess method first. I want the link to direct to
http://www.sitename.com/james-jeans.php
Is this correct?
Rewrite my link as follows:
<li><a href="james-jeans.php" target="_self">James Jeans</a></li>

then in .htaccess - is this right?:
Options +FollowSymlinks
RewriteEngine on
RewriteRule ^.+BrandName=([A-Za-z0-9\-\_\.]+).*$ http://www.sitename.com/$1.php [R,NC]

I had been trying this previously but this doesn't work either!:
RewriteBase /
RewriteRule ^([A-Za-z0-9-]+)\/([A-Za-z0-9-]+)\.php$ product.php?BrandName=$1 [NC,L]


Secondly (and I'm not that familiar with re-writing headers - so go easy) - should this script work as is? I've been testing it but can't seem to get it to work. on my local host it goes to:
http://localhost/james-jeans.php which doesn't exist and on my server to my custom404 page.
Do I need any more backslashes or can I put the full path in and if so what would be the correct syntax.

It would be really great if I could get both these to work, so thanks again for your help.


0
Use Case: Protecting a Hybrid Cloud Infrastructure

Microsoft Azure is rapidly becoming the norm in dynamic IT environments. This document describes the challenges that organizations face when protecting data in a hybrid cloud IT environment and presents a use case to demonstrate how Acronis Backup protects all data.

 
LVL 6

Expert Comment

by:Ludwig Diehl
ID: 34174818
Hey. Have u should first check the following:

1. is mod_rewrite installed?
2. is it activated?
3. RewriteEngine on must be included in order tu use it.


Your example may not work because your base directory is "/" which means it points to localhost.
I think you should put this:

RewriteBase http://www.sitename.com/





0
 

Author Comment

by:DallySP
ID: 34174908
thanks for this - yes def. got all the mod rewrite installed and activated will try the rewritebase
0
 
LVL 16

Expert Comment

by:CWS (haripriya)
ID: 36045860
This question has been classified as abandoned and is closed as part of the Cleanup Program. See the recommendation for more details.
0

Featured Post

What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Author Note: Since this E-E article was originally written, years ago, formal testing has come into common use in the world of PHP.  PHPUnit (http://en.wikipedia.org/wiki/PHPUnit) and similar technologies have enjoyed wide adoption, making it possib…
This article discusses how to create an extensible mechanism for linked drop downs.
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

772 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question