【问题标题】:Removing spaces and html special characters from URL with .htaccess使用 .htaccess 从 URL 中删除空格和 html 特殊字符
【发布时间】:2017-04-04 22:59:02
【问题描述】:

我需要一些 .htaccess 文件和 URL 重写方面的帮助。我在这里搜索了所有问题和答案,我坚持下去。

因此,我需要帮助将空格 %20 替换为 - 以创建对 SEO 友好的 URL。

当前网址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

【问题讨论】:

    标签: php apache .htaccess mod-rewrite


    【解决方案1】:
    // 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 -

    【讨论】:

    • 好的,我已经用 - 替换了空格,但我仍然没有找到干净的 url
    猜你喜欢
    • 1970-01-01
    • 2021-10-16
    • 2014-08-17
    • 1970-01-01
    • 1970-01-01
    • 2021-04-23
    • 1970-01-01
    • 2015-01-13
    • 1970-01-01
    相关资源
    最近更新 更多