Search code examples
.htaccesshttp-redirectmod-alias

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:

  1. 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.

  1. 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?


Solution

  • 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.