Clean Urls for Search Query

Clean URLs for search query?

I think the problem is that you've created an HTML form with GET method, which automatically opens the URL that way you specified as the result. If you want to submit your search query like the desired one, you should hack the form with some JavaScript to call your good-looking URL like this:

<form method="get" action="/search/" onsubmit="return false;">
<input type="search" name="q" value="querystring" />
<input type="submit" onclick="window.location.href=this.form.action + this.form.q.value;" />
</form>

What's the best way to return a clean URL by removing unwanted query params while keeping others?

You can use URLSearchParams which is supported on all major browsers: https://developer.mozilla.org/en-US/docs/Web/API/URLSearchParams

Something like:

const params = new URLSearchParams(window.location.href);
for (var key of params.keys()){
// do something with logic from your allow/block list
// such as removing it:
params.delete(key);
}

Make clean URLS and retrieve query string

... carried on from OP comments.

These frameworks read the request again in their respective languages to allow the framework to route to specific controllers, but they still need the webserver to be setup to send the request to the framework in the first place.

  • Client requests http://example.com/forums/123
  • After DNS lookup, request hits server at 127.0.0.1:80
  • Webserver (eg Apache) listens for port 80 and accepts request
  • Apache routes request to server-side script

This is the journey as the web server sees it. It needs to read the request before it even hits the server-side scripting language (PHP/Python/Ruby etc).

The server-side languages can then re-read the URL once the webserver has hit the front controller as they please.

mod_rewrite Clean URL rewriting

You can combine two line in one line:

RewriteEngine On
RewriteRule ^([a-zA-Z0-9-]+)/?$ something.php?query=$1

In this case / will be optional

How to hide the URl query string?

I think you are talking about Friendly URL. Maybe the below links will help you.

https://tracker.moodle.org/browse/MDL-28030

https://www.moodleworld.com/add-clean-urls-to-your-moodle-site-using-this-clean-urls-plugin-brendanheywood-moodle-moodledev/

From the above-mentioned plugin, your URL will be converted as below.

Messy URL: http://example.com/course/view.php?id=1

Clean URL: http://example.com/course/1

Mod Rewrite - Clean URL with query string not working

You can use the following single rule :

    Options +FollowSymlinks
RewriteEngine On
DirectoryIndex api.php
FallbackResource index.php
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^api/([^/]+)/([^/]+)/([^/]+)/?$ /api/api.php?endpoint=$1&id=$2&endpoint2=$3 [QSA,L]

Getting query string parameters from clean/SEO friendly URLs with JavaScript

How about something like this? It splits the path and keeps shifting off the first element of the array as it determines key/value pairs.

function getPathVariable(variable) {  var path = location.pathname;
// just for the demo, lets pretend the path is this... path = '/images/awesome.jpg/page/about'; // ^-- take this line out for your own version.
var parts = path.substr(1).split('/'), value;
while(parts.length) { if (parts.shift() === variable) value = parts.shift(); else parts.shift(); }
return value;}
console.log(getPathVariable('page'));


Related Topics



Leave a reply



Submit