When I submit the form by GET, I would like the URL ?search=xxx to be /search/xxx.

Asked 1 months ago, Updated 1 months ago, 4 views

Search results page URL

http://example.com/?search=search string

From Parameter Format to

http://example.com/search/ search string

We received a request from the other party that we would like this format.

I wanted to listen to the other party's request, so I tried implementing it with javascript and PHP, but I felt very uncomfortable, so I started thinking about refusing.
"I have been conducting various investigations, but I can't find any reason to say no other than ""feeling uncomfortable."""

$('#form').on('click', function(e){
    // Cancel submit
    e.preventDefault();

    // Get string and create url
    varurl='http://example.com/search/'+$('#search').val();
    // prevented the transition destination url from entering values in parameter format
    $('#search') .val(');
    // Redirect instead of submit
    location.href=url;
});
// The content is abstracted.

The discomfort I felt was mainly as follows.

Rather, if we can decide that there is no problem with this, we can proceed in this direction, so we would like to hear your opinions and knowledge.

Thank you for your cooperation.

javascript php html

2022-09-30 11:40

2 Answers

If apache and nginx handle URL changes instead of programming, the program will be able to meet your needs and not be too lazy.I can't write specifically because the environment is not written, but if it's apache, I think you can enable mod_rewrite and write the following settings.

RewriteRule^/search/(.*)/?search=$1


2022-09-30 11:40

I tried it out of the blue.
I tried it locally, and it seems that IE11 and Firefox 50.1.0 can go.
I didn't verify anything else.

<form method="get"
      action="http://example.com/serch/"
      onsubmit="this.action=this.action+ document.getElementById('serch_text').value;"
      >
 <p><input type="serch" id="serch_text"/></p>
 <p><button type="submit">Submit</button></p>
</form>


2022-09-30 11:40

If you have any answers or tips


© 2022 OneMinuteCode. All rights reserved.