Remove Trailing Slash With .htaccess - .htaccess

I've looked at 5 pages of Google, and StackOverflow, and I can't seem to find a way to remove the trailing slash on my domain using .htaccess. It uses Litespeed. Can anyone provide me with code I can add to my file? I'll reply to say whether or not it worked. Thanks!

they're physical directories.
You can't "simply" remove the trailing slash from physical filesystem directories. And I would recommend against trying to do so.
This is very different to trailing slashes on arbitrary "virtual" URLs that do not map to physical file-paths.
The trailing slash on directories is required by Apache to be able to correctly serve the DirectoryIndex document (mod_dir) and generate directory listings (mod_autoindex).
By default mod_dir explicitly appends the trailing slash to directories (if omitted) by issuing a 301 (permanent) redirect which is cached persistently by the browser.
In order to remove the trailing slash on directories (or prevent it from being appended) you need to override this default behaviour. But you must then manually append the trailing slash with an internal rewrite, so the URLs work as intended.
It is relatively trivial to prevent mod_dir appending the trailing slash:
# Prevent mod_dir from appending trailing slashes to directories
DirectorySlash Off
# Prevent mod_autoindex generating directory listings
Options -Indexes
Ensuring that directory indexes are disabled is an additional security measure when DirectorySlash Off is set, since without a trailing slash the presence of a DirectoryIndex document does not prevent mod_autoindex from generating a directory listing and exposing your file structure.
However, you now need to issue internal rewrites to "silently" append the trailing slash if a physical directory is requested without, for example:
# Append trailing slash on directories with an internal rewrite
RewriteCond $1 !/$
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule (.+) $1/ [L]
Having implemented the above you should already be requesting URLs/directories without the trailing slash. However, if you are changing this behaviour and the URL with the trailing slash has been indexed by search engines or linked to by third parties then you also need to implement an external redirect to remove the trailing slash in order to preserve SEO. For simplicity, I assume you would want to remove the trailing slash from all URLs (not just physical directories).
For example, the following would need to go before the above rewrite:
# Remove trailing slash if requested directly
# NB: Applies to ALL URLs, including directories
RewriteCond %{ENV:REDIRECT_STATUS} ^$
RewriteRule (.+)/$ /$1 [R=301,L]
The condition that checks against the REDIRECT_STATUS environment variable is required in order to prevent a redirect loop. We only want to redirect direct requests from the client and not rewritten requests by the later rewrite.
NB: Test with 302 (temporary) redirects to avoid potential caching issues.
You will need to clear your browser (and any intermediary) caches before testing.
However, as I noted at the top of my answer, unless you have a good reason for doing this then I would recommend against implementing this as you could encounter unexpected conflicts. At the end of the day, Apache needs that trailing slash on physical filesystem directories.
Aside:
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)/$ /$1 [L,R=301]
This removes the trailing slash from an arbitrary URL-path. This is your "standard" slash removal rule. However, this will not remove the trailing slash from physical directories, as you are trying to do. For two reasons:
The condition explicitly checks that the request does not map to a physical directory.
Removing the slash from a physical directory without first setting DirectorySlash Off will result in a redirect-loop since mod_dir will naturally try to append it again.

You can remove the trailing slash on your URL’s with the following in your .htaccess:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)/$ /$1 [L,R=301]
For your testing, i’d recommend using R=302 so it’s not a permanent redirect as some browsers will cache these indefinitely without a full cache clear and in prod use R=301.

Related

How to exclude trailing slash on source URL when redirecting?

I have to make a 301 redirect from a domain to another (example-a.com to example-b.com).
This is the code I put in .htaccess of example-a.com:
RewriteEngine on
RewriteRule ^(.*)$ https://www.example-b.com/$1 [R=301,L]
All URLs are the same, except pages of example-a.com all have a / (slash) at the end of the URL, but example-b.com does not. So how to redirect without the ending / if the URL contains one?
RewriteRule ^(.*)$ https://www.example-b.com/$1 [R=301,L]
To exclude the slash from the redirected URL, then exclude the slash from the captured group in the RewriteRule pattern. eg. ^(.*)/$.
However, assuming you also want the document root to redirect, where there is no slash in the URL-path, then you need to make the trailing slash optional (or create an entirely separate rule). But in that case you need to also make the capturing group non-greedy, otherwise the trailing slash will always be included in the captured group (since regex is greedy by default).
So, try the following instead:
RewriteRule ^(.*?)/?$ https://www.example-b.com/$1 [R=302,L]
You will need to clear your browser cache before testing. Since the earlier 301s will have likely been cached.
Note that this is a 302 (temporary) redirect. Only change this to a 301 (permanent) redirect - if that is the intention - once you have confirmed that it works OK. This is to avoid the browser caching erroneous redirects whilst testing.

