简体   繁体   English

更改页面URL并获取前两个字母

[英]Changing the page URL and get first two letters

How can I change page url query and get first two letters . 如何更改页面URL查询并获取前两个字母。 in .htaccess or php 在.htaccess或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 Htaccess代码: 127.0.0.1/site/.htaccess

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

Add the following to your .htaccess 将以下内容添加到.htaccess中

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

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

Then http://127.0.0.1/site/flowers/Bird.html redirects to http://127.0.0.1/site/flowers/index.php?query=Bird which in turn redirects to http://127.0.0.1/site/flowers/search/Bi/Bird.html 然后http://127.0.0.1/site/flowers/Bird.html重定向到http://127.0.0.1/site/flowers/index.php?query=Bird ,后者又重定向到http://127.0.0.1/site/flowers/search/Bi/Bird.html

I'm assuming this is what you want? 我假设这是你想要的?

Here is a Javascript code that you can use for this redirection: 这是一个可用于此重定向的Javascript代码:

<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 have this rule in site/.htaccess : 然后你可以在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 search using word bird in search field. 在搜索栏中使用单词bird进行搜索时。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM