mvc powered by slash based urls => muweb.com/controller/param1/param2

how to rewrite muweb.com/controller/?item=45 to muweb.com/controller/45 or even to muweb.com/controller/item45 ?

    1 answer 1

    As with most questions of this type, it is not clear what you want to change. That is, it is unclear what the user should enter in the address bar and what should be after the redirection. Therefore, we consider both options for redirection using the .htaccess file.


    Option 1.1.

    The source address is muweb.com/controller/?item=45 , the address after the redirection is muweb.com/controller/45 .

     RewriteEngine On RewriteBase / RewriteCond %{QUERY_STRING} item=(\d+) RewriteRule ^(controller)$ $1/%1? [L,R] 

    Option 1.2.

    The source address is muweb.com/controller/?item=45 , the address after the redirection is muweb.com/controller/item45 .

     RewriteEngine On RewriteBase / RewriteCond %{QUERY_STRING} item=(\d+) RewriteRule ^(controller)$ $1/item%1? [L,R] 

    Option 2.1.

    The source address is muweb.com/controller/45 , the address after the redirection is muweb.com/controller/?item=45 .

     RewriteEngine On RewriteBase / RewriteRule ^(controller)/(\d+)/?$ $1?item=$2 [L,R,QSA] 

    Option 2.2.

    The source address is muweb.com/controller/item45 , the address after the redirection is muweb.com/controller/?item=45 .

     RewriteEngine On RewriteBase / RewriteRule ^(controller)/(item)(\d+)/?$ $1?$2=$3 [L,R,QSA] 

    Option 2.3.

    The source address is muweb.com/controller/item45 , or muweb.com/controller/45 , the address after the redirection is muweb.com/controller/?item=45 .

     RewriteEngine On RewriteBase / RewriteRule ^(controller)/(\d+)/?$ $1?item=$2 [L,R,QSA] RewriteRule ^(controller)/(item)(\d+)/?$ $1?$2=$3 [L,R,QSA] 
    • Thank you, for such a detailed answer, the essence of my questions was that after submit from the search form, the GET parameters were immediately transferred to the controller through a slash, I just cannot understand one thing. or just after the transition the URL itself looks through a slash and the parameters in the controller are available only via GET - Cone Enoc
    • @ConeEnoc I can't help here. Better ask a separate question. - user194374