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
https://forums.phpfreaks.com/topic/232401-dynamically-rewrite-urls/
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]

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

 

 

 

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.

 

Archived

This topic is now archived and is closed to further replies.

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