How should I structure my query string in MVC htaccess applications?
I was wondering about creating "fake" URL queries in a web application. When you put a .htaccess
file in the root folder of a project you can rewrite (behind the scenes) this URL http://www.website.com/blog/view
into http://www.website.com/index.php?q=blog/view
. That way you can maintain a single access point and create a MVC application out of the path in $_GET["q"]
.
But what about actual query strings? Surely this type of rewrite doesn't offer the flexibility of inputting a variable query string. Thus I liked the idea of the following:
http://www.website.com/blog/search?keywords=some+blog+post&limit=10&order=DESC
But the minute you put & symbols in your URL, things get smelly. For tha开发者_StackOverflow中文版t reason I'm currently replacing the & symbols with | symbols
http://www.website.com/blog/search?keywords=some+blog+post|limit=10|order=DESC
But now a user could break my application by mistakenly putting a & symbol in the URL.
To sum up, what I'm wondering is if there is a way to "escape" the & symbols behind the scenes so that I could put & symbols in the URL, but my application would see | symbols (Or something like it)
Any other method of making this possible I would also love to hear
You want to use the Query String Append ([QSA]
) flag on the end of your RewriteRule in your .htaccess file.
For example:
RewriteRule ^(.*)$ index.php?request=$1 [L,QSA]
The [QSA] flag on the rule says to take any pre-existing GET parameters and attach them to the new URI. So this:
http://www.example.com/blog?post=123
Becomes this:
http://www.example.com/index.php?request=blog&post=123
精彩评论