SLV
SLV

Reputation: 21

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.

Upvotes: 0

Views: 8390

Answers (3)

Deepak Bharadwaj
Deepak Bharadwaj

Reputation: 1

RewriteEngine On

RewriteCond %{REQUEST_URI} /$ RewriteRule ^(.*)/$ /$1 [R=301,L]

Upvotes: 0

Thomas Zickell
Thomas Zickell

Reputation: 1

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:

  1. http://www.example.com/ *redirects to
  2. http://example.comt -
  3. http://example.com/something - Everything else redirects to

Upvotes: 0

anubhava
anubhava

Reputation: 784868

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]

Upvotes: 4

Related Questions