SW_Cali
SW_Cali

Reputation: 393

htaccess - Skip first folder and pass as variable

I am changing a site into a multilingual site:

example.com/en/view-item/sku/

example.com/es/view-item/sku/

example.com/de/view-item/sku/

And need to pass the /fr/ part as a variable.

This questions gave a very good answer: htaccess - skip first folder

However, in my htaccess file, I also have other rules, for example:

RewriteRule view-item/([^/.]+)/?$ /shop/view.php?productcode=$1

RewriteRule product-category/([^/.]+)/?$ /shop/products.php?selectedcat=$1

The problem is, that using...

RewriteRule ^(en|fr|es|de|nl|it)(/.*)$ $2?language=$1 [NC,L,QSA]

...works when a path is not edited by htaccess, for example:

www.example.com/fr/shop/view.php?productcode=abc123

this adds the language variable on ok.

But for

www.example.com/fr/view-item/sku/

It does not work, as the htaccess is altering this again. I guess after it has accounted for the /fr/ the url will look like this:

www.example.com/fr/view-item/sku/

to

www.example.com/view-item/sku/?language=fr

My knowledge of htaccess isnt go enough to know what code to use to pass the language variable on again.

I have also tried...

RewriteRule ^(en|fr|es|de|nl|it)(/.*)$ $2/$1/ [NC,L,QSA]

RewriteRule view-item/([^/.]+)/([^/.]+)/?$ /shop/view.php?productcode=$1&language=$2

This works, but wont work for these urls...

www.example.com/fr/

www.example.com/fr/shop/view.php?productcode=abc123

I assume the very first example will work, if I can pass on the ?language=$1 when editing the /view-item/ folder later.

Thank you in advance.

Upvotes: 0

Views: 114

Answers (2)

anubhava
anubhava

Reputation: 785631

Have it like this:

RewriteEngine On

RewriteRule ^(en|fr|es|de|nl|it)(/.*)$ $2?language=$1 [NC,L,QSA]

RewriteRule ^view-item/([^/.]+)/?$ /shop/view.php?productcode=$1 [L,QSA,NC]

RewriteRule ^product-category/([^/.]+)/?$ /shop/products.php?selectedcat=$1 [L,QSA,NC]

Upvotes: 0

SW_Cali
SW_Cali

Reputation: 393

I have just found a work-around for this, by basically putting the above in a different order:

RewriteRule ^(en|fr|es|de|nl|it)/view-item/([^/.]+)(/.*)$ /$1/shop/view.php?productcode=$2 [NC,L,QSA]

RewriteRule view-item/([^/.]+)(/.*)$ /shop/view.php?productcode=$1

RewriteRule ^(en|fr|es|de|nl|it)(/.*)$ $2?language=$1 [NC,L,QSA]

Although this isnt what my original question was needing exactly, it works fo thought I would post this here. If anyone has any better solutions, please post, thanks.

Upvotes: 0

Related Questions