-1

I've just re-developed one of my websites. The structure has changed therefore I need to globally redirect the old non existent pages that are still currently ranked on Google (50 or so pages) to redirect to my new website's HOME PAGE.

The structure has basically changed from pages - example:

www.website.com/page1.php www.website.com/page2.htm www.website.com/page3.php ... and so forth

to

www.website.com/page1/ www.website.com/page2/ www.website.com/page3/ ... and so forth

After a bit of Googling, I came across this solution on SO

When I add:

RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule . / [L,R=301]

to my .htaccess file, the redirect (to the home page) for non-existing pages works - Great!

BUT now every link in my navigation / side panel on my new website (that exists) is also being redirected to the home page. So I cannot navigate through my website!

So for example when I click the about us link www.website.com/about-us/ or any other of my other 50 pages (regardless what they're named), the 301 redirect solution above keeps re-directing me to home page (as is if those pages didn't exist).

I want to globally redirect the old non existent pages that are still currently ranked on Google (50 or so pages) to redirect to my new website's HOME PAGE ... NOT my website's existing links.

Why is this? I thought the 404 redirect using 301 method would only target pages that DON'T exist.

Am I doing anything wrong?

Community
  • 1
  • 1
user3364730
  • 243
  • 1
  • 5
  • 16

2 Answers2

2

To redirect /page.php to /page , you can use the following :

RewriteEngine on
RewriteCond %{ENV_REDIRECT_STATUS} !200
RewriteRule ^(.+)\.php$ /$1 [L,R,NE]

You can change R to R=301 to make the redirect permanent.

Amit Verma
  • 40,709
  • 21
  • 93
  • 115
  • I am trying to find out why my NEW websites links in the navigation are resolving / redirecting to the HOME page with `RewriteEngine on RewriteCond %{REQUEST_FILENAME} !-f RewriteRule . / [L,R=301]` – user3364730 Mar 16 '17 at 09:44
  • That's because of your rule as it rewrites everything but existing files to / . – Amit Verma Mar 16 '17 at 09:49
  • I am also talking about globally redirecting *any* non existent page (that's still ranked on Google) to my home page ... not 1 specific page. My real problem is that my existing website links aren't working (they are being treated like they don't exist - please see my post) – user3364730 Mar 16 '17 at 09:49
  • "it rewrites everything but existing files to /" ... but my problem is that it is rewriting existing file too – user3364730 Mar 16 '17 at 09:50
0

The rule is applied as follows

  1. Check if about-us matches the pattern ., yes it does.
  2. Check if the conditions match, %{REQUEST_FILENAME} !-f. It is a directory, so yes it is not a file.
  3. Everything matches -> apply the rule.

If you don't want a directory to match, you may say so by

RewriteCond %{REQUEST_FILENAME} !-d

So the complete rewrite rule would be

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . / [L,R]

When everything works as it should, you may replace R with R=301 (permanent redirect). Never test with R=301.

Community
  • 1
  • 1
Olaf Dietsche
  • 72,253
  • 8
  • 102
  • 198