Reputation: 15
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
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]
Redirect
or RedirectMatch
rules while using mod_rewrite
based rules.Upvotes: 1