How can I redirect the page after a rewrite rule .htaccess

Hi all

I’m in the process of updating some pages from http to https. At the moment I’m just updating a single page at a time until all links and data is updated.

Which brings me to one snippet/issue I’m having right now.

RewriteRule ^/?event/([0-9]+)$ event.php?ID=$1 [L]

The rule above changes
http://www.example.com/event?id=1234
to
http://www.example.com/event/1234
works good.

What I need now, is to redirect any event to the below with the https

https://www.example.com/event/1234

How would I do this?
Can I add the redirect within the snippet above, or do I need as separate line?

I’ve tried a few combinations already though keeps causing issues.

Any help thanks.
Barry

You need a separate line before the line you already have:

RewriteCond %{SERVER_PORT} 80
RewriteRule ^/?event/([0-9]+)$ https://www.yourdomain.com%{REQUEST_URI} [L,R=301]
1 Like

I was so close :smile:
Working great now thanks.

And would I be correct saying:

80 is used for HTTPS

%{SERVER_PORT} 80

443 is used for HTTP

%{SERVER_PORT} 443

Is that correct?
And what if we don’t add the PORT number, does it default to something?

I’ve also noticed since my host installed the SSL certificate my .htaccess now has other snippets as shown below:

RewriteCond %{REQUEST_URI} !^/[0-9]+\..+\.cpaneldcv$
RewriteCond %{REQUEST_URI} !^/\.well-known/acme-challenge/[0-9a-zA-Z_-]+$
RewriteCond %{REQUEST_URI} !^/\.well-known/pki-validation/[A-F0-9]{32}\.txt(?:\ Comodo\ DCV)?$
RewriteRule ^/?event/([0-9]+)$ event.php?ID=$1 [L]

Are these needed and if so, do I need to re-add them to any new rules I apply?

Another small issue I have
redirect 301 /listings.php https://www.example.com/whats-on

This redirects http://www.example.com/listings.php to https://www.example.com/whats-on
Works good, but if I land on http://www.example.com/whats-on it dosen’t redirect to https://www.example.com/whats-on - what rule how would I redirect the http to https in this instance?

Thanks,
Barry

No it’s actually the other way around: port 80 is HTTP and port 443 is HTTPS.

The additional RewriteCond that were added for SSL can be removed. They don’t apply.

As for the redirect that would be the same as what I posted earlier but with a different URL. Given all the information so far you should be able to figure that one out. Just shout if you need help.

Cool! Will give this a shot see if I can get things working :sunglasses:

Barry

Thanks for clarifying.

Ok, I have now added:

RewriteCond %{SERVER_PORT} 80
RewriteRule /whats-on https://www.example.com%{REQUEST_URI} [L,R=301]
redirect 301 /listings.php https://www.example.com/whats-on

The last redirect 301 works ok. But the first RewriteRule is not working. No errors though it doesn’t redirect to the https.

I see you used
^/?event/([0-9]+)$

I have only used
/whats-on

Do I need to re-add the brackets or one of the other characters ^/?()$ ?

Thanks,
Barry

Does it work if you change it to ^/?whats-on

?

No :thinking:

RewriteCond %{SERVER_PORT} 80
RewriteRule ^/?whats-on https://www.example.com%{REQUEST_URI} [L,R=301]

It will be something like this I’m sure.

Thanks,
Barry

Could you post the full .htaccess please?

Thanks @rpkamp

I’ve minimised for viewing, some snippets are the same but different pages that don’t apply.

Options All -Indexes

RewriteEngine On
RewriteCond %{REQUEST_URI} !^/[0-9]+\..+\.cpaneldcv$
RewriteCond %{REQUEST_URI} !^/\.well-known/acme-challenge/[0-9a-zA-Z_-]+$
RewriteCond %{REQUEST_URI} !^/\.well-known/pki-validation/[A-F0-9]{32}\.txt(?:\ Comodo\ DCV)?$
RewriteRule (listing|whats-on|event)/?$ $1.php [L]

redirect 301 /listing.php https://www.example.com/whats-on

RewriteCond %{SERVER_PORT} 80
RewriteRule ^/?whats-on https://www.example.com%{REQUEST_URI} [L,R=301]

RewriteCond %{SERVER_PORT} 80
RewriteRule ^/?event/([0-9]+)$ https://www.example.com%{REQUEST_URI} [L,R=301]

RewriteCond %{REQUEST_URI} !^/[0-9]+\..+\.cpaneldcv$
RewriteCond %{REQUEST_URI} !^/\.well-known/acme-challenge/[0-9a-zA-Z_-]+$
RewriteCond %{REQUEST_URI} !^/\.well-known/pki-validation/[A-F0-9]{32}\.txt(?:\ Comodo\ DCV)?$
RewriteRule ^/?event/([0-9]+)$ event.php?ID=$1 [L]

rewritecond %{http_host} ^example.com [nc]
RewriteCond %{REQUEST_URI} !^/[0-9]+\..+\.cpaneldcv$
RewriteCond %{REQUEST_URI} !^/\.well-known/acme-challenge/[0-9a-zA-Z_-]+$
RewriteCond %{REQUEST_URI} !^/\.well-known/pki-validation/[A-F0-9]{32}\.txt(?:\ Comodo\ DCV)?$
rewriterule ^(.*)$ http://www.example.com/$1 [r=301,nc]

Also got me thinking.

Moving forward I’ll be adding whats-on/nye etc. though not an issue right now, just keep this in mind it will end up working like the event snippet.

Also wondering

Couldn’t we have for example just one single RewriteCond, with multiple RewriteRule below:

RewriteCond %{SERVER_PORT} 80
RewriteRule ^/?whats-on https://www.example.com%{REQUEST_URI} [L,R=301]
RewriteRule ^/?event/([0-9]+)$ event.php?ID=$1 [L]

Or do we need RewriteCond for every RewriteRule?

Cheers,
Barry

You need to put the http to https redirects before any other rules. They are already matched now before the rules have a chance to fire.

Regarding combining them, you can’t. You need to repeat the RewriteCond with every RewriteRule.

1 Like

It’s working :grinning:

Good catch, I see what you mean now.
It reminds me of when I was trying to run some javascript functions before the framework had loaded, similar process.

In conclusion — Always put the redirects before the other rules :sunglasses:

You need to repeat the RewriteCond with every RewriteRule.

Shame. Though not big issue.

Cheers,
Barry

One final question @rpkamp

Once I’ve moved all http pages to https, could I remove the individual http to https redirects and simply add something like:

RewriteCond %{HTTPS} off
RewriteRule ^(.*)$ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]

Or would I still need the separate rules?
Or use both just incase?

Barry

Once everything is over just the one rule is fine :+1:

1 Like

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.