4

I have a bunch of products listed at

https://www.example.com/all/products/foldername/1234567890-ProductDescriptionA-456.html
https://www.example.com/all/products/foldername/7654321-SomeOtherDesc-B123.html
https://www.example.com/all/products/foldername/93939393-anotherthing-F93939393.html

and I want these to be redirected to

https://www.example.com/products.php?p=1234567890
https://www.example.com/products.php?p=7654321
https://www.example.com/products.php?p=93939393

respectively. Is there an htaccess rule to do string operations on a matched parameter? For example, if I had to convert my URL using Python, it would look like this:

def make_new_url(old_url):
    product_id = old_url.split('/')[-1].split('-')[0]
    new_url = 'https://www.example.com/products.php?p=%s' % product_id
    return new_url

In the old URLs, the product ID is found after the last "/" and just before the first "-". Another regular expression based rule that would work would be:

\/(\d*?)\-

Any thoughts as to how to accomplish this inside an Apache htaccess file?

3
  • Do you want URLs to change to https://www.example.com/products.php?p=1234567890 in browser? Commented Jun 22, 2015 at 7:18
  • Yes. When a request is made to the server for https://www.example.com/all/products/foldername/1234567890-ProductDescriptionA-456.html I want https://www.example.com/products.php?p=1234567890 to be returned. I don't care about any of the content except SOMENUMBERS in: ...../foldername/SOMENUMBERS-otherstuff.html Commented Jun 22, 2015 at 12:06
  • Hmm then either of 2 answers should work for you. Commented Jun 22, 2015 at 12:12

2 Answers 2

3

Try this one:

RedirectMatch ^.*\/(\d+)\-.*$ products.php?p=$1

The rewrite rule matches the number inside a capture group and then substitutes it for $1.

Sign up to request clarification or add additional context in comments.

2 Comments

I think the OP is saying that the top three URL examples are the old ones. As such, those should be redirected and not rewritten.
This was good enough for me to tweak it to get it working successfully. Thanks!
1

Try this :

RedirectMatch ^/all/products/foldername/([0-9]+)-productDiccriptionA-([A-Za-z0-9]+)\.html$ https://example.com/product.php?p=$1

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.