mod_alias redirects adding two trailing slashes

I have a number of URLs which may be entered with or without a trailing slash. They're used as vanity URLs.
.htaccess looks like this:
Redirect 301 /folder/vanity1 http://example.com/differentfolder/
Redirect 301 /folder/vanity2 http://example.com/a/third/folder/
Redirect 301 /vanity3 http://example.com/fourth/folder/
Users type in:
http://example.com/folder/vanity1
http://example.com/folder/vanity2
http://example.com/vanity3
http://example.com/folder/vanity1/
http://example.com/folder/vanity2/
http://example.com/vanity3/
When users type these in with no trailing slash, the redirects are working correctly, ending up at http://example.com/differentfolder/ with one trailing slash as desired.
The problem:
When users type these in with a trailing slash, such as http:/example.com/folder/vanity1/ they redirect to a URL with two trailing slashes, such as http://example.com/differentfolder// which throws a 404 error.
I have tried:
Removing all other lines from .htaccess, except these three vanity URLs and the standard WordPress block required to keep the site functioning. The WordPress block appears at the very end of the file:
# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
# END WordPress
I checked not only with my browser but with WebConfs HTTP Header Check to make sure I wasn't seeing a cached rule. Still the same, if you type a vanity URL with no trailing slash it works; if you type it with one trailing slash, it redirects to two trailing slashes which causes a 404.
Adding a trailing slash in my rules:
Redirect 301 /folder/vanity1/ http://example.com/differentfolder/
This works fine if the user types the trailing slash, but if they leave off the slash it does not redirect and 404s because the URL http://example.com/folder/vanity1/ does not actually exist.
Question:
Is there a different way to 301 redirect the vanity URLs to a final destination with only one trailing slash, no matter which way the visitor types in the vanity URL - with or without a trailing slash?
This works as documented in Redirect,
Additional path information beyond the matched URL-path will be appended to the target URL.
This means /folder/vanity1 works only as a prefix, and an additional slash or anything else in the requested URL will be appended to the target, e.g. http://example.com/folder/vanity1/hi/there would result in http://example.com/differentfolder//hi/there
If you want to match exactly these two URLs, /folder/vanity1 and /folder/vanity1/, you must use either RedirectMatch or mod_rewrite
With a RewriteRule this would look like
RewriteRule ^folder/vanity1/?$ /differentfolder/ [R,L]
This pattern matches both with or without a trailing slash because of /?, which denotes an optional slash. The other rules would look similar.
When everything works as it should, you may replace R with R=301 (permanent redirect). Never test with R=301.
And with RedirectMatch, it would look almost the same
RedirectMatch ^/folder/vanity1/?$ /differentfolder/
Same disclaimer applies here, when it works as expected, you may set the status code to 301.

.htaccess folder as parameter to root index.php

