2

I have created an API and i get all my data with a specific url, and by 'id' like this :

'myfolder.php/1'

So now i try to get my url with a string :

myfolder.php/mystring 

in a Htacess for the id i do like this :

RewriteRule ^[^/]+/(\d+)$ myfolder.php?id=$1

but i don't find the good regex for characters i have try like this :

RewriteRule ^[^/]+/([a-z]+)$ myfolder.php?string

Edit : If i call url 'myurl.com', my function getProducts is called. If i call 'myurl.com/1', getProduct($id) is called But if i call 'myurl.com/string', $_GET is empty, and getProducts is called.

   switch($request_method) {

        case 'GET':
            // HERE $_GET is empty if i called a string
            if(!empty($_GET["id"])) {
                $id= $_GET["id"];
                getProduct($id);
            }
            else {
                getProducts();
            }
            break;
        default:
            header("HTTP/1.0 405 Method Not Allowed");
            break;
    }
RavinderSingh13
  • 130,504
  • 14
  • 57
  • 93
Ygg69
  • 261
  • 6
  • 22

1 Answers1

2

With your shown samples, please try following. Please make sure to clear your browser cache before testing your URLs. Also make sure your php file is residing in same folder where you are keeping your htaccess rules file.

##Enabling RewriteEngine here.
RewriteEngine ON
##Placing conditions for non-existing requests with rewriterule to php file.
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([\w-]+)/?$  myfolder.php/$1 [NC,QSA,L]
RavinderSingh13
  • 130,504
  • 14
  • 57
  • 93