1

I want apache to:

  1. redirect http users to https
  2. if they access http(s)://example.com/1234 redirect to /view/1234

I have tried different flavours but cannot get it work. When a user access the website, they get redirected to https://, however I cannot get the /{numeric} to redirect to add /view/. The numeric rule was working okay until I put the https redirect on.

I have tried it with RewriteCond %{HTTPS} = on, but this causes 500 errors.

RewriteEngine On
RewriteCond %{HTTPS} !=on
RewriteRule ^/?(.*) https://domain.example.org/$1 [R,L]
RewriteRule ^(\d+)$ https://domain.example.org/view/$1 [R=301,L]
0

4 Answers 4

1

Replace first RewriteCond with:

RewriteCond %{SERVER_PORT} !^443$
1
  • Thanks. When I go the http(s)://domain.example.org/123 it still does not redirect me to http(s)://domain.example.org/123. I just got a "404" page.
    – Sc0rian
    Apr 11, 2013 at 17:22
1

Will this not work for you?

RewriteEngine On
RewriteCond %{HTTPS} off
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI}

Try this to test:

http://htaccess.madewithlove.be/

Ok it sounds like you actually want something like this:

RewriteEngine On
RewriteCond %{HTTPS} off
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI}
RewriteCond %{REQUEST_URI} /1234
RewriteRule (.*) https://%{HTTP_HOST}/view%{REQUEST_URI} [L]
1
  • thank you for that url. http(s):/domain.example.org/123 it still does not redirect me to http(s)://domain.example.org/123
    – Sc0rian
    Apr 11, 2013 at 17:26
0

This is going to be an off the cuff answer, but, I believe the rewrite rules match in a first come/first serve basis. Since the path structure changes, I'll bet your http://domain.example.org/1234 users are getting redirected to the https equivalent but without the path change. Try switching the order of the rules and you'll get closer to a full solution.

If that doesn't work, let me know and I'll fire up an Apache instance and we'll see what we can get working in tandem.

1
  • Thanks, swapping order makes no difference, I've tried that.
    – Sc0rian
    Apr 11, 2013 at 17:27
0

Thanks for your help.

I finally have it working. I used the following:

RewriteCond %{HTTPS} off
RewriteRule ^/?(.*) https://domain.example.org/$1 [R,L]
RewriteRule ^([0-9]+)/?$    https://%{HTTP_HOST}/view/$1    [NC,L] 

So any numeric requests have /view added on, any attempts to access the site with http:// gets forwarded to https://. Seems to work well.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .