I am designing my application. And I should make the next things. All GET parameters (?var=value) with help of mod_rewrite should be transform to the /var/value. How can I do this? I have only 1 .php file (index.php), because I am usign the FrontController pattern. Can you help me with this mod_rewrite rules?
Sorry for my english. Thank you in advance.
相关问题
- Views base64 encoded blob in HTML with PHP
- Laravel Option Select - Default Issue
- PHP Recursively File Folder Scan Sorted by Modific
- Can php detect if javascript is on or not?
- Using similar_text and strpos together
The following code in your .htaccess will rewrite your URL from eg.
/api?other=parameters&added=true
to/?api=true&other=parameters&added=true
I do something like this on sites that use 'seo-friendly' URLs.
In .htaccess:
Then on index.php:
The .htaccess rule tells it to load index.php if the file or directory asked for was not found. Then you just parse the request URI to decide what index.php should do.
.htaccess
The first part is the URL that is received. The second part the rewritten URL which you can read out using
$_GET
. Everything between(
and)
is seen 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 retrieve them.You can keep it very general and allow "everything" by using
(.+)
. This simply means: one or more (the+
) of any character (the.
). Or be more specific and e.g. only allow digits:[0-9]+
(one or more characters in the range 0 through 9). You can find a lot more information on regular expressions on http://www.regular-expressions.info/. And this is a good site to test them: http://gskinner.com/RegExr/.AFAIK mod_rewrite doesn't deal with parameters after the question mark — regexp end-of-line for rewrite rules matches the end of path before the '?'. So, you're pretty much limited to passing the parameters through, or dropping them altogether upon rewriting.