htaccess用 - 替换%20

时间:2014-04-12 20:31:25

标签: apache .htaccess mod-rewrite

请放轻松我,但我仍然是htaccess重写的新手,而且从来没有做过。

我试图用 - 在我的网址中替换%20。我似乎无法弄明白。

你能告诉我以下htaccess代码有什么问题吗?仅在URL重写部分

# Begin hotlink protection #
RewriteEngine on
RewriteCond %{HTTP_REFERER} !^http://webawwards.com/.*$ [NC]
RewriteCond %{HTTP_REFERER} !^http://www.webawwards.com/.*$ [NC]
RewriteCond %{HTTP_REFERER} !^http://webawwardscom.ipage.com/.*$ [NC]
RewriteCond %{HTTP_REFERER} !^http://www.webawwardscom.ipage.com/.*$ [NC]
RewriteRule .(gif|jpg|png)$ - [F]
# End hotlink protection #


# Begin cache control #
ExpiresActive on
ExpiresDefault "now plus 240 minutes"
ExpiresByType text/html "now plus 240 minutes"
<FilesMatch "\.(css|png|bmp|ico|htm|gff|html|js|jpg|jpeg|gif|gcf)$">
FileETag MTime Size
ExpiresDefault "now plus 240 minutes"
</FilesMatch>


# End cache control #

# Url rewritting start #
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ $1.php [NC,L]
RewriteCond %{HTTP_HOST} ^webawwards\.com [NC]
RewriteRule (.*) http://www.webawwards.com/$1 [L,R=301]

# remove spaces from start or after /
RewriteRule ^(.*/|)[\s%20]+(.+)$ $1$2 [L]

# remove spaces from end or before /
RewriteRule ^(.+?)[\s%20]+(/.*|)$ $1$2 [L]

# replace spaces by - in between
RewriteRule ^([^\s%20]*)(?:\s|%20)+(.*)$ $1-$2 [L,R]

# Url rewrite end #


# Enable gzip compression
<ifModule mod_gzip.c>
 mod_gzip_on Yes
 mod_gzip_dechunk Yes
 mod_gzip_item_include file \.(html?|txt|css|js|php|pl)$
 mod_gzip_item_include handler ^cgi-script$
 mod_gzip_item_include mime ^text/.*
 mod_gzip_item_include mime ^application/x-javascript.*
 mod_gzip_item_exclude mime ^image/.*
 mod_gzip_item_exclude rspheader ^Content-Encoding:.*gzip.*
</ifModule>

它似乎无法奏效。

我希望将www.domain.com/blog/post.php?title=text%headline替换为www.domain.com/blog/title-headline

我知道上面的代码是错误的,但我无法弄清楚

的位置

1 个答案:

答案 0 :(得分:1)

试试这个

RewriteCond %{THE_REQUEST} ^(GET|POST)\ /blog/post\.php\?title=(.*)%20(.*)\ HTTP
RewriteRule ^ /blog/%2-%3? [R=301,L]

因此,如果title包含空格,则会将其重写为-