SSL Redirect in VirtualHost causing url to break

5,387

Solution 1

I managed to solve the problem by changing:

https://example.com/

to

https://example.com\/

This seems to have solved the problem, I assume that at some point Apache stripped off the slash.

Solution 2

For redirecting a TLS/SSL site to a non-TLS site, see the details below:

Adding a trailing slash, and escaping it with a backslash restores the missing slash.

I found that my particular site needed the redirect in both virtual hosts (*:80 and *:443) but the TLS one was not working properly. The following added to both virtual hosts fixed the issue.

Redirect permanent "/" http://example.com\/  

Note: The apache documentation shows adding quotes around everything. In my case the above worked just fine, allowing pages in the URL to be resolved as expected. Source: https://httpd.apache.org/docs/2.4/en/rewrite/avoid.html

Examples extrapolated from Source:

Redirect "/one/" "http://one.example.com/"
Redirect permanent "/" "http://example.com/"

Hope this helps someone.

Share:
5,387

Related videos on Youtube

stmfunk
Author by

stmfunk

I am an energetic engineering and computer science student from Ireland, studying in Edinburgh. I am incredibly passionate about learning, self improvement and everything to do with building stuff. I love all aspects of engineering, especially electronics and software. I tend to ask a million questions a minute but in return I try to regurgitate the answers when they might be useful so I guess I'm kind of an information recycling plant. I am a tireless and enthusiastic debater which is usually annoying, sometimes fun and often overwhelming. Some of my favourite things in the whole wide world are: big data (the cloud and all that malarky but I like the nitty gritty under the hood stuff), robots (I want them everywhere), space (nuff said), maths inc applied maths, physics etc (maths is like the most perfect thing in the world) and finally apple computers and unix operating systems (but really thats kind of a given for most people). I am still learning allot of stuff but I am doing so as fast as I can so I'll probably ask allot of questions and hopefully I'll be able to answer plenty soon.

Updated on September 18, 2022

Comments

  • stmfunk
    stmfunk over 1 year

    I recently configured by website to direct all traffic through https. I did this by modifying the vhost file to show this

    <VirtualHost *:80>
        DocumentRoot "/srv/http/example"
        ServerName example.com
        Redirect permanent / https://example.com/
    </VirtualHost> 
    

    This works fine when I go to the root of the site i.e. example.com, however when I try to go to a subdirectory I get a weird break in the url. For example if I type example.com/blog it tries to redirect to https://example.comblog. What is going on here?

    • masegaloeh
      masegaloeh about 10 years
      I can't reproduce this error if the Redirect line become Redirect permanent / https://example.com (e.g without trailing slash). With your configuration above, the redirection works properly.
    • stmfunk
      stmfunk about 10 years
      I tried removing the trailing / and using RedirectMatch permanent ^/(.*)$ https:/example.com/$1 and neither worked.