Change the page URL and get the first two letters

How can I change the request for a url and get the first two letters. in .htaccess or php

http://127.0.0.1/site/flowers/index.php?query=Bird
http://127.0.0.1/site/flowers/index.php?query=eagle

Like this:

http://127.0.0.1/site/flowers/search/bi/bird.html
http://127.0.0.1/site/flowers/search/ea/eagle.html

Htaccess Code: 127.0.0.1/site/.htaccess

RewriteEngine on 
RewriteRule ([^/\.]+)/?.html$ index.php?query=$1 [L]
+4
source share
2 answers

Here is the Javascript code you can use for this redirect:

<html>
<head>
<title>Form Post Example</title>
<script>
function prettySubmit(form, evt) {
   evt.preventDefault();
   var val = form.query.value.toLowerCase();
   window.location =
          form.action + '/' + val.substr(0, 2) + '/' + val.replace(/ /g, '+') + '.html';
   return false;
}

</script>
</head>
<body>
<form method="get" action="flowers/search" onsubmit='return prettySubmit(this, event);'>
   <input type="text" name="query">
   <input type="submit" value="Search">
</form>
</body>
</html>

Then you can get this rule in site/.htaccess:

RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ([^/.]+)\.html$ index.php?query=$1 [L,QSA,NC]

This will convert your urls to:

http://127.0.0.1/site/flowers/search/bi/bird.html

When you perform a search using the word birdin the search box.

+1
source

Add the following to your .htaccess

RewriteRule ([^/\.]+)/?.html$ index.php?query=$1 [R=301]

RewriteCond  %{QUERY_STRING} query=([^/\.]{2})([^/\.]+)
RewriteRule index.php search/%1/%1%2.html [R=301]

It then http://127.0.0.1/site/flowers/Bird.htmlredirects to http://127.0.0.1/site/flowers/index.php?query=Bird, which in turn redirects tohttp://127.0.0.1/site/flowers/search/Bi/Bird.html

, , ?

+1

Source: https://habr.com/ru/post/1680580/


All Articles