URL Re-Writing Help

Hello Masters !

I need some help about setting up my .htaccess file.

I need url rewriting for this:

www.mysite.com/var1/ =================== should go to index.php?category=var1
www.mysite.com/var1/var2/ ================= should go to index.php?category=var1&product=var2
www.mysite.com/var1/var2/var3/============== should go to index.php?category=var1&product=var2&version=var3
www.mysite.com/var1/var2/var3/var4/========== should go to index.php?category=var1&product=var2&version=var3&config=var4

Now - here i must mention that www.mysite.com/help.php should go to help.php or contact.php should go to contact.php

Please guide !

Regards
ZH

Hi,

I have written the code for below two urls

www.mysite.com/var1/ =================== should go to index.php?category=var1
www.mysite.com/var1/var2/ ================= should go to index.php?category=var1&product=var2

Please place this code in your .htaccess file…

RewriteEngine On
RewriteRule ^([^/])/$ /index.php?category=$1 [L]
RewriteRule ^([^/]
)/([^/]*)/$ /index.php?category=$1&product=$2 [L]

Thanks

ZH,

All you really need is one RewriteRule for that (with optional parts):

RewriteEngine on RewriteRule ^([a-z]+)/(([a-z]+)/(([a-z]+)/(([a-z]+)/)?)?)?$ index.php?category=$1&product=$3&version=$5&config=$7 [L]

Please note that this code precludes you using extensionless filenames (no dot characters in the character range definitions) and this REQUIRES a category to have a product to have a version to have a config.

Please note that trailing /'s are reserved in the Apache world for directories AND that index.php will be forced to use internal absolute URIs for its support files (because of the different directory levels of your optional key/value pairs).

Regards,

DK

Hi guys !

Thanks a lot - I am going to use your codes - will put the results here.

1 thing that I forgot to mention is that, in the 4 variables it could be Alphabets, Numbers, and alphabets from French too.

Your simplest and I think best option is to match path segments [^/]+ That is, match whatever happens to be between the slashes. If you put that in place of the [a-z]+ bits, you should be good to go.

Thanks Jeff,

What if i browse to “mysite.com/contact-us.php” ?

The regex would consider that a category and rewrite. A couple ways around this are: 1) Clearly define the syntax of a category. For example, you could distinguish them by deciding that a category name must never contain a period and real files always must. Or 2) you could decide on a path prefix, such as **/products/**var1/var2/var3/var4. Or 3) you could condition the rewrite on whether the request already points to a real file. If it’s a real file, then assume it isn’t a category and don’t rewrite, or if it’s not a real file, then assume it’s a category and rewrite.

I think #2 is best, possibly with #3 in addition to handle special cases.

Thank you yes that is great.

Can you please guide me on the #2 and #3 conditions ?

I will make sure that none of the variables will have - OR . OR /

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.