How to redirect page from http to htpps with url name change

Viewed 85

I have the following problem

I need two things:

  1. Redirect from unsafe HTTP to safe HTTPS and then
  2. Redirect from https://example.com/index.html to https://example.com/work-for-us

I know the first one I can obtain using:

RewriteEngine on
RewriteCond %{HTTPS} !=on
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]

and the second one using

RewriteEngine on
RewriteRule work-for-us index.html

But is it possible to connect those two together?

So I have both HTTP -> HTTPS and /index.html -> /work-for-us

I don't have too much knowledge about .htaccess file, I read something on the internet but didn't find anything that answers my question.

So putting everything together, I want to change the URL from this:

http://example.com/index.html to this https://example.com/work-for-us

Thank you in advance.

1 Answers

Based on your shown samples, could you please try following. Please make sure to clear your browser cache before testing your URLs.

RewriteEngine on
##First rule for applying https to URLs.
RewriteCond %{HTTPS} !=on
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301,NE]

##Second rule for serving homepage(only dns link) with index.html file.
RewriteRule ^/?$ work-for-us [R=301]
RewriteRule ^/?$ index.html [L]

##Third rule for serving non-existing directories/files with index.html
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^ index.html [L]
Related