How to redirect or White space automatically to + or - with htaccess?

前端 未结 2 1310
一向
一向 2020-12-10 00:04

I need to redirect automatically lots of url like /file%20name/ ore /file name/ to /file+name/ or /file-name/.

How can I do this with .htacess ?

相关标签:
2条回答
  • 2020-12-10 00:41

    Try this rule in your .htaccess:

    RewriteEngine on
    Options +FollowSymlinks
    
    # executes repeatedly as long as there are more than 1 spaces in URI
    RewriteRule "^(\S*)\s+(\S* .*)$" $1+$2 [N,NE]
    
    # executes when there is exactly 1 space in URI
    RewriteRule "^(\S*)\s(\S*)$" /$1+$2 [L,R=302,NE]
    
    • R=301 will redirect with https status 301

    • L will make last rule

    • NE is for no encoding URI

    0 讨论(0)
  • 2020-12-10 00:53

    You can use something like this:

    RewriteRule file\ name\ http://example.com/file+name [R=301,NC,NE,L]
    

    Edit after comment:

    Try this:

    RewriteRule ^([^\ ]*)\ (.*)$ $1-$2 [E=rspace:yes,N]
    
    0 讨论(0)
提交回复
热议问题