[2 days left] What’s wrong with your cloud strategy? Learn why multicloud solutions matter with Nimble Storage.Register Now

x
?
Solved

htaccess rewrite rule

Posted on 2010-11-18
7
Medium Priority
?
307 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
Get your Conversational Ransomware Defense e‑book

This e-book gives you an insight into the ransomware threat and reviews the fundamentals of top-notch ransomware preparedness and recovery. To help you protect yourself and your organization. The initial infection may be inevitable, so the best protection is to be fully prepared.

 
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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Password hashing is better than message digests or encryption, and you should be using it instead of message digests or encryption.  Find out why and how in this article, which supplements the original article on PHP Client Registration, Login, Logo…
Introduction This article is intended for those who are new to PHP error handling (https://www.experts-exchange.com/articles/11769/And-by-the-way-I-am-New-to-PHP.html).  It addresses one of the most common problems that plague beginning PHP develop…
The viewer will learn how to count occurrences of each item in an array.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
Suggested Courses

656 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