How should I structure my query string in MVC htac

2019-05-31 18:04发布

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 that 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

1条回答
Emotional °昔
2楼-- · 2019-05-31 18:40

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
查看更多
登录 后发表回答