?
Solved

htaccess rewrite rule

Posted on 2010-11-18
7
Medium Priority
?
306 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 1000 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 1000 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
Optimum High-Definition Video Viewing and Control

The ATEN VM0404HA 4x4 4K HDMI Matrix Switch supports 4K resolutions of UHD (3840 x 2160) and DCI (4096 x 2160) with refresh rates of 30 Hz (4:4:4) and 60 Hz (4:2:0). It is ideal for applications where the routing of 4K digital signals is required.

 
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

Part of the Global Positioning System A geocode (https://developers.google.com/maps/documentation/geocoding/) is the major subset of a GPS coordinate (http://en.wikipedia.org/wiki/Global_Positioning_System), the other parts being the altitude and t…
This article discusses how to implement server side field validation and display customized error messages to the client.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
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.
Suggested Courses

770 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