Mobile app version of vmapp.org
Login or Join
Nickens628

: Removing slash from directory URL I need to somehow remove the last slash in my directory URLs. For example, instead of example.com/projects/, I'd like to see example.com/projects. I have been

@Nickens628

Posted in: #CleanUrls #Htaccess #Url

I need to somehow remove the last slash in my directory URLs. For example, instead of example.com/projects/, I'd like to see example.com/projects. I have been trying many solutions with no luck so far. Here is my .htaccess file currently:

Options +FollowSymLinks
DirectoryIndex index.html

RewriteEngine on

RewriteCond %{HTTP_HOST} ^example.com [NC]
RewriteRule ^(.*)$ www.example.com/ [R=301,L]

RewriteCond %{THE_REQUEST} /index.html [NC]
RewriteRule ^(.*?)index.html$ / [L,R=301,NC,NE]

RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{DOCUMENT_ROOT}//index.html -f [NC]
RewriteRule ^(.+?)/?$ //index.html [L]

ErrorDocument 404 www.example.com/error404.html

10.01% popularity Vote Up Vote Down


Login to follow query

More posts by @Nickens628

1 Comments

Sorted by latest first Latest Oldest Best

 

@Jamie184

Presumably you are already linking to the directory URLs without the trailing slash? That is the first step.

Since these are physical directories you need to tell mod_dir to not "fix" these URLs by appending a trailing slash. You can do this with the DirectorySlash directive near the top of your file:

DirectorySlash Off


This means that you now need to manually append the trailing slash with an internal rewrite. Note that mod_dir ordinarily 301 redirects to the slashed URL, so this will have been cached by the browser. Ensure your browser cache is clear before testing.


RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{DOCUMENT_ROOT}//index.html -f [NC]
RewriteRule ^(.+?)/?$ //index.html [L]



The main problem with this is that the first RewriteCond directive prevents directories from being rewritten, which would seem to be the opposite of what you are trying to achieve. Try the following instead:

RewriteCond %{REQUEST_FILENAME} -d
RewriteCond %{DOCUMENT_ROOT}//index.html -f
RewriteRule ^(.+?)/?$ //index.html [L]



ErrorDocument 404 www.example.com/error404.html


Aside: You should specify a root-relative URL to your error document, like:

ErrorDocument 404 /error404.html


If you use an absolute URL Apache will trigger an external redirect to your error document (instead of an internal subrequest) which is generally undesirable.

10% popularity Vote Up Vote Down


Back to top | Use Dark Theme