简体   繁体   中英

htaccess rewrite remove trailing slash

I have the following rewrite rules which work perfectly

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

Example: http://myurl.com/username/ points to http://myurl.com/social/user.php?user=username

I want to remove the trailing slash from the second rewrite rule so it will return as follows:

Example: http://myurl.com/username points to http://myurl.com/social/user.php?user=username

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

The problem is when I do this, the entire domain is redirecting

so: http://myurl.com/ points to http://myurl.com/social/user.php?user=

and http://myurl.com points to http://myurl.com/social/user.php?user=

Obviously I don't want those last two things happening. How can I modify my htaccess to achieve the desired outcome?

Try this,

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]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM