I've been trying for some time to delete the slash at the end of the URL link, but it doesn't work. I searched a lot of examples but none of them solve my problem.
I'm using Silverstripe 4 and currently running on a local server. I have to remove the slash for SEO reasons.
My current URL is:
www.example.com/
// Need to be like below
www.example.com
I try via htaccess
Exampe from stackoveflow question
I put in /public/.htaccess
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)/$ /$1 [L,R] # <- for test, for prod use [L,R=301]
and when i visit homepage slash is there at the end.
I try via code in SiteTree
public function Link($action = null)
{
return rtrim(parent::Link($action), '/');
}
Above code remove slash at the end from all pages but on home page still there.
www.example.com/about-us (here removed)
www.exaple.com/ (here exists)
And also try via config file
Director::config()->set('alternate_base_url', rtrim(Environment::getEnv('SS_BASE_URL'), '/'));
But again slash exists at the end of the url on homepage.
Does someone have solution for this? Thanks!
Here is my full htaccess file
<IfModule mod_rewrite.c>
# Turn off index.php handling requests to the homepage fixes issue in apache >=2.4
<IfModule mod_dir.c>
DirectoryIndex disabled
DirectorySlash On
</IfModule>
SetEnv HTTP_MOD_REWRITE On
RewriteEngine On
# Enable HTTP Basic authentication workaround for PHP running in CGI mode
RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]
# Deny access to potentially sensitive files and folders
RewriteRule ^vendor(/|$) - [F,L,NC]
RewriteRule ^\.env - [F,L,NC]
RewriteRule silverstripe-cache(/|$) - [F,L,NC]
RewriteRule composer\.(json|lock) - [F,L,NC]
RewriteRule (error|silverstripe|debug)\.log - [F,L,NC]
# Process through SilverStripe if no file with the requested name exists.
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule .* index.php
# REMOVE SLASH AT THE END OF THE URL
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)/$ /$1 [L,R] # <- for test, for prod use [L,R=301]
</IfModule>