replace space by - using htaccess

后端 未结 2 1227
旧巷少年郎
旧巷少年郎 2020-12-18 15:13

hi i have this code in my htaccess


Options +FollowSymLinks
RewriteEngine On
RewriteBase /

RewriteCond %{THE_REQUEST} \\s/+sin         


        
2条回答
  •  太阳男子
    2020-12-18 15:42

    You could try replacing the first rule with something like:

    RewriteCond %{ENV:REDIRECT_STATUS} !200
    RewriteCond %{THE_REQUEST} \s/+single\.php\?title=([^\s&]+) [NC]
    RewriteRule ^ /%1? [L,NE]
    
    RewriteCond %{ENV:REDIRECT_STATUS} 200
    RewriteCond %{THE_REQUEST} \s/+single\.php\?title=([^\s&]+) [NC]
    RewriteRule ^(.*)(?:\ |\+)(.*)$ /$1-$2 [L,NE]
    
    RewriteCond %{ENV:REDIRECT_STATUS} 200
    RewriteCond %{THE_REQUEST} \s/+single\.php\?title=([^\s&]+) [NC]
    RewriteCond %{REQUEST_URI} !(\ |\+)
    RewriteRule ^(.*)$ /$1 [R=301,L,NE]
    

    So instead of just redirecting direct requests to the "/title", you first internally rewrite it to "/title" (first rule), then iteratively replace all spaces and "+"'s with dashes "-" (second rule). When there are no more spaces or "+"'s in the URI, redirect (third rule).

    Then you'd need to edit your single.php so that it can interpret "title"'s with dashes instead of spaces.

提交回复
热议问题