简体   繁体   中英

remove trailing slash with htaccess

I want to remove file endings (.html) and the trailing slashes in urls. When the user enters the requests ttp://example.com/jobs/director.html he should be redirected to http://example.com/jobs/director . This works perfectly for most cases, but when the page name equals the name of a subdirectory the trailing slash isn't removed and the server tries to resolve the path like this: /jobs/.html (the request in this case was http://example.com/jobs/ ). Does anybody know a solution? I am really stuck. Thanks!

My file structure:

  • jobs (directory)

  • jobs/artist.html

  • jobs/director.html

  • index.html

  • jobs.html

My .htaccess file:

Options +FollowSymLinks
RewriteEngine On
DirectorySlash Off

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

# remove .html file ending
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ $1.html [NC,L]

You can use:

Options +FollowSymLinks
RewriteEngine On
DirectorySlash Off

# remove trailing slash to non-directories
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.+?)/$ /$1 [R=302,L]

# add trailing slash internally to directories
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^(.+?[^/])$ $1/ [L]

# add .html file ending internally
RewriteCond %{DOCUMENT_ROOT}/$1\.html -f
RewriteRule ^(.+?)/?$ $1.html [L]

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM