PHP all GET parameters using mod_rewrite



I am developing my application. And I have to do the following. All GET parameters (? Var = value) must be converted to the value / var / using mod_rewrite. How can i do this? I only have a .php file (index.php) because I am using the FrontController template. Can you help me with this mod_rewrite rule?

Sorry for my English. Thank you in advance.

+4
source share
4 answers

I do something similar on sites that use "seo-friendly" URLs.

In .htaccess:

Options +FollowSymLinks RewriteEngine on RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_FILENAME} !-d RewriteRule .* /index.php [L] 

Then on index.php:

 if ($_SERVER['REQUEST_URI']=="/home") { include ("home.php"); } 

The .htaccess rule tells it to load index.php if the requested file or directory is not found. Then you simply parse the request URI to decide what to do index.php.

+6
source

The following code in your .htaccess will rewrite your URL, for example. /api?other=parameters&added=true to /?api=true&other=parameters&added=true

 RewriteRule ^api/ /index.php?api=true&%{QUERY_STRING} [L] 
+5
source

.htaccess

 RewriteEngine On # generic: ?var=value # you can retrieve /something by looking at $_GET['something'] RewriteRule ^(.+)$ /?var=$1 # but depending on your current links, you might # need to map everything out. Examples: # /users/1 # to: ?p=users&userId=1 RewriteRule ^users/([0-9]+)$ /?p=users&userId=$1 # /articles/123/asc # to: ?p=articles&show=123&sort=asc RewriteRule ^articles/([0-9]+)/(asc|desc)$ /?p=articles&show=$1&sort=$2 # you can add /? at the end to make a trailing slash work as well: # /something or /something/ # to: ?var=something RewriteRule ^(.+)/?$ /?var=$1 

The first part is the URL that is received. The second part rewrites the URL that you can read using $_GET . Everything between ( and ) considered as a variable. The first will be $1 , the second $2 . That way, you can determine exactly where the variables should go in the rewritten URL, and thereby know how to get them.

You can keep it very general and allow "everything" using (.+) . It just means: one or more ( + ) of any character ( . ). Or be more specific and, for example, allow only numbers: [0-9]+ (one or more characters in the range from 0 to 9). You can find much more information about regular expressions at http://www.regular-expressions.info/ . And this is a good site for checking them: http://gskinner.com/RegExr/ .

+3
source

AFAIK mod_rewrite does not process parameters after the question mark - regexp end-of-line for rewrite rules corresponds to the end of the path to '?'. Thus, you are largely limited to passing parameters or generally discarding them when overwriting.

+1
source

Source: https://habr.com/ru/post/1335307/


All Articles