【问题标题】:htaccess redirect replacing %20 with hyphen only for file nameshtaccess 重定向将 %20 替换为仅用于文件名的连字符
【发布时间】:2016-02-23 21:17:49
【问题描述】:

我需要替换所请求 URL 的文件名中的 %20 个空格。

我正在使用这个正则表达式:^(.*)\s(.*)$,它替换了 URL 中的所有空格。我无法使其仅适用于文件名。

http://example.com/subfolder/One More Sub Folder/Onother Sub Folder/File%20test%20example.jpg

我在 wordpress 下,我需要一个 RewriteCondition 和 RewriteRule 来做到这一点:

http://example.com/wordpress/subfolder/One More Sub Folder/Onother Sub Folder/File-test-example.jpg

我也需要对 htm 请求进行相同的替换,但我也需要从文件名中删除 html 扩展名。

所以现在我有了这些适用于 html 的规则:

RewriteCond %{THE_REQUEST} (\.htm|\.html)
RewriteRule ^(.*)(\.htm|\.html)$ http://www.example.com/wordpress/$1 [R=301,L]

这些用于重定向 jpg:

RewriteCond %{REQUEST_URI} !^/wordpress/wp-content/uploads/
RewriteRule ^(.*\.jpg)$ /wordpress/wp-content/uploads/2016/02/$1 [R=301,L]

在 htaccess 文件的开头,我有 wp slug 工作所需的小写转换:

<IfModule mod_speling.c>
CheckSpelling on
</IfModule>

【问题讨论】:

    标签: regex wordpress .htaccess redirect mod-rewrite


    【解决方案1】:

    如果这些是存在的文件,您可以使用以下规则将 %20 替换为 - :

    RewriteCond %{REQUEST_FILENAME} -f
    RewriteRule ^(.*)\s(.*)$ /$1-$2 [NC,L,R]
    

    该规则仅适用于现有文件。

    【讨论】:

      【解决方案2】:

      你试过RewriteRule ^(.*)(%20)(.*)$ /$1-$2 [R=301,NE,L]吗?

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2012-09-29
        • 1970-01-01
        • 2013-10-20
        • 2011-08-14
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多