I have a little complex rewrite rule and I can’t get the hang of it.
What I want to do is use .htaccess to make my website see this link:
https://some-domain.com/human/jeff
As this link (because the system knows how to handle this link):
https://some-domain.com/?type=human&which=jeff
Notes:
- I want to apply this rule only if
type=human
. which
param can contain only letter characters.- If the type is human, there wont be any more url params except for those.
After a lot of trying this is my final and failed .htaccess content:
RewriteEngine On
RewriteRule ^(https://[^/]+)/??type=(human)&which=([a-zA-z]+)$ $1/$2/$3 [NC, L]
Here is the logic behind it:
- Group 1:
(https://[^/]+)
$1 will contain any string matchinghttps://{any non empty string that does not containing "/"}
– https://some-domain.com - Static:
/??
matches “/?” or “?” - Group 2:
type=(human)
$2 will contain “human” - Group 3:
which=([a-zA-z]+)
$3 will contain a non empty string with only letter characters (jeff).
Expected result:
This will match the regex
https://{any non empty string that does not containing "/"}/human/{any non empty string}
And I’m pretty much stuck here
Thank you!
EDIT:
I Should also mention that the rewrite should be for POST requests only, and the post params are needed.
3
Answers
Thank you, Michał Turczyn and Mohammed Elhag for helping.
Here is the final and working content of .htaccess.
This rule applies only when the REQUEST_URI is /human/{any non empty string}
Try this pattern:
^(https?://[^/]+/)?type=(human)&which=([a-z]+)$
^
– beginning of a stringhttps?://[^/]+/
– matchhttp
literally, then matchs
optionally (zero or one),//
matches//
literally,[^/]+
matches on or more characters otehr than/
and then matches/
literally?type=(human)&which=([a-z]+)
– match?
literally, then matchtype=human
literally (human
will be stored in captruing group), then match&which=
literally, then match one or more letters with[a-z]
It will match your string when it is in desird form (
type=human
and only letters inwhich
).Substitution pattern:
12/3
1
– replace with first capturing group, similairly for2
and3
Demo
In this line :
This part
^(https://[^/]+)/??type=(human)&which=([a-zA-z]+)$
is pattren and should match against aURI
but not fullURL
.Try this :
Note: clear browser cache then test.