Bruce Bruce - 2 months ago 10
Apache Configuration Question

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?

Answer

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]
Comments