AlbertEngelB
AlbertEngelB

Reputation: 16436

Alter Query Strings Server Side

I have an Express application that gets search parameters and page numbers via query strings.

var name = req.query.name;
var state = req.query.state;
var category = req.query.category;
var pageNum = req.query.pagenum;

What I want is if the search query has a page number and yields no results, to do a search without the page number and pass that back to the client. This works fine, and the front-end is receiving the data correctly.

The only thing is that I want the URL to reflect the changed page number search, so rather than being website.com/search?page=4&state=AL I'd like it to have the page number stripped or changed to 1. I can do this via Javascript on the front-end, but I'd rather have the url changed on the server side (it makes more sense to me at least, rather than having to change every template to do that).

EDIT:

Looks like the only way of doing this is to redirect to the URL without the page query string parameter. It seems this will help me out in getting this done.

Upvotes: 0

Views: 477

Answers (1)

Zoltán Tamási
Zoltán Tamási

Reputation: 12754

I don't quite know your environment, but generally URLs cannot be "changed" on server-side, because they are displayed in the browser on the client-side. The user sees the last whole page request's url. You can only force an URL update (actually a page reload with different URL) from server-side with a HTTP redirect header.

You could check if there are any results for the query with page number, and if not, simply send a redirect to the URL without the page number, so the user sees that extended result with its proper (page-less) URL.

Hope it helps something.

Upvotes: 2

Related Questions