2017-07-02 7 views
4

ページのURLクエリを変更して最初の2文字を取得するにはどうすればよいですか。ページURLを変更して最初の2文字を取得する

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

htaccessのコード:.htaccessファイルやPHP、このよう

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

127.0.0.1/site/.htaccess

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

なぜ '/ site /'に最初のスラッシュの後に '+'がありますか?どのように '/site/flowers/index.php?query = Bird' URLを取得していますか?それはいくつかの形式の投稿やhrefリンク経由ですか? – anubhava

+0

@anubhava、サー..フォーム投稿/site/flowers/index.php?query=Bird ....(アドレスバー) – moon93

+0

.htaccesが見つかりました> ... 127.0.0.1/site/.htaccess – moon93

答えて

1

は、このリダイレクトのために使用することができます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> 

次にあなたがsite/.htaccessでこのルールを持つことができます。

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

これはにあなたのURLを変換します:

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

検索フィールドに単語birdを使用して検索を実行すると。

+1

ご協力いただきありがとうございます。 – moon93

1

あなたに以下を追加します。 htaccess

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

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

はその後http://127.0.0.1/site/flowers/Bird.htmlは、順番にhttp://127.0.0.1/site/flowers/search/Bi/Bird.html

私は、これはあなたが望むものであると仮定していますにリダイレクトhttp://127.0.0.1/site/flowers/index.php?query=Birdにリダイレクト?ここで

関連する問題