I would like to 301 redirect my URL that has a trailing slash to a URL without a trailing slash. That part is simple (RewriteRule ^(.*)\/(\?.*)?$ $1$2 [R=301,L])

However I only want to do it if that directory is /abc/

For example:

  • http://www.example.com/abc/cowboy/ should be 301ed to http://www.example.com/abc/cowboy
  • http://www.example.com/xyz/cowboy/ should stay the same and NOT be 301ed
  • Is this in .htaccess or your server config?
    – MrWhite
    Nov 8, 2014 at 17:22
  • 1
    I would prefer if it was in the server conf file. But either will do. Nov 8, 2014 at 18:06

1 Answer 1


In your server config, you can modify your current example to include the /abc/ directory to restrict redirects to that one directory:

RewriteRule ^/abc/(.*)/(\?.*)?$ /abc/$1$2 [R=301,L]

I've removed the backslash escape from the slash, in your example, as this would appear to be unnecessary.

Note that to make this work in .htaccess you would need to removed the slash prefix from the RewriteRule pattern.


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.