.htaccess Rewrite last five directories as GET values - php

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 to GET values, to reflect the following URL:
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=$1&gateway=$2&amt=$3&curr=$4&ts_ttPageLoad=$5 [QSA,L]
Over at https://htaccess.madewithlove.com/ I get the following rewrite result:
https://sub.example.com/index.php?id=economy/billing/payment-check/434&gateway=khipu&amt=1000&curr=CLP&ts_ttPageLoad=1673526088
What am I doing wrong? Thank you!
EDIT: The .htaccess is located in https://sub.example.com/economy/billing/payment-check/

The regex quantifier * is greedy by default so consumes as much as possible and then backtracks as required. This is why $1 contains economy/billing/payment-check/434 (ie. as much as possible that satisfies the regex). Although, this is assuming the .htaccess file is in the document root (which is what the "MWL tester" assumes).
If you are wanting to match a single path segment only then exclude the slash from the capturing subpattern, eg. ([^/]+).
Also, from the nature of your rule, I'm assuming this .htaccess file is in the /economy/billing/payment-check/ subdirectory (not the root)? The "MWL tester" tool assumes the .htaccess file is located in the document root (and this cannot be changed). In fact, this would "work" on your server if the .htaccess file is located in the subdirectory, however, it is not optimal, as it could still potentially match too much given a variation in the URL structure.
If the .htaccess file is located at /economy/billing/payment-check/.htaccess then use the following instead:
RewriteRule ^([^/]+)/([^/]+)/([^/]+)/([^/]+)/([^/]+)/$ index.php?id=$1&gateway=$2&amt=$3&curr=$4&ts_ttPageLoad=$5 [QSA,L]
The regex [^/]+ matches something except a slash.
If, however, the rule is in the root .htaccess file (which the MWL tester is expecting) then the rule would need to look like this:
RewriteRule ^(economy/billing/payment-check)/([^/]+)/([^/]+)/([^/]+)/([^/]+)/([^/]+)/$ $1/index.php?id=$2&gateway=$3&amt=$4&curr=$5&ts_ttPageLoad=$6 [QSA,L]
Note that the first capturing subpattern is now the URL-path, that is repeated in the substitution string (2nd argument). This simply avoids having to manually repeat the URL-path in the 2nd argument.
It looks like your URL is expecting specific data types in the path segments. For instance, the 1st, 3rd and 5th "parameters" look to be digits only. If this is the case then you should be specific and match digits only. Likewise, the 4th "param" is uppercase and the 2nd is lowercase letters. So, being specific, this could be rewritten like this (with the .htaccess file in the subdirectory):
RewriteRule ^(\d+)/([a-z]+)/(\d+)/([A-Z]+)/(\d+)/$ index.php?id=$1&gateway=$2&amt=$3&curr=$4&ts_ttPageLoad=$5 [QSA,L]
It then only matches the format of URLs you are expecting.

Related

I need a mod_rewrite expression to rewrite all paths that do not have a file extension

What i'm trying to do is have my main script index.php process all requests that do not contain a file extension. All other requests that do contain a file extension such as images should not be affected by mod_rewrite.
For example, I need the following urls to all be rewritten to /index.php?req={REQUEST_URI}:
/index/a/b/c
/index/a/b
/index
But disregard urls with an extension. Apache and not PHP should process urls such as:
/index/a/test.png
/index/a/b/c/script.js
My current expression only works for paths with a single directory.
RewriteRule ^/?([a-z0-9]+)$ /index.php?req=%{REQUEST_URI} [QSA,L]
This expression rewrites "/index", but not "/index/a" and so on...
Any help would be greatly appreciated. Thanks!
You can use:
RewriteRule !^/.*\.([a-zA-Z0-9]){2,}$ /index.php?req=%{REQUEST_URI} [QSA,L]
This redirects anything that does not match the pattern ^/.*\.([a-zA-Z0-9]){2,}$. The pattern defines a string ending with a period \. followed by two or more alphanumeric characters ([a-zA-Z0-9]){2,}.
If you know the length of your file extensions, you can modify the numbers in the curly braces; e.g. file extensions between 1 and 3 characters would be satisfied with ([a-zA-Z0-9]){1,3}

I want to hide URL parameters of my website page by using .htaccess

