.htaccess用 - ("%20"到" - ")

时间:2016-02-12 17:23:35

标签: .htaccess mod-rewrite

我想删除链接上的%20 - (破折号),

我的.htaccess就是这样,

RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ icerik.php?name=$1 [QSA,L]

例如,

site.com/vision-and-mision, site.com/do-re-mi-fa-so-la-si。

实际上我搜索了一些东西,但信息非常具体,我很困惑

谢谢。

1 个答案:

答案 0 :(得分:3)

您可以在/.htaccess文件中使用以下内容:

RewriteEngine On

# Replace whitespace with hyphens, set the environment variable,
# and restart the rewriting process. This essentially loops
# until all whitespace has been converted.

RewriteRule ^([^\s]*)\s(.*)$ $1-$2 [E=whitespace:yes,N]

# Then, once that is done, check if the whitespace variable has
# been set and, if so, redirect to the new URI. This process ensures
# that the URI is rewritten in a loop *internally* so as to avoid
# multiple browser redirects.

RewriteCond %{ENV:whitespace} yes
RewriteRule (.*) /$1 [R=302,L]

然后添加规则:

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /icerik.php?name=$1 [QSA,L]

如果这对您有用,并且您希望将浏览器和搜索引擎缓存重定向,请将302更改为301