2

The code below rewrites all URLs in the /profiles/ directory on our site from example.com/profiles/name/ to example.com/name/, but we'd also like to remove the trailing slashes to further simplify the resulting URLs to the prettier example.com/name -- just like on modern social media.

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /profiles/$1 [NC,L]

How can this be done (and done safely)? We have seen several solutions on Stumble Upon that if combined possibly could work, but all profiles on our site currently have their own physical directories rather than being assembled on the fly by a script.

Update: @jon-lin offered a solution to a similar situation at How to access directory's index.php without a trailing slash AND not get 301 redirect -- but we didn't figure out how to apply it to ours (described above).

haadaa
  • 87
  • 11

4 Answers4

1

You could try doing

RewriteRule ^(.*)/+$ $1 [R=301,L]

Which should work for any url

NooBskie
  • 3,761
  • 31
  • 51
  • Hey @NooBskie! We just tried, but, testing this out on Google's Chrome, `example.com/name/` redirected to a URL containing the entire internal directory structure and `example.com/name` to Google search. – haadaa Oct 20 '15 at 05:26
0

Use the following redirection:

RewriteEngine On

RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} /(.+)/+$
RewriteRule ^ /%1 [R=301,L]

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^.*$ /profiles/$0 [NC,L]
hjpotter92
  • 78,589
  • 36
  • 144
  • 183
  • Hey @hjpotter92 -- this produced the same errors as with NooBskie's solution. – haadaa Oct 20 '15 at 06:10
  • Thanks! This again redirects back to Google search on Chrome; on Safari and Firefox, it redirects `example.com/name` back to `example.com/profiles/name`. – haadaa Oct 20 '15 at 06:55
  • @haadaa Could you elaborate what you mean by: "_This again redirects back to Google search on Chrome_"? – hjpotter92 Oct 20 '15 at 07:11
  • Hey @hjpotter92 Sure -- with your code in place, Google's Chrome browser treats `example.com/name` as a search for `example.com/name` -- that is, entering `example.com/name` in the combined search/URL bar, Chrome displays the search results for `example.com/name` rather than the page (and its errors). I assume this is their way to protect sites from showing the content of their directories (which indeed is a risk if the trailing slashes aren't removed correctly). – haadaa Oct 20 '15 at 17:36
  • Hey @hjpotter92 As all our profiles have their own physical pages at `example.com/profiles/name/index.html`, is it possible to do this with something similar to `RewriteRule ^.*$ /profiles/$0/index.html [NC,L]`? – haadaa Oct 20 '15 at 17:49
0

You need to disable directory slash

Try :

DirectorySlash Off

RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /profiles/$1 [NC,L]
Amit Verma
  • 40,709
  • 21
  • 93
  • 115
  • Hey @Starkeen! This didn't work -- `example.com/name` now got a "Forbidden -- You don't have permission to access /profiles/name on this server." – haadaa Oct 20 '15 at 06:07
0

By adding part of the code suggested by @jon-lin at How to access directory's index.php without a trailing slash AND not get 301 redirect (internally rewriting the trailing slash back in), we actually made this work:

# Vanity URLs

DirectorySlash Off

RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ /profiles/$1 [NC,L]

RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^(.*[^/])$ /$1/

The profile for Gucci on FASHION NET (located at /profiles/gucci/) can now be accessed at https://www.fashion.net/gucci -- with no trailing slash! Thank you, @jon-lin!!!!

haadaa
  • 87
  • 11