Apache2 Redirect Trailing Slash Not Working

apache-2.2redirect

I have attempted to read various documentation, as well as other answers online, and I cannot find a solution to my problem.

Here is my issue:

I have a permanent www to non-www redirect setup to point traffic from www.example.com to example.com

This works, but I am having issues when trying to visit specific urls. For example:

We have three PDFS that we would like people to be able to download via:

example.com/paper/somepdfname.pdf

When i visit the url like this it works, but if I try to visit something like this:

www.example.com/paper/somepdfname.pdf

The browser gets redirected, and strips one of the slashes out, resulting in "the site cannot be reached error". This is the url I end up with in the browser:

example.compaper/somepdfname.pdf

I think its pretty clear that I need to somehow make sure there is a forward slash put before the paper, but I do not know how to do this. Below you will find my .htaccess directive for handling the redirect:

RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC]
RewriteRule ^(.*)$ http://%1/$1/ [R=301,L]

Best Answer

Using mod_rewrite seems like an overkill for you. I recommend using mod_alias and its Redirect directive.

NameVirtualHost 111.22.33.44:80
<VirtualHost 111.22.33.44:80>
    ServerName www.example.com
    Redirect permanent / http://example.com
</VirtualHost>
Related Topic