htaccess用短划线替换空格和圆点

时间:2016-08-08 17:56:32

标签: apache .htaccess mod-rewrite url-rewriting

好的,我已经让我的htaccess代码达到了一定程度......现在我被卡住了。

以下是原始网址:

  

example.com/search/store_info.php?store=113&dentist=Dr.%20John%20Doe

我想要实现的是一个干净的网址,没有点/句点或空格(%20),如下所示:

  

example.com/search/113/dr-john-doe

但是,使用我目前正在使用的htaccess代码,我得到了这个结果:

  

example.com/search/113/dr。

医生名称是从数据库中提取的,每个" dr"后面都有一个点(。)所以这就是过程在某种意义上停止的地方。我因为点而猜?

这是我的htaccess代码:

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]

1 个答案:

答案 0 :(得分:1)

您可以使用以下规则:

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]