我在这个网站上尝试了很多选项,但无法解决我的问题。 我的代码:
->
htaccess的:
<?php
$district = $_GET['district'];
$state = $_GET['state'];
$con = mysqli_connect('localhost','codeslib_service','service','codeslib_vdr123');
if (!$con) {
die('Could not connect: ' . mysqli_error($con));
}
// the query, limit the number of results to 5
$sql="SELECT * FROM pincodes WHERE districtName='$district' AND stateName='$state'";
$result = mysqli_query($con,$sql);
$row = $result->fetch_assoc();
$district = $row['districtName'];
$office = $row['officeName'];
$state = $row['stateName'];
$pincode = $row['pinCode'];
?>
现在,上面的代码生成了RewriteRule ^([^/]*)/([^/]*)$ district.php?state=$1&district=$2 [L]
虽然我不想在网址中占用空间,但想用连字符( - )替换空格。 尝试了很多事但没有收获,请帮助我。
谢谢,
维卡斯
答案 0 :(得分:0)
尝试在htaccess
中的任何其他规则之前编写此代码RewriteEngine On
RewriteRule ^/?/(.*)\ (.*)$ /$1-$2 [L,R=301]
这会将空格从URL替换为连字符( - )
答案 1 :(得分:0)
您是否尝试过str_replace?
如str_replace(“”,“ - ”,$ url);
答案 2 :(得分:0)
# keep replacing space to hyphen until there is no space use internal rewrite
RewriteRule ^([^\s%20]*)[\s%20]+(.*)$ $1-$2 [E=NOSPACE:1]
# when there is no space make an external redirection
RewriteCond %{ENV:NOSPACE} =1
RewriteRule ^([^\s%20]+)$ $1 [R=301,L]
这会将所有空格字符(\s
或%20
)替换为连字符-
因此/tag/bob%20hope%20is%20funny
的URI将变为/tag/bob-hope-is-funny
,其中包含301
答案 3 :(得分:0)
要用连字符替换所有空格,您可以在.htaccess顶部使用此规则:
RewriteEngine On
# redirect when only one space is left
RewriteRule "^(\S*)\s+(\S*)$" /$1-$2 [L,R=302]
# otherwise rewrite by replacing each space by -
RewriteRule "^(\S*)\s+(\S*\s+.*)$" $1-$2 [L]
答案 4 :(得分:0)
最后破解了它,我以下列方式使用了str_replace:
str_replace(&#34; - &#34;,&#34;&#34;,$ url);
在google和论坛上,我注意到第一个值是空格然后是连字符,我将其反转以使用基于连字符的网址:)