Jump to content

dynamically rewrite URLs


zander1983

Recommended Posts

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

Link to comment
Share on other sites

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]

Link to comment
Share on other sites

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

 

 

 

Link to comment
Share on other sites

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.

 

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.