htaccess编辑以避免重复

时间:2015-01-31 12:20:41

标签: regex apache .htaccess mod-rewrite

在我的网站上我有这个.htaccess代码

<IfModule mod_rewrite.c>
Options +FollowSymLinks
RewriteEngine On
RewriteBase /

RewriteCond %{HTTP_HOST} ^einfogarden.com$
RewriteRule (.*) http://www.einfogarden.com/$1 [R=301,L]

RewriteCond %{THE_REQUEST} \s/+single\.php\?title=([^\s&]+) [NC]
RewriteRule ^ %1/? [R=302,L,NE]

# convert %20 to -
RewriteRule "^(\S*) +(\S* .*)$" $1-$2 [N,NE]
RewriteRule "^(\S*) (\S*)$" $1-$2 [L,R=302,NE]

## Adding a trailing slash
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{THE_REQUEST} \s/+(.*?)[^/][?\s]
RewriteRule [^/]$ %{REQUEST_URI}/ [L,R=301]

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.+?)/?$ single.php?title=$1 [NE,L,QSA]
</IfModule>

我转换20% - 但问题是反斜杠“/”最后是根据“ - ”数重复,我怎么能得到一个反斜杠

1 个答案:

答案 0 :(得分:1)

你的规则如下:

Options +FollowSymLinks
RewriteEngine On
RewriteBase /

RewriteCond %{HTTP_HOST} ^einfogarden.com$
RewriteRule (.*) http://www.einfogarden.com/$1 [R=301,L]

RewriteCond %{THE_REQUEST} \s/+single\.php\?title=([^\s&]+) [NC]
RewriteRule ^ %1/? [R=302,L,NE]

## Adding a trailing slash
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{THE_REQUEST} \s/+(.*?)[^/][?\s]
RewriteRule [^/]$ %{REQUEST_URI}/ [L,R=302]

# convert %20 to -
RewriteRule "^(\S*) +(\S* .*)$" $1-$2 [L,NE]
RewriteRule "^(\S*) (\S*)$" $1-$2 [L,R=302,NE]

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.+?)/?$ single.php?title=$1 [NE,L,QSA]

即重新排列并删除N标志。