1

My SEO team has reported duplicate URLs. I have turned on SEF URLs and I have renamed htaccess.txt to .htaccess.

This does not show the index.php when I click on the link, but if I manually add it back in then I can still see the page. e.g. both of these URLs continue to work.

example.com/foldername/

example.com/index.php/foldername/

How could I make a redirect which would remove index.php from any combination of folder and redirect it?

2 Answers 2

1

You can add something like the following to the top of the root .htaccess file to 301 redirect requests for /index.php/<anything> back to /<anything> (the canonical URL) - to make your SEO team happy:

# Remove "index.php" from the start of the URL-path
RewriteRule ^index\.php(?:/(.*))?$ https://example.com/$1 [R=301,L]

Where example.com is your canonical hostname (www vs non-www etc.). By including an absolute/canonical URL here, it potentially avoids multiple redirects later.

The $1 backreference contains the URL-path following index.php (if any), less the slash prefix - as captured by the RewriteRule pattern. So, this will also match both /index.php and /index.php/ and redirect to https://example.com/ (the homepage) in both cases.

This redirect is essential for SEO if you have only recently enabled SEF URLs and the old URLs containing index.php have already been indexed by search engines, or linked to by external third parties. However, if you implement SEF URLs from the very beginning then it is unlikely that search engines / third parties will find the non-canonical URLs so is unlikely to cause issues (but not impossible).

You don't need to include an additional RewriteEngine On directive, since this already occurs later in the file. Or, include the above rule immediately after the RewriteEngine directive - to make it more "readable".

Note that you should first test with a 302 (temporary) redirect to avoid potential caching issues. 301 (permanent) redirects are cached persistently by the browser so can make testing problematic.

4
  • I always wonder why you remove index.php. Maybe remove whole javascript, because google needs browser-client to analyze the content ?:)
    – Elboyler
    Commented Nov 28, 2022 at 20:29
  • In part the problem is the site has had multiple iterations with multiple platforms, so there are indexed URLs. Although I had it turned on from day 1 of Joomla, that isn't necessarily the case for older URLs which were e-commerce (not sure which) and WordPress
    – Eoin
    Commented Nov 29, 2022 at 14:52
  • I used this rule but the index.php URL seems to still work fine without any redirect. You can see an example here: aromabathjpd.com/index.php/our-top-brands/discover-vola and my rule is RewriteRule ^index\.php(?:/(.*))?$ https://www.aromabathjpd.com/$1 [R=301,L]
    – Eoin
    Commented Nov 29, 2022 at 14:56
  • 1
    @Eoin Where exactly have you added that rule? It needs to be before all other rewrites. Please update your question with your complete .htaccess file with this rule in-place - maybe there is a conflict. I notice that requests for /index.php only are 302 redirected to /, but /index.php/anything is not. (Note that your 404 page links back to /index.php, not / as it should do.)
    – MrWhite
    Commented Nov 30, 2022 at 0:27
0

Google is not stupid. In my opinion this is pointless and I won't paste the code that makes that redirect. SEO fools will moan, but google bots won't.

Duplicate entries are visible when you type site:mysite.xxx into google. I hardly believe they see any duplicates, canonicals, etc.

People who claim seo might require money. Check "site" tag at google first and push your site into google services to get errors etc before you start to listen to anyone.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.