Home  >  Q&A  >  body text

.htaccess rewrites the last five directories as GET values

I have the following URL:

https://sub.example.com/economy/billing/ payment-check/434/khipu/1000/CLP/1673526088/

I want the last 5 directories to be converted into GET values ​​to reflect the following URLs:

https://sub.example.com/economy/billing/ payment-check/?id=434&gateway=khipu&amt=1000&curr=CLP&ts_ttPageLoad=1673526088

I have the following .htaccess lines:

RewriteEngine on
RewriteRule ^(.*)/(.*)/(.*)/(.*)/(.*)/ index.php?id=&gateway=&amt=&curr=&ts_ttPageLoad= [QSA,L]

On https://htaccess.madewithlove.com/ I get the following rewrite results:

https://sub.example.com/index.php?id=economy/billing/ payment-check/434&gateway=khipu&amt=1000&curr=CLP&ts_ttPageLoad=1673526088

What did i do wrong? Thanks!

Edit: .htaccess located at https://sub.example.com/economy/billing/ payment-check/

P粉451614834P粉451614834300 days ago388

reply all(1)I'll reply

  • P粉384679266

    P粉3846792662024-01-17 13:06:03

    Regular Expression Quantifier * The default is greedy, so consumes as much as possible and then backtracks as needed. That's why $1 contains economy/billing/ payment-check/434 (i.e. satisfies the regex as much as possible). However, this assumes that the .htaccess file is located in the document root (which is what the "MWL Tester" assumes).

    If you only want to match a single path segment, exclude slashes from the capturing subpattern, eg. ([^/] ).

    Also, based on the nature of your rule, I'm assuming this .htaccess file is located in the /economy/billing/payment-check/ subdirectory (not the root)? The "MWL tester" tool assumes that the .htaccess file is located in the document root (and cannot be changed). In fact, this will "work" on your server if the .htaccess file is in a subdirectory, however, this is not optimal as it may still match too much given the URL changes. Multiple structures.

    If the .htaccess file is located in /economy/billing/ payment-check/.htaccess, then use the following:

    RewriteRule ^([^/]+)/([^/]+)/([^/]+)/([^/]+)/([^/]+)/$ index.php?id=&gateway=&amt=&curr=&ts_ttPageLoad= [QSA,L]

    Regular expression [^/] Matches content except slashes.

    However, if the rule is in the root .htaccess file (as expected by MWL testers), then the rule needs to look like this:

    RewriteRule ^(economy/billing/payment-check)/([^/]+)/([^/]+)/([^/]+)/([^/]+)/([^/]+)/$ /index.php?id=&gateway=&amt=&curr=&ts_ttPageLoad= [QSA,L]

    Note that the first captured subpattern is now the URL path, which is repeated in the replacement string (second argument). This just avoids manually repeating the URL path in the second parameter.

    Your URL appears to require a specific data type in the path segment. For example, the first, third, and fifth "arguments" look like just numbers. If this is the case, then you should be specific and only match numbers. Likewise, the fourth "param" is uppercase and the second is lowercase. So, specifically, it can be rewritten like this (using a .htaccess file in a subdirectory):

    RewriteRule ^(\d+)/([a-z]+)/(\d+)/([A-Z]+)/(\d+)/$ index.php?id=&gateway=&amt=&curr=&ts_ttPageLoad= [QSA,L]

    It will only match the URL format you expect.

    reply
    0
  • Cancelreply