http://eoflex/CAportal/readmore/?slug=blogtitle
I am trying to hide url parameters by .htaccess file is it possible to show url like this ?
http://eoflex/CAportal/blogtitle
Give me solution I am new to .htaccess file
I am writing code in .htaccess file given below and its not working, i want url like http://eoflex/CAportal/blogtitle I don't want to display /?slug=blogtitle*
RewriteRule ^([A-Za-z0-9-]+)/?$ page.php?slug=$1 [L]
you can use the following Code in .htaccess file . you can also use apache rewrite module . By First enabling apache rewrite module in apache server .
Write Below Lines In .htaccess file
RewriteEngine On
RewriteRule ^/CAportal/readmore/([a-zA-Z0-9_-]+)/$ /CAportal/readmore/page.php?slug=$1
HINT
^/read/This/Url/blogtitle /as/This/Url/page.php?slug=blogtitle
INFO
^
Matches the beginning of a string
$
Matches the end of a string
[0-9]
Matches a number, 0–9. [2-4] would match numbers 2 to 4 inclusive.
[a-z]
Matches lowercase letters a–z
[A-Z]
Matches uppercase letters A–Z
[a-z0-9]
Combining some of these, this matches letters a–z and numbers 0–9
Try this (put this in .htaccess in the document root).
RewriteRule ^CAportal/readmore/([a-zA-Z0-9_-]+)/?$ /CAPortal/readmore/?slug=$1 [L]

RewriteRule to allow forward slash in query string

I have the following re-write rule:
RewriteRule ^([\w-]+)$ modules.php?mod_name=$1
It allows me to open a url like:
mydomain.com/settings
This calls the php file in the following format:
modules.php?mod_name=settings
However, I need to call my php script with mod_name values that contain a path e.g:
modules.php?mod_name=settings/preferences.php?id=1103
I would like the re-write rule to be able to accept the above in the following format:
mydomain.com/settings/preferences/1103
Any ideas how this can be done?
RewriteRule ^([\w-]+)$ modules.php?mod_name=$1
Your \w matches word characters ([a-z] [A-Z] [0-9]) and dashes (-) (thus not the /).
To include the /, you need something like ^([\w-/]+)$

Simple wild character for mod_rewrite?

new to mod_rewrite. I simply want to make something like this:
# Turn on the rewriting engine
RewriteEngine On
# use item php page to show the item
RewriteRule ^parts/./[.]$ parts/item.php?id=$1 [NC]
so when a person types in example.com/parts/anything/anything, the URL will go to
example.com/parts/item.php?id=the2ndanything
From my understanding, the period means any character. Also, I tried to add a + after the period(which should mean as much as anything), but that still didn't work.
by the way, I am on a temporary URL, since I have not changed my previous web host yet, and therefore my actual full URL has a ~ in it. I am hoping this is not an issue for mod_rewriting and therefore being the issue that stops it from working. the .htaccess file is in the root folder with the web sites index.html.
The . does mean any character, but you must follow it by + (one or more) or * (zero or more) and you must capture it in () to be used as $1. Use [^/]+ to match all characters up to but not including / to match but not capture the first directory after parts/.
RewriteEngine On
# (.*) captures the second "anything" in $1
RewriteRule ^parts/[^/]+/(.*)$ parts/item.php?id=$1 [L]

Trouble with URL rewriting in .htaccess

My .htaccess file looks like this:
RewriteEngine On
RewriteRule ^articles/(\d+)*$ ./articles.php?id=$1
So, if the URL foo.com/articles/123 is requested, control is transferred to articles.php?id=123.
However, if the requested URL is:
foo.com/articles/123/
or
foo.com/articles/123/whatever
I get a "404 Not Found" response.
I would like to call articles.php?id=123 in all these cases. So, if the URL starts with foo.com/articles/[digits]... no matter what other characters follow the digits, I would like to execute articles.php?id=[digits]. (The rest of the URL is discarded.)
How do I have to change the regular expression in order to achieve this?
Just don't look for the end:
RewriteRule ^articles/(\d+) ./articles.php?id=$1
You do need to allow the trailing / with:
RewriteRule ^articles/(\d+)/?$
The \d+ will only match decimals. And the $ would disallow matches beyond the end.
If you also need trailing identifiers, then you need to allow them too. Then it might be best to make the match unspecific:
RewriteRule ^articles/(.+)$
Here .+ matches virtually anything.
But if you want to keep the numeric id separate then combine those two options:
RewriteRule ^articles/(\d+)(/.*)?$ ./articles.php?id=$1

Categories