What I need is any subfolder to be passed as a parameter to the root index.php
This is the code and it actually works.
RewriteEngine On
RewriteBase /
RewriteCond %{SCRIPT_FILENAME} !-f
RewriteCond %{SCRIPT_FILENAME} -d
RewriteRule ^(.+?)(/[^/]*|)$ index.php?dir=$1/ [L,QSA]
There is a problem:
When the url is like this (no end slash after 'projects'):
http://example.com/projects
the rewrite rule changes the link in the address bar and it looks like this:
http://example.com/projects/?dir=projects/
Is there a chance the url in the address bar always stays the same(no matter if there is an end slash or not) so the dir parameter is not visible to the user?
I tried with multiple rules - the first one to add an end slash, and then the second rule to pass the directory as parameter, but with no luck so far.
EDIT: so far thanks to w3d I managed to get it working. In the .htaccess just add:
DirectorySlash Off
tl;dr Make the trailing slash mandatory in the RewriteRule pattern (and remove the DirectorySlash Off directive, ie. keep it On).
RewriteRule ^(.+)/$ index.php?dir=$1/ [L,QSA]
As suggested in comments, this "strange" redirect is the result of mod_dir's DirectorySlash On directive, which is On by default. This can be quickly resolved by including DirectorySlash Off at the top of your .htaccess file (or making the trailing slash mandatory - see above and below).
The DirectorySlash On directive instructs Apache to automatically append a slash to URLs that end in a file system directory. In this sense it is "fixing" the URL. mod_dir achieves this with a 301 external redirect.
So, what is actually happening in the above, when DirectorySlash is enabled, is:
Initial request:
/projects (no trailing slash)
Internal rewrite in .htaccess:
/index.php?dir=projects/ (note that the request URL is still /projects)
mod_dir now kicks in and "fixes" the initial request (/projects --> /projects/) by appending a slash to the end of the URL-path. However, the query string from the rewritten URL (above) is passed through:
/projects/?dir=projects/ (this is a 301 external redirect, ie. a new request!)
Internal rewrite in .htaccess (again - new request):
/index.php?dir=projects/&dir=projects/ (note that the request is still /projects/?dir=projects/)
The doubling of the dir=projects/ query param is a result of the QSA flag on the RewriteRule (which I assume is required for other requests?). Your PHP script simply sees a single dir GET param (the later overwrites the former), unless you included dir[]=$1/ in your RewriteRule and you will end up with a 2-element array!
Your RewriteRule pattern also looks unnecessarily complex. You could simply make the trailing slash optional. ie:
RewriteRule ^(.+?)/?$ index.php?dir=$1/ [L,QSA]
Alternatively, having said all the above, you should probably leave DirectorySlash On (default) and simply make the trailing slash mandatory! For example:
RewriteRule ^(.+)/$ index.php?dir=$1/ [L,QSA]
mod_dir will now kick in before your internal rewrite (since it won't match without a trailing slash). This is also better for canonicalising your URLs and there are also potential security risks with turning it off.
Reference:
http://httpd.apache.org/docs/2.2/mod/mod_dir.html#directoryslash

.htaccess rewrite directories with index.html to subdirectory

At the site were some directories with html files like:
site.com/folderone/index.html
site.com/foldertwo/index.html
and it was available by links like:
site.com/folderone
site.com/foldertwo
now all these directories moved to /old-pages:
site.com/old-pages/folderone/index.html
site.com/old-pages/foldertwo/index.html
but old links should be available, so .htaccess file:
RewriteEngine on
RewriteRule ^folde(.*)$ /old-pages/folde$1 [L]
it's rewrite correct site.com/folderone/ and site.com/folderone/index.html
the problem is: for site.com/folderone it's not rewrite but redirect to site.com/old-pages/folderone/
The redirect is probably happening because of mod_dir and the DirectorySlash directive, which redirects requests for a directory that is missing the trailing slash so that the trailing slash is there. There's a good reason why that happens, as there's a info disclosure security concern without the trailing slash.
What you can do to avoid the redirect is either turn it off (not recommended):
DirectorySlash Off
or include the slash via mod_rewrite so that the two modules won't interfere with each other over the same request:
RewriteRule ^folder([^/]+)$ /folder$1/ [L,R=301]
You must add that before your other rewrite rules so that it gets applied first. Then your other rule should work.

Trying to stop urls such as mydomain.com/index.php/garbage-after-slash

I know very little about .htaccess files and mod-rewrite rules. Looking at my statcounter information today, I noticed that a visitor to my site entered a url as follows:
http://mywebsite.com/index.php/contact-us
Since there is no such folder or file on the website and no broken links on the site, I'm assuming this was a penetration attempt. What was displayed to the visitor was the output of the index.php file, but without benefit of the associated CSS layout.
I need to create a rewrite rule that will either remove the information after index.php (or any .php file), or perhaps more appropriately, insert a question mark (after the .php filename), so that any following garbage will be treated like a parameter (and will be gracefully ignored if no parameters are required).
Thank you for any assistance.
If you're only expecting real directories and real files that do exist, then you can add this to an .htaccess file. What it does is it takes a non-existent file or directory request and gives the user the index.php page with the original request as a query string. [QSA] appends any existing query string.
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule (.*) index.php?$1 [PT,QSA]
I found a solution, using information provided by AbsoluteZero as well as other threads that popped up on the right side of the screen as the solution came closer.
Here's the code that worked for me...
Options -Multiviews -Indexes +FollowSymLinks
RewriteEngine On
RewriteBase /
DirectorySlash Off
# remove trailing slash
RewriteRule ^(.*)\/(\?.*)?$ $1$2 [R=301,L]
# translate PATH_INFO information into a parameter
RewriteRule ^(.*)\.php(\/)(.*) $1.php?$3 [R=301,L]
# rewrite /dir/file?query to /dir/file.php?query
RewriteRule ^([\w\/-]+)(\?.*)?$ $1.php$2 [L,T=application/x-httpd-php]
I got the removal of trailing slash from another post on StackOverflow. However, even after removing the trailing slash, the rewrite rule did not account for someone appending what looks to be valid information after the .php file
(For example: mysite.com/index.php/somethingelse)
My goal was to either remove the "/somethingelse", or render it harmless. The PATH_INFO rule locates a "/" after the .php file, and turns everything else from that point forward into a query string (which will usually be ignored by the PHP file).

Resources