Solved

htaccess rewrite rule

Posted on 2010-11-18
7
304 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
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

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

If you are a web developer, you would be aware of the <iframe> tag in HTML. The <iframe> stands for inline frame and is used to embed another document within the current HTML document. The embedded document could be even another website.
Many old projects have bad code, but the budget doesn't exist to rewrite the codebase. You can update this code to be safer by introducing contemporary input validation, sanitation, and safer database queries.
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…
The viewer will learn how to count occurrences of each item in an array.

739 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