How to add Trailing Slash at the end of directory ONLY - trailing-slash

How to add Trailing Slash at the end of directory only, not file?
example: with Trailing Slash:
https://www.mywebsite.com/directory/
example: without Trailing Slash:
https://www.mywebsite.com/file.php

Related

.htaccess redirect for specific URL structures

I have the following URL:
https://www.site-a.xyz/tutorials/post-name/2
I need it to redirect to the following URL
https://www.site-b.xyz/post-name/2
Essentially If there is a trailing number element to the URL (in this case /2) I need the /tutorials/ part of the URL to be removed.
Note: ONLY if there is a trailing number
Try the following (using mod_rewrite) near the top of your .htaccess file at www.site-a.xyz:
RewriteEngine On
RewriteRule ^tutorials/([^/]+/\d+)$ https://www.site-b.xyz/$1 [R=302,L]
In this case, the trailing "number" can be 1 or more digits. If it is only a single digit (as in your example) then this should be simplified (change \d+ to \d). The $1 is a backreference to the captured group in the RewriteRule pattern.
Note that this is a 302 (temporary) redirect, if this is intended to be permanent then change to 301 when you are sure it's working OK. 301s are cached by the browser so can make testing problematic.
UPDATE: To allow for an optional trailing slash on the source URL then add /? near the end of the RewriteRule pattern, like so:
RewriteRule ^tutorials/([^/]+/\d+)/?$ https://www.site-b.xyz/$1 [R=302,L]
This notatably strips that optional trailing slash from the redirect target. (Thus avoiding any duplicate content issues.)

Redirect URLs with a trailing slash to URLs with no trailing slash via htaccess rule with [QSA,L]

Recently I broke ties with WordPress and migrated all of my site's content to my own custom-made CMS. All works great except for one thing. All previous links to my site's blog posts have a trailing slash. Since none of my current URLs have a trailing slash, the previous links no longer work and my SEO is nearly non-existent. I've been attempting to find an htaccess rule that will redirect all trailing slash URLs to URLs with no trailing slash, but as of now, nothing works.
Use this redirect rule as your very first rule to remove trailing slash:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.+)/$ /$1 [NE,R=301,L]
You don't want to remove it 100%for SEO in WordPress.#
But this will show you how to if you want that. WordPress "/" fixes. My WordPress Gist
Can convert so it's helpful
Config File: nginx.conf
...
location /mirror/foo/ {
...
rewrite ^(.*[^/])$ $1/ permanent;
...
}
...
Description WordPress "/" PHP fixes
Retrieve trailing slash string, if blog set for adding trailing slashes.
Conditionally adds a trailing slash if the permalink structure has a trailing slash, strips the trailing slash if not. The string is passed through the ‘user_trailingslashit’ filter. Will remove trailing slash from string, if blog is not set to have them.
Usage
<?php user_trailingslashit( $string, $type_of_string ); ?>
Parameters
$string
(string) (false) URL with or without a trailing slash.
Default: None
$type_of_url
(string) (false) The type of URL being considered (e.g. single, category, etc) for use in the filter.
Default: None
Return Value (string)
Adds/removes a trailing slash based on the permalink structure.
codex.wordpress. /Function_Reference/user_trailingslashit
2.
If you want to add "/"
<?php trailingslashit( $string ) ?>
Examples
<?php
$path = trailingslashit( '/home/julien/bin/dotfiles' );
?>
$path will now contain:
/home/julien/bin/dotfiles/
(Notice the trailing slash)
https://codex.wordpress.org/Function_Reference/trailingslashit
3.
This is new
The most important part of any test is the assertion. An assertion is a comparison between the value you expect to get from the system and the value that you actually get. The very simplest tests may consist of nothing but a single assertion. Example:
public function test_trailingslashit_should_add_slash_when_none_is_present() {
$this->assertSame( 'foo/', trailingslashit( 'foo' ) );
}
The assertSame() method accepts two parameters: the expected value (in this case, the hardcoded string 'foo/'), and the actual value (the value returned by trailingslashit()).
An annotated list of common assertions can be found below.
https://make.wordpress.org/core/handbook/testing/automated-testing/writing-phpunit-tests/#assertions
Try these rules: (in www.example.com's server block) rewrite ^/$ http://example.com permanent break; rewrite ^/main(.*)$ http://example.com$1 permanent break; rewrite ^(.*)$ http://blog.example.com$1 permanent; Make sure you reload nginx.
With this config:
http://www.example.com/ *redirects to
http://example.comt -
http://www.example.com/main/something redirects to
http://example.com/something - Everything else redirects to
http://blog.example.com/

How can I get rid of trailing slash for index.html

How do I get rid of the trailing slash on my site?
For example, I have an index.html at
http://example.com/examplepage/index.html
Instead of
http://example.com/examplepage/
I would like the URL to be
http://example.com/examplepage
Could I do this with .htaccess?
The trailing slash is really important for apache, without it, even if you have an index.html sitting in the examplepage folder, people will be able to see the contents of your folders. Apache deals with this by having a module loaded by default that redirects the browser to include the trailing slash everytime a directory/folder is accessed. You can turn that off but it's noted in the documentation that there's a major security concern when you do that; mainly, the contents of your folders can be viewed regardless of having an index file or not.
So you can turn this off, but you probably want to still have the trailing slash at least internally. You can do that with mod_rewrite:
# turn off the mechanism to always redirect to the trailing slash
DirectorySlash Off
# Internally add the trailing slash
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} -d
RewriteCond $1 .*[^/]$
RewriteRule ^(.*)$ /$1/ [L]
That should allow you to access http://example.com/examplepage without getting redirected to http://example.com/examplepage/.

Apache appends trailing slash to my rewrite rule

I have clean path with the same name as existing directory.
I use these .htaccess rules to support clean path for the path:
RewriteCond ${REQUEST_URI} ^/mydir
RewriteCond ${REQUEST_FILENAME} !-f
RewriteRule ^ index.php [L]
everything works correctly (I have a "mydir" clean path working and I can access existing files in the /mydir directory directly), but apache appends the trailing slash all the time to requests.
I request http://domain.com/mydir, and it redirects me 301 to http://domain.com/mydir/.
What is the reason?
Trailing slash after /mydir/ is added by an Apache module called mod_dir that adds a trailing slash after all the directories. This is due to this setting turned on by default:
DirectorySlash On
You can turn it off using:
DirectorySlash Off
However it might expose some directories by showing their listings.
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.
Apache's proper URL always ends in a slash /. Because it treats URL's as if they were a disk file path (which always ends in a slash). If it's not there, the server needs to take one additional step to internally add it. I say let it be.
Plus Google (supposedly) likes the trailing slashes.
I say keep it as is.
Please read more: http://cdivilly.wordpress.com/2014/03/11/why-trailing-slashes-on-uris-are-important/
and here: http://bit.ly/1uSvbfy :)

.HTACCESS Mod Rewrite with NO suffix?

I'm trying to get the following Mod Rewrite to work with no trailing slash as a suffix or file name.
RewriteEngine On
RewriteRule ^test/([^/]*)/([^/]*)/$ /example1/example2/index.php?brand=
$1&video=$2 [L]
The above works however if I remove the trailing slash from /$ it wont work.
So just to recap:
www.example.co.uk/example/example2/index.php?brand=x&video=y
rewrites to
www.example.co.uk/test/brand/video/
but I can't get it working for
www.example.co.uk/test/brand/video
Try changing the regular expression from ^test/([^/]*)/([^/]*)/$ to ^test/([^/]*)/([^/]*)/?$