Reputation: 216
I would like to create a short URL for voucher cards, which redirect to the correct product with the corresponding code and applies it.
So actually an end url would show like this: https://shop.com/client/cart.php?a=add&pid=11&billingcycle=monthly&promocode=v1-B2J3 ("pid"= specific product id | "v1-B2J3"= individual promocode)
My idea was, to create the short url: shop.com/promo/"CODE" and the client will be redirected (with .htaccess rule) to https://shop.com/client/cart.php?a=add&pid=11&billingcycle=monthly&promocode="CODE"
Relatively simple so far. But there are 4 products and 4 different code layouts:
The first letter and number defines the product (pid) and after the hyphen comes an individual code.
So the rule must not only pass the variable, the coupon code, during the forwarding, it must also determine the PID depending on the beginning of the coupon code.
Upvotes: 0
Views: 65
Reputation: 42984
If you have a fixed number of codes, four in your case, then it certainly is best to implement those one by one. Trying to implement a general solution will only make things complex and hard to maintain.
Here is a simple example:
RewriteEngine on
RewriteRule ^/?(V1-2B3K)$ /client/cart.php?a=add&pid=11&billingcycle=monthly&promocode=$1 [R=301]
RewriteRule ^/?(V2-2B3K)$ /client/cart.php?a=add&pid=12&billingcycle=monthly&promocode=$1 [R=301]
RewriteRule ^/?(W1-2B3K)$ /client/cart.php?a=add&pid=20&billingcycle=monthly&promocode=$1 [R=301]
RewriteRule ^/?(W2-2B3K)$ /client/cart.php?a=add&pid=21&billingcycle=monthly&promocode=$1 [R=301]
It is a good idea to start out with a 302 temporary redirection and only change that to a 301 permanent redirection later, once you are certain everything is correctly set up. That prevents caching issues while trying things out...
This rule will work likewise in the http servers host configuration or inside a dynamic configuration file (".htaccess" file). Obviously the rewriting module needs to be loaded inside the http server and enabled in the http host. In case you use a dynamic configuration file you need to take care that it's interpretation is enabled at all in the host configuration and that it is located in the host's DOCUMENT_ROOT
folder.
And a general remark: you should always prefer to place such rules in the http servers host configuration instead of using dynamic configuration files (".htaccess"). Those dynamic configuration files add complexity, are often a cause of unexpected behavior, hard to debug and they really slow down the http server. They are only provided as a last option for situations where you do not have access to the real http servers host configuration (read: really cheap service providers) or for applications insisting on writing their own rules (which is an obvious security nightmare).
Upvotes: 1