user2961763
user2961763

Reputation: 63

HTTPS to HTTP redirect doesn't keep custom URL

Let's say I have a domain name called: www.example.com/today.php?id=123. I got it to redirect to the url www.example.com/yes/123/some-name with the following code:

Rewrite Rule ^yes/123/some-name$ today.php?id=123

But now I wanted to make that link always redirect to http when called from https. So, I tried the following code by adding:

RewriteCond %{HTTPS} on
Rewrite Rule ^today\.php(.*)$ http://%{HTTP_HOST}%{REQUEST_URI} [L,NC,R]

What happens is that it get redirected to http but with the following url: www.example.com/today.php?id=123 instead of keeping the original www.example.com/yes/123/some-name. What am I doing wrong?

Thank you!

Upvotes: 1

Views: 128

Answers (2)

Jon Lin
Jon Lin

Reputation: 143906

You need to make sure your redirect is before the rule that internally routes. And you need to match against the cleaned up URL instead of the internally mapped one. So something like:

RewriteCond %{HTTPS} on
RewriteRule ^yes/[0-9]+/ http://%{HTTP_HOST}%{REQUEST_URI} [L,NC,R]

RewriteRule ^yes/123/some-name$ today.php?id=123 [L]

Upvotes: 0

anubhava
anubhava

Reputation: 785561

Change order of rules:

RewriteEngine On

RewriteCond %{HTTPS} on
RewriteRule ^yes/[^/]+/.*$ http://%{HTTP_HOST}%{REQUEST_URI} [L,NC,R=301]

RewriteRule ^yes/[^/]+/.*$ today.php?id=123 [L,QSA,NC]

In general keep your 301 rule before pretty rewrite rules.

Upvotes: 4

Related Questions