TalkPHP
 
 
Account Login
Latest Articles
» The basic usage of PHPTAL, a XML/XHTML template library for PHP
» Vulnerable methods and the areas they are commonly trusted in.
» Simple way to protect a form from bot
» The Basics On: How Session Stealing Works
» How to keep your forms from double posting data
IRC Channel
IRC Speech Bubble Join the friendly bunch on IRC...
(#TalkPHP on Freenode)

...Also available via a web interface.

See this thread for information on the TalkPHP Free Hugs Initiative™. Subject to availability.
Associates
Associates
CSS Tutorials
Reply
 
LinkBack Thread Tools Search this Thread Display Modes
Old 10-10-2008, 03:05 AM   #1 (permalink)
The Addict
 
sarmenhb's Avatar
 
Join Date: Jan 2008
Location: los angeles
Posts: 309
Thanks: 44
sarmenhb is on a distinguished road
Default need some mod_rewrite help

i read a few tutorials on mod_rewrite but im still stumped.

what i'm wondering is if i have something like this

Code:
cart.php?m=product_list&c=7
how would i write this? to put like /products/the-products-name (where 7 is the products title)

this is what i've tried so far

Code:
RewriteEngine On
^products$/the-products-name cart.php?m=product_list&c=$1
ps: another thing that confuses me is once i have written the rules in .htaccess will the rules automatically take effect? or do i have to go back to the code and rewrite all the links such as wherever it calls ?m=product_list&c i have to rewrite it with /products/$tblrow ? i just hope its not like that but let me know :) thanks alot
__________________
no signature set
sarmenhb is offline  
Reply With Quote
Old 10-10-2008, 05:54 AM   #2 (permalink)
The Frequenter
 
ReSpawN's Avatar
 
Join Date: Nov 2007
Location: Netherlands
Posts: 460
Thanks: 49
ReSpawN is on a distinguished road
Default

Perhaps some one could write a tutorial on this one, because I still suck at writing htaccess.
__________________
"Life is a bitch, take that bitch on a ride"
Send a message via MSN to ReSpawN
ReSpawN is offline  
Reply With Quote
Old 10-10-2008, 10:45 AM   #3 (permalink)
The Frequenter
Newcomer 
 
xenon's Avatar
 
Join Date: Dec 2007
Location: Bucharest, Romania
Posts: 438
Thanks: 3
xenon is on a distinguished road
Default

Well, first of all, $1, $2, ..., $n are references to the previously captured groups (stuff between paranthesis, in the order they were created). Second of all, in regex, ^ delimits the beginning of a regular expression, whereas $ represents the end of it. Thirdly, you need to tell apache that it needs to do a URL rewrite, by prefixing rewrite rules with "RewriteRule". So, your rule is wrong right from the beginning of the second line. Here's the correct rule:

Code:
RewriteRule  ^products/([0-9]+)$ cart.php?m=product_list&c=$1 [QSA]
Mod_Rewrite Forums :: Index

Now, about the rules effect. Well, they take effect in the moment you save your .htaccess file. Meaning that you will be able to access http://somesite.com/products/7 after you add that rule to your .htaccess. However, it will not replace your current URLs with the rewritten ones. How should apache know which ones should be left intact and which ones should be rewritten? So yes, you need to manually change all URLs you are rewriting. That's why you have to think about URL rewriting (whether you will ever need it or not) BEFORE the actual coding, so you don't have to do the same thing twice.
__________________
I have optimistic thoughts, even though sometimes (if not always) life's a bitch.
xenon is offline  
Reply With Quote
The Following User Says Thank You to xenon For This Useful Post:
sarmenhb (10-10-2008)
Old 10-10-2008, 10:49 AM   #4 (permalink)
The Frequenter
 
ReSpawN's Avatar
 
Join Date: Nov 2007
Location: Netherlands
Posts: 460
Thanks: 49
ReSpawN is on a distinguished road
Default

The URL provided has been spammed by a so called pr0nbot and I can't any normal topic anywhere.

Yet, some good advise. I just followed a fairly large tutorial on RegEx and it should apply fine to htaccess mod_rewrites, right? Xenon is the master of RegEx is what it looks like.
__________________
"Life is a bitch, take that bitch on a ride"
Send a message via MSN to ReSpawN
ReSpawN is offline  
Reply With Quote
Old 10-10-2008, 03:59 PM   #5 (permalink)
The Addict
 
sarmenhb's Avatar
 
Join Date: Jan 2008
Location: los angeles
Posts: 309
Thanks: 44
sarmenhb is on a distinguished road
Default

thanks xenon! u rock.

these are the rules i wrote based on the cart that i'm using
which can be found at digiSHOP :: Shopping Cart Software & Ecommerce :: Products
i have to figure out how to change the urls in the application lol thats gonna take forever but if you can please review the rules that i wrote and see if there correct before i impplement it that would be great :)

