How to remove wordpress directory slash

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
RewriteCond %{ENV:REDIRECT_STATUS} ^$
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).

Reference:

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.