问题
How to hide URL GET parameters (http://domain.com/MyFirstYii/page?view=about). I've searched lot of posts. They all are saying about rewrite and URL manager, but i couldn't achieve what i want. :(
My scenario is,
I just want to hide the URL GET parameters.
Eg:
http://domain.com/MyFirstYii/page***?view=about***
I wanted to hide ***?view=about***
.
Then URL should look like this http://domain.com/MyFirstYii/page
. Other pages like this http://domain.com/MyFirstYii/post
. In a simple words my GET parameters should act like POST parameters.
Thanks in Advance.
Edit:
I want to create some rules in the URLManager, but what kind of rules will hide the GET parameter.
回答1:
\w in regexp means „word“ character and such url part as „my-prety-page“ will NOT match. To hide GET params you must improve your urlManager rules. You can write such a rule for pages using SEF urls:
'<controller:\w+>/<id:\d+>/<title:[^\/]*>/*' => '<controller>/view'
In this case when you enter url
http://example.com/page/12/my-prety-title
a Page controller will be called to perform view action with id and title as arguments. It is the same if you enter this url:
http://example.com/page/view?id=12&title=my-prety-title
The last part /*
in rule allows to keep additional params. E.g. if your address is
http://example.com/user/55/john-doe-junior/foo/bar/
in UserController
's actionView
you can write
echo '<pre>' ;
print_r($_GET);
echo '</pre>' ;
die();
and you'll see
Array
(
[id] => 55
[title] => john-doe-junior
[foo] => bar
)
回答2:
uncomment these line from main.php
'urlManager'=>array( 'urlFormat'=>'path', 'showScriptName'=>false, 'rules'=>array( 'MyFirstYii/post/<view>'=>'MyFirstYii/post', '<controller:\w+>/<id:\d+>'=>'<controller>/view', '<controller:\w+>/<action:\w+>/<id:\d+>'=>'<controller>/<action>', '<controller:\w+>/<action:\w+>'=>'<controller>/<action>', ), ),
And put .htaccess file in root directory of project and write following code
RewriteEngine on # if a directory or a file exists, use it directly RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_FILENAME} !-d # otherwise forward it to index.php RewriteRule . index.php
回答3:
Add this url rule to the top of your url rules:
'page/<view:\w>' => 'user/page'
I'm assuming the next:
- controller name: UserController
- action name: actionPage
If my assumptions are wrong, please provide the right controller name and action name, so I can fix the answer.
UPDATE: fixed controller name based on comment
UPDATE2:
If you want this to work for all actions in your controller, use:
'<action:\w>/<view:\w>' => 'user/<action>'
回答4:
If you intend to use GET and need these parameter, you can't hide it, as this is the way GET works. If you really need to hide the parameter, you must switch to POST, as then parameters will be passed in the request payload instead of in url.
回答5:
use post method instead of get....that's the best and efficient solution.
to follow up on your query check out this site:
[http://pure-essence.net/2007/06/29/simple-php-path-rewrite/]
来源:https://stackoverflow.com/questions/17086732/hide-get-parameter-from-url