Code:
RewriteEngine On
RewriteRule  ^Products/([0-9]+)$ cart.php?m=product_list&c=$1 [QSA]
RewriteRule ^Details/([0-9]+)$ cart.php?m=product_detail&p=$1 [QSA]
RewriteRule ^Login/$ secure/login.php?m=client_login [QSA]
RewriteRule ^Cart/$ cart.php?m=view
RewriteRule ^Login/$ secure/login.php
RewriteRule ^Search/$/([a-z][A-Z][0-9]+)/([0-9]+)/([0-9]+) cart.php?m=search_results&search=$1&x=$2&y=$3
RewriteRule ^Account/$ secure/account.php
RewriteRule ^GiftCertificate/$ cart.php?m=gift_certificates
RewriteRule Search/$ cart.php?m=search
RewriteRule ^Featured/$ featured.php
RewriteRule ^Billing/$ checkout.php?m=bill
RewriteRule ^Payment/$ secure/checkout.php?m=payment
RewriteRule ^Checkout/$ secure/checkout.php
RewriteRule ^OrderSubmited/$ secure/orderSubmitted.php
RewriteRule ^Account/$ secure/account.php
RewriteRule ^History$/ secure/account.php?m=order_history

# the oNum value is displayed in this format 1223652380-305
#
RewriteRule ^OrderDetails/([0-9])\-([0-9]+)/$ secure/account.php?m=order_detail&oNum=$1
RewriteRule ^Billing/$ secure/account.php?m=change_billing
RewriteRule ^Shipping/$ secure/acount.php?m=change_shipping
RewriteRule ^ChangePassword/$ secure/account.php?m=change_password
RewriteRule ^Login/$ secure/login.php?m=client_login
let me know what you think,
thanks again.
ps: how do i know what flags to put?
__________________
no signature set
sarmenhb is offline  
Reply With Quote
Old 10-10-2008, 04:48 PM   #6 (permalink)
The Prestige
Upcoming Programmer Inquisitive 
 
Tanax's Avatar
 
Join Date: Sep 2007
Location: Sweden, Stockholm
Posts: 1,080
Thanks: 115
Tanax is on a distinguished road
Default

What's the [QSA] for, in the end of the row??
__________________
Tanax is offline  
Reply With Quote
Old 10-10-2008, 05:40 PM   #7 (permalink)
The Addict
 
sarmenhb's Avatar
 
Join Date: Jan 2008
Location: los angeles
Posts: 309
Thanks: 44
sarmenhb is on a distinguished road
Default

Quote:
Originally Posted by Tanax View Post
What's the [QSA] for, in the end of the row??
their called flags each letter in the bracket has a purpose. the purpose is what i'm trying to figure out lol.
__________________
no signature set
sarmenhb is offline  
Reply With Quote
Old 10-10-2008, 06:04 PM   #8 (permalink)
Moderateur
RegEx Guru PHP Guru Top Contributor Advanced Programmer 
 
Salathe's Avatar
 
Join Date: Apr 2007
Posts: 1,393
Thanks: 5
Salathe is on a distinguished road
Default

The QSA flag stands for "query-string append". The query string (if any) of the request will be tacked onto the end of the rewritten URL if you use this flag.
Salathe is offline  
Reply With Quote
Old 10-10-2008, 06:35 PM   #9 (permalink)
The Prestige
Upcoming Programmer Inquisitive 
 
Tanax's Avatar
 
Join Date: Sep 2007
Location: Sweden, Stockholm
Posts: 1,080
Thanks: 115
Tanax is on a distinguished road
Default

Ye, I found explenation on mod_rewrite forum..

[QSA] stands for Query String Append. This means the 'query string' (stuff after the ?) should be passed from the original URL (the one we are rewriting) to the new URL.
__________________
Tanax is offline  
Reply With Quote
Old 10-11-2008, 01:00 AM   #10 (permalink)
The Addict
 
sarmenhb's Avatar
 
Join Date: Jan 2008
Location: los angeles
Posts: 309
Thanks: 44
sarmenhb is on a distinguished road
Default

so can someone see if i did the rules correctly or if any fixes needed? thanks
__________________
no signature set
sarmenhb is offline  
Reply With Quote
Old 10-11-2008, 08:47 AM   #11 (permalink)
The Frequenter
Newcomer 
 
xenon's Avatar
 
Join Date: Dec 2007
Location: Bucharest, Romania
Posts: 438
Thanks: 3
xenon is on a distinguished road
Default

You forgot the regex begin tag on this line:

Code:
RewriteRule Search/$ cart.php?m=search
Other than that, they look fine...you may also want to add a [L] after each rule, so that other rules following that line will not be processed (if you have another directive at the end of the rule -- like QSA -- that becomes [QSA,L]). Also, you might want to put the lines in the order of their usage, for apache to work less, therefore increasing the loading speed of your pages. But these are just minor tweaks.
__________________
I have optimistic thoughts, even though sometimes (if not always) life's a bitch.
xenon is offline  
Reply With Quote
Reply



Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


All times are GMT. The time now is 07:15 PM.

 
     

Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0
Inactive Reminders By Icora Web Design