3

In apache 2.2 I am using mod_rewrite and I have a rewrite rule as -

RewriteCond %{REQUEST_URI} ^/?mysite/?
RewriteRule (.*)mysite/(.*) $1$2 [R=301,L]

This rewrites this url -

https://www.domain.com/mysite/about/

to

https://www.domain.com/about/

The rule seems to work. But when I see the network trace in chrome developer tools or httpwatch I can see redirects with http protocol with 302. The trace is below (I have removed the timings from trace and copied only the redirects)

GET 301 Redirect to http://www.domain.com/about     https://www.domain.com/mysite/about
GET 302 Redirect to https://www.domain.com/about    http://www.domain.com/about
GET 302 Redirect to http://www.domain.com/about/    https://www.domain.com/about
GET 302 Redirect to https://www.domain.com/about/   http://www.domain.com/about/
GET 200 html    https://www.domain.com/about/

I would like to avoid the 302 redirects to http://...... and just have a 301 redirect as per my rewrite rule. Is there a way to do this ?

Nikunj Sharma
  • 53
  • 1
  • 1
  • 6

1 Answers1

0

The first redirect (the one you talk about) works as expected and sends the user to the new page with a 301. However, it looks like the system you are using forces a '/' at the end of the URI and does another redirect because of that.

Maybe something like this would work better:

RewriteCond %{REQUEST_URI} ^/?mysite/?
RewriteRule (.*)mysite/(.*)/? https://www.example.com/$1$2/ [R=301,L]

Yet your trace shows 4 redirects... I'm not too sure I understand why there is an extra 2 redirects in there.

Alexis Wilke
  • 19,179
  • 10
  • 84
  • 156
  • Thanks, tweaking the rule and also combining few conditions I was able to resolve the additional redirects. But to solve the changing protocol problem I had to forcefully redirect to https. RewriteRule (.*)mysite/(.*)/? https://$1$2/ [R=301,L] – Nikunj Sharma Aug 17 '14 at 21:32
  • Ah! Yes! Sorry for the missing protocol. I'm glad you succeeded. – Alexis Wilke Aug 18 '14 at 00:42