zander1983 Posted April 1, 2011 Share Posted April 1, 2011 Hi Im setting up a website where people can start their own shops and the url is www.mydomain.com/shops/shop_name Now i have a simple mod_rewrite rule which directs urls above to the shop_details.php page. What i need to do is dynamically create new rules because when a shop adds a product, it will look like www.mydomain.com/shops/shop_name/product_name In cases like this, is it correct to edit the .htaccess file on the fly using File Write and add a new rule to the file. Then when a user hits the url www.mydomain.com/shops/shop_name/product_name, the new rule will recognise the shop and product and redirect to product_details.php. I think this method will work, Im just wondering is this the normal/correct way to go about solving a problem like this.... thanks in advance Quote Link to comment https://forums.phpfreaks.com/topic/232401-dynamically-rewrite-urls/ Share on other sites More sharing options...
j9sjam3 Posted April 1, 2011 Share Posted April 1, 2011 The .htaccess file should never (or very rarely) change. As long as your are rewriting your URL's to: shop_details.php?url=$1 Then then in the case of: www.mydomain.com/shops/shop_name/product_name The $url var would be : shops/shop_name/product_name Use PHP's explode function to separate the parts. Example: $default_place = "shops"; $default_name = "name"; $default_product = "product"; $default_id = "001002"; if(isset($_GET['url'])) { $url = explode("/", $_GET['url']; $default_place = isset($url[0]) ? $url[0] : $default_place; // etc etc } [/code] Quote Link to comment https://forums.phpfreaks.com/topic/232401-dynamically-rewrite-urls/#findComment-1195502 Share on other sites More sharing options...
zander1983 Posted April 1, 2011 Author Share Posted April 1, 2011 im going to be using shop_details.php for links that look like: www.mydomain.com/shops/shop_name My rule for this is: RewriteRule ^shops/([^/\.]+)/?$ ./shop_details.php?shopurl=$1 [L] but links to www.mydomain.com/shops/shop_name/product_name, I want it to go to product_details.php So the rule I have will redirect www.mydomain.com/shops/shop_name/product_name to shop_details.php, which I dont want..... I was hoping to dynamically edit the .htaccess file so that a new rule would be added for each shop saying anything with /shops/shop_name/prouct_name in the url, go to product_details.php............ Quote Link to comment https://forums.phpfreaks.com/topic/232401-dynamically-rewrite-urls/#findComment-1195514 Share on other sites More sharing options...
DavidAM Posted April 1, 2011 Share Posted April 1, 2011 I'm no expert at rewrite, but you could put another rule BEFORE your existing one: RewriteRule ^shops/([^/\.]+)/([^/\.]+)/?$ ./product_details.php?shopurl=$1&produrl=$2 [L] I'm not sure why you are excluding the period between the slashes. Quote Link to comment https://forums.phpfreaks.com/topic/232401-dynamically-rewrite-urls/#findComment-1195671 Share on other sites More sharing options...
zander1983 Posted April 1, 2011 Author Share Posted April 1, 2011 yep figured this out earlier and what i did is almost identical to what u just suggested so you're spot on it man Quote Link to comment https://forums.phpfreaks.com/topic/232401-dynamically-rewrite-urls/#findComment-1195677 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.