使用.htaccess从URL中删除空格和html特殊字符

时间:2017-04-04 22:59:03

标签: php apache .htaccess mod-rewrite

我需要帮助.htaccess文件和URL重写。我在这里搜索了所有问题和答案,我坚持不懈。

所以我需要帮助用 - 替换白色空格%20来制作SEO友好网址。

当前网址http://localhost/blog/Blog%20of%20the%20day

这是我的.htaccess文件

RewriteEngine On
# Removes .php extension from inner_blog.php
RewriteRule ^blog/?$ blog.php [NC]
# Remove inner_blog.php and changing it to blog/articlename
RewriteRule ^blog/([^/]*)$ /inner_blog.php?title=$1 [L]
# Need to replace %20 with - from URL
# Current URL http://localhost/blog/Blog%20of%20the%20day
# Need to be  http://localhost/blog/Blog-of-the-day
# Was like    http://localhost/inner_blog.php?title=Blog%20of%20the%20day

1 个答案:

答案 0 :(得分:0)

// Start or After "/"
RewriteRule ^(.*/|)[\s%20]+(.+)$ $1$2 [L]

// End or Before "/"
RewriteRule ^(.+?)[\s%20]+(/.*|)$ $1$2 [L]

我在之前的项目中遇到过这个问题,并从这个帖子得到答案。

供参考:htaccess clean urls & replacing whitespaces and %20 with -