Castria
Castria

Reputation: 15

301 Redirect to HTTPS causing all non-homepage, non-HTTPS pages to redirect to homepage

I have a website that i just added an SSL certificate to. I added the following to .htaccess to redirect all non-HTTPS URLs to the HTTPS version:

RewriteEngine On
RewriteCond %{HTTPS} off
RewriteRule ^(.*)$ https://www.myurl.co.uk [R=301,L]

I also wanted all non-www version of the site to redirect to www version so i added the following:

RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} ^myurl.co.uk
RewriteRule ^(.*)$ https://www.myurl.co.uk/$1 [R=301,L]

Then, to help with SEO, i turned on URL rewriting (in Joomla, which I'm using) so now my URLs show as https://www.myurl.co.uk/about instead of https://www.myurl.co.uk/index.php/about.

I noticed however that when a link to my site is clicked (from Google for example) that still uses the old URL structure, it will always take the visitor to the homepage instead of the page they clicked on. So a visitor clicking on www.myurl.co.uk/index.php/about should be taken to https://www.myurl.co.uk/about but they are instead taken directly to https://www.myurl.co.uk (the homepage).

I tried adding the following for every page on my site:

Redirect 301 /index.php/about https://www.myurl.co.uk/about

...but this didn't fix the issue.

I'm very inexperienced with .htaccess. Where am I going wrong here?

Upvotes: 1

Views: 85

Answers (1)

anubhava
anubhava

Reputation: 785256

For meeting all 3 requirements i.e. (http -> https, non-www -> www, /index.php/ removal) you can use this single rule but make sure this rule is placed at top before all other rules:

RewriteEngine On
RewriteBase /

RewriteCond %{HTTPS} off [OR]
RewriteCond %{HTTP_HOST} !^www\. [NC,OR]
RewriteCond %{REQUEST_URI} ^/index\.php/. [NC]
RewriteRule ^(?:index\.php/)?(.*)$ https://www.myurl.co.uk/$1 [R=301,L,NE]
  • Avoiding multiple redirect rules will be good not just for SEO purpose but for client experience also.
  • Don't use Redirect or RedirectMatch rules while using mod_rewrite based rules.
  • Make sure you completely clear your browser cache while testing this change.

Upvotes: 1

Related Questions