Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I've got a CAKEPHP app which has the following rewrite rules in the .htaccess in the webroot folder.

<IfModule mod_rewrite.c> 
RewriteEngine On RewriteCond %{HTTP_HOST} !^domain.com$ [NC] 
RewriteRule ^(.*)$ domain.com/$1 [L,R=301] 
RewriteCond %{REQUEST_FILENAME} !-d 
RewriteCond %{REQUEST_FILENAME} !-f 
RewriteRule ^(.*)$ index.php?url=$1 [QSA,L] 
</IfModule>

I have never worked with CakePHP before and am a PHP beginner, and have written a standalone php script which I have in a folder named 'products' in the webroot folder. My index page in this folder takes in an id and then retrieves the product name and description from the DB. (There are hundreds of products)

So: www.domain.com/products/index.php?id=1

I wish to include a rule where by the URL should look like: www.domain.com/products/the-flux-capacitor

Please can someone tell me how I can achieve this without destroying any of the CakePHP rewrites?

share|improve this question
    
why dont you write that code in the cakephp application?? why do you need a php file in the webroot?? –  pleasedontbelong Dec 27 '11 at 22:15
    
Thanks for the reply. I haven't tried because I have never used Cake before and I don't want to risk touching the code in case it all goes crazy –  jIMBOBOB Dec 27 '11 at 22:20
    
seems to me that it would be less risky if you learn the basics of Cakephp and include your code in a controller inside cake.. :) and seems to me that it'll be easier. Cakephp is quite simple –  pleasedontbelong Dec 27 '11 at 22:34
    
I don't want to risk touching the code in case it all goes crazy - so back it up first then... –  DaveRandom Dec 27 '11 at 22:37
add comment

1 Answer

up vote 0 down vote accepted

If you use the following URL structure www.domain.com/products/the-flux-capacitor you still need a way to map every product name, which must be unique, to its id, and it will be a larger effort.

However, if you use a slightly modified URL structure, www.domain.com/products/the-flux-capacitor/id (even Amazon does this), then it is much easier and the following .htaccess code will do the rewrite for you

#existing rules...
RewriteRule ^(.*)$ domain.com/$1 [L,R=301] 

#Insert new rule to match products, assumes ID is numeric
RewriteRule ^products/[^/]+/([0-9]+) products/index.php?id=$1 [L,QSA,NC]

#existing rules ...
RewriteCond %{REQUEST_FILENAME} !-d 
share|improve this answer
    
Hi, thanks for the response. My id is alpha numeric, so should I use: RewriteRule ^products/[^/]+/([A-z0-9]+) products/index.php?id=$1 [L,QSA,NC]? Also, how will the rule know how to use 'the-flux-capacitor' in the URL? –  jIMBOBOB Dec 28 '11 at 11:44
    
If its alpha numeric use [a-zA-Z0-9]. I am assuming that your HTML already has links of the form www.domain.com/products/the-flux-capacitor/id, or that you will construct these in your application. The rule will merely map this link to your index.php, ignoring the product description and only extracting the id. –  Ulrich Palha Dec 28 '11 at 14:42
    
Thank you Ulrich –  jIMBOBOB Dec 28 '11 at 16:45
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.