TalkPHP

TalkPHP (http://www.talkphp.com/forums.php)
-   Advanced PHP Programming (http://www.talkphp.com/advanced-php-programming/)
-   -   need some mod_rewrite help (http://www.talkphp.com/advanced-php-programming/3466-need-some-mod_rewrite-help.html)

sarmenhb 10-10-2008 03:05 AM

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

ReSpawN 10-10-2008 05:54 AM

Perhaps some one could write a tutorial on this one, because I still suck at writing htaccess.

xenon 10-10-2008 10:45 AM

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.

ReSpawN 10-10-2008 10:49 AM

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

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. :-P

sarmenhb 10-10-2008 03:59 PM

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?

Tanax 10-10-2008 04:48 PM

What's the [QSA] for, in the end of the row??

sarmenhb 10-10-2008 05:40 PM

Quote:

Originally Posted by Tanax (Post 18774)
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.

Salathe 10-10-2008 06:04 PM

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.

Tanax 10-10-2008 06:35 PM

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.

sarmenhb 10-11-2008 01:00 AM

so can someone see if i did the rules correctly or if any fixes needed? thanks

xenon 10-11-2008 08:47 AM

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.


All times are GMT. The time now is 02:39 PM.

Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.1.0