htaccess重写删除尾部斜杠

时间:2016-09-20 23:31:29

标签: apache .htaccess mod-rewrite url-rewriting

我有以下重写规则,它们完美无缺

RewriteEngine On
RewriteRule ^([^/]*)/([^/]*)/([^/]*)\/$ /social/single_post.php?name=$1&t=$2&id=$3 [L]
RewriteRule ^([^/]*)\/$ /social/user.php?user=$1 [L]

示例:http://myurl.com/username/指向http://myurl.com/social/user.php?user=username

我想从第二个重写规则中删除尾部斜杠,因此它将返回如下:

示例:http://myurl.com/username指向http://myurl.com/social/user.php?user=username

RewriteRule ^([^/]*)$ /social/user.php?user=$1 [L]

问题在于,当我这样做时,整个域都会重定向

所以: http://myurl.com/指向http://myurl.com/social/user.php?user=

http://myurl.com指向http://myurl.com/social/user.php?user=

显然我不希望最后两件事情发生。如何修改我的htaccess以达到预期的效果?

1 个答案:

答案 0 :(得分:1)

试试这个,

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]*)/([^/]*)/([^/]*)\/$ /social/single_post.php?name=$1&t=$2&id=$3 [L]

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]*)\/$ /social/user.php?user=$1 [L]