Solved

htaccess rewrite rule

Posted on 2010-11-18
7
294 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
Microsoft Certification Exam 74-409

Veeam® is happy to provide the Microsoft community with a study guide prepared by MVP and MCT, Orin Thomas. This guide will take you through each of the exam objectives, helping you to prepare for and pass the examination.

 
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

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

I imagine that there are some, like me, who require a way of getting currency exchange rates for implementation in web project from time to time, so I thought I would share a solution that I have developed for this purpose. It turns out that Yaho…
In Solr 4.0 it is possible to atomically (or partially) update individual fields in a document. This article will show the operations possible for atomic updating as well as setting up your Solr instance to be able to perform the actions. One major …
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

762 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

Need Help in Real-Time?

Connect with top rated Experts

23 Experts available now in Live!

Get 1:1 Help Now