Vanity URLs without trailing slashes on Apache

后端 未结 4 608
天命终不由人
天命终不由人 2021-01-21 18:13

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 r

相关标签:
4条回答
  • 2021-01-21 18:54

    You could try doing

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

    Which should work for any url

    0 讨论(0)
  • 2021-01-21 18:55

    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!!!!

    0 讨论(0)
  • 2021-01-21 18:56

    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]
    
    0 讨论(0)
  • 2021-01-21 18:58

    You need to disable directory slash

    Try :

    DirectorySlash Off
    
    RewriteEngine on
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule ^(.*)$ /profiles/$1 [NC,L]
    
    0 讨论(0)
提交回复
热议问题