skip to Main Content

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:

  1. I want to apply this rule only if type=human.
  2. which param can contain only letter characters.
  3. 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 matching https://{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


  1. Chosen as BEST ANSWER

    Thank you, Michał Turczyn and Mohammed Elhag for helping.
    Here is the final and working content of .htaccess.

    <IfModule mod_rewrite.c>
     RewriteEngine On
     RewriteCond %{REQUEST_URI} ^/(human)/([a-zA-Z]+)/?$ [NC]
     RewriteRule ^(human)/(.*)$ /?type=$1&which=$2 [L]
    </IfModule>
    

    This rule applies only when the REQUEST_URI is /human/{any non empty string}


  2. Try this pattern: ^(https?://[^/]+/)?type=(human)&which=([a-z]+)$

    ^ – beginning of a string

    https?://[^/]+/ – match http literally, then match s 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 match type=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 in which).

    Substitution pattern: 12/3

    1 – replace with first capturing group, similairly for 2 and 3

    Demo

    Login or Signup to reply.
  3. In this line :

    RewriteRule     ^(https://[^/]+)/??type=(human)&which=([a-zA-z]+)$ $1/$2/$3 [NC, L]
    

    This part ^(https://[^/]+)/??type=(human)&which=([a-zA-z]+)$ is pattren and should match against a URI but not full URL.

    Try this :

    RewriteEngine On
    RewriteCond %{QUERY_STRING} ^type=(human)&which=([A-Za-z]+)$  [NC]
    RewriteRule ^ /%1/%2? [R=301,L]
    RewriteRule ^(human)/(.*)$ /?type=$1&which=$2 [L]
    

    Note: clear browser cache then test.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search