I have installed a website in my domain directory like example.com/website.

The main issue or problem is that it redirects me to example.com/website/.

I never want redirection and I just want that my subdirectory website never redirects me.

I want to do it so that the user can access my website without the last slash it means example.com/website.

Can I do this using a PHP function or also kindly share .htaccess code if it is possible.

I have read many articles or even question asked by users on stack exchange related trailing slash but no benefit.

Also I have tried out various .htaccess codes but no benefit.

Kindly help me with this.

  • Have you tried: RewriteCond %{REQUEST_FILENAME} !-d RewriteRule ^(.*)/$ /$1 [L,R] – phillsav_ Feb 21 at 1:52
  • I have checked it what this not work. kindly share the full htaccess code. – Duni Chand Feb 21 at 8:42
  • @phillsav_ That directive targets non-directories, so it will never match the directory in which WordPress is installed. However, if you simply try to remove the trailing slash on directories without first disabling the DirectorySlash then it will trigger a redirect loop as it will conflict with mod_dir. (You need to prevent the trailing slash being appended in the first place.) – MrWhite Feb 22 at 1:59

The main issue or problem is that it redirects me to example.com/website/

By default Apache (mod_dir) will "fix" the URL with a 301 redirect to append the trailing slash if it is omitted from a filesystem directory. This is necessary for other Apache features to function correctly...

  • without the trailing slash, the DirectoryIndex document is not served and a 403 Forbidden response will result (unless Options Indexes is enabled in which case you'll get a directory listing despite the presence of an index document - a potential security vulnerability, see below).

  • If the request is /website (no trailing slash) then mod_rewrite directives in the /website/.htaccess file will not trigger (so the WordPress front-controller will fail).

It is possible to prevent Apache (mod_dir) from appending the trailing slash on the /website directory (using DirectorySlash Off), however, we must then take additional steps to workaround the above issues and manually append the trailing slash (or rewrite directly to the WP front-controller) with an internal rewrite. We also can't do this from within the /website/.htaccess file - this must be moved to the document root (the parent directory).

For example:

  1. Move the existing WordPress /website/.htaccess file to the parent directory (document root), ie. /.htaccess.

  2. You will then need to make changes like the following to the /.htaccess file and the WordPress front-controller:

# mod_autoindex must be disabled
# Otherwise your site will be vulnerable to "information disclosure"
Options -Indexes

# Prevent mod_dir appending the trailing slash to filesystem directories
DirectorySlash Off

# BEGIN WordPress
RewriteEngine On
RewriteBase /website

# Remove trailing slash from "/website/" if requested directly
RewriteRule ^(website)/$ /$1 [R=301,L]

# Route requests for the directory itself (ie. the "homepage")
# (The DirectoryIndex is not otherwise triggered.)
RewriteRule ^website/?$ index.php [L]

# Front controller for all other URLs of the form "/website/<something>"
RewriteRule ^[^/]+/index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f 
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^website/. index.php [L]
# END WordPress

Since we've had to customise the # BEGIN WordPress section, you must prevent WordPress from overwriting this and creating another (default) .htaccess file in the /website/ subdirectory.

The above will also canonicalise any requests for /website/ (with a trailing slash) and redirect to /website in order to remove the trailing slash. You should test first with a 302 (temporary) redirect in order to avoid potential caching issues.

You will need to clear your browser cache before testing since the earlier 301 redirect (by mod_dir/Apache) to append the trailing slash will have been cached by the browser.

Note that the DirectorySlash Off directive applies to all subdirectories. So, if any other subdirectories should be accessible (unlikely I would think) then you will need to take additional steps.

Can i do this using a php function

The appending of the trailing slash has nothing to do with PHP - it is an Apache "feature". However, you do need to ensure that WordPress is still able to route the URL, since the requested URL-path is now /website, and not /website/ (which is technically a different URL).


Security Warning
Turning off the trailing slash redirect may result in an information disclosure. Consider a situation where mod_autoindex is active (Options +Indexes) and DirectoryIndex is set to a valid resource (say, index.html) and there's no other special handler defined for that URL. In this case a request with a trailing slash would show the index.html file. But a request without trailing slash would list the directory contents.

  • It shows me ERR_TOO_MANY_REDIRECTS – Duni Chand Feb 22 at 13:29
  • What URL are you requesting? What is the nature of the redirect loop? What is it redirecting from/to? Have you cleared your browser cache? Please add the contents of your .htaccess file (now located in the document root) to your question. – MrWhite Feb 22 at 13:40

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.