Apache htaccess用破折号替换空格和点
好的,我已经有了我的htaccess代码可以使用到一定程度。。。现在我被卡住了 以下是原始URL: example.com/search/store_info.php?store=113&dentor=Dr.%20John%20Doe 我试图实现的是一个干净的URL,没有点/句点或空格(%20),如下所示: example.com/search/113/dr-john-doe 但是,对于我当前使用的htaccess代码,我得到了以下结果: example.com/search/113/dr 医生的名字是从数据库中提取出来的,每个“医生”后面都有一个点(.),所以从某种意义上说,这就是过程停止的地方。我猜是因为这个点 以下是我拥有的htaccess代码:Apache htaccess用破折号替换空格和点,apache,.htaccess,mod-rewrite,url-rewriting,Apache,.htaccess,Mod Rewrite,Url Rewriting,好的,我已经有了我的htaccess代码可以使用到一定程度。。。现在我被卡住了 以下是原始URL: example.com/search/store_info.php?store=113&dentor=Dr.%20John%20Doe 我试图实现的是一个干净的URL,没有点/句点或空格(%20),如下所示: example.com/search/113/dr-john-doe 但是,对于我当前使用的htaccess代码,我得到了以下结果: example.com/search/113/dr 医生
RewriteEngine On
RewriteBase /search/
RewriteCond %{THE_REQUEST} /store_info\.php\?store=([a-z0-9]+)&dentist= ([a-z0-9]+) [NC]
RewriteRule ^ %1/%2/? [R=301,L]
RewriteRule ^([a-z0-9]+)/([a-z0-9]+)/?$ store_info.php?store=$1&dentist=$2 [QSA,L,NC]
您可以使用以下规则:
Options -MultiViews
RewriteEngine On
RewriteBase /search/
# redirect internal URL to pretty URL
RewriteCond %{THE_REQUEST} /store_info\.php\?store=([a-z0-9]+)&dentist=([^\s&]+) [NC]
RewriteRule ^ %1/%2/? [L,NE,R=301]
# skip all files and directories from rules below
RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^ - [L]
# recursive rule to repeatedly convert DOT or space to hyphen
RewriteRule ^([a-z0-9]+)/([^\s.]*)[.\s]+(.*)$ $1/$2-$3 [NC,DPI,E=DONE:1]
# after all the hyphen conversion is done do a redirect
RewriteCond %{ENV:DONE} =1
RewriteRule ^([0-9a-z]+)/([^\s.]+)$ $1/$2 [R=301,NE,L]
# internally rewrite pretty URL to actual one
RewriteRule ^([a-z0-9]+)/([a-z0-9-]+)/?$ store_info.php?store=$1&dentist=$2 [QSA,L,NC]
这是错误的做法。您的web页面首先应该使用干净的URL生成,然后再将其转换回“丑陋的”内部使用的。@MarcB如果我不使用htaccess代码,我正在尝试生成干净的URL,它看起来会像
example.com/search/store\u info.php?store=113&dentiter=Dr.%20John%20Doe
,但我提供的htaccess代码在最后一刻切断了牙医的全名dot@anubhava当我现场推送代码时,代码在我这边起作用。。。但是,它将URL显示为example.com/search/113/dr.
,而不是医生的全名。您确定在牙医=
之后有空格吗?如图所示?@anubhava当我在牙医=
之后添加空格时,它抛出一个错误,如果没有空间,那么URL可以工作,但它只显示example.com/search/113/dr.
,而不是这个example.com/search/113/dr john doe
你知道我的意思吗?啊,太接近了!此代码在某种程度上起作用。。。它之所以有效,是因为它抓住了医生的全名。但是,有没有可能让医生的名字在名字中间加上破折号?例如:drjohndoe
而不是像这样的drjohndoe
破折号最终取代了通常在URL中的“%20”OK URL正常工作它以正确的方式显示,例如:drjohndoe
但是,现在页面已断开。。。没有显示css或图像,我使用了前面一个问题的其他答案中提到的css或图像OK我尝试了您提供的基本代码,得到了相同的结果。。。什么也没出现。原始代码有效,允许所有这些图像和CSS工作,即使我使用
,是的,我清除了缓存并在私人浏览器窗口中尝试了它。是的,它是在上面的代码中提供的。。。我复制了所有代码并将其实现到我的htaccess文件中。您提供的代码在
中,让我们看看。