Vanity URLs without trailing slashes on Apache

北慕城南 提交于 2019-12-02 04:15:44

问题


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).


回答1:


You could try doing

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

Which should work for any url




回答2:


You need to disable directory slash

Try :

DirectorySlash Off

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



回答3:


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]



回答4:


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



来源:https://stackoverflow.com/questions/33228428/vanity-urls-without-trailing-slashes-on-apache

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!