0

I have a bunch of indexed pages that start with the same word and they are all 404 so I want to redirect them all to the homepage of the site. The server is Apache, so I would like to use the .htaccess file.

So I need to redirect pages (for example):

https://example.com/page-1.html

https://example.com/page-2.html

https://example.com/page-3.html

https://example.com/page-4.html

https://example.com/page-5.html

to: https://example.com/

  • If the pages don’t exist, why do you want to redirect them to the homepage? – MrWhite Feb 07 '23 at 13:41
  • Because they are indexed on Google, so I want to redirect them to the home page, so that they don't lead to a 404 page. Also, I need to know if is it possible to make that kind of redirection. – Petar Cvetic Feb 07 '23 at 13:59
  • But the pages don't exist, so they _should_ return a 404. Redirecting these URLs to the homepage is not helping Google (which will still see them as a soft-404) and it's confusing for users (as they are not seeing the content they are expecting and "bounce"). A custom 404 is generally preferable with a meaningful message for users, with useful links to other pages on your site. – MrWhite Feb 08 '23 at 00:30

1 Answers1

0

Something like:

RewriteEngine On
RewriteRule ^/page-.*  /  [R,L]

in your Apache config should do the trick. All pages starting with /page- will be redirected to the top page.

mod_rewrite module needs to be enabled in Apache configuration for this.

You may wish to use R=permanent instead of just R if you wish to signal to the clients (via response code 301) that the redirection is permanent and the pages are not supposed to return (this should probably make Google update its index, but I don't know for sure).

raj
  • 542
  • 2
  • 8
  • "in your Apache config" - Although the OP has stated they are using `.htaccess`. "this should probably make Google update its index, but I don't know for sure" - Well, that's the problem. Redirects to the homepage like this will likely be seen as soft-404's by Google and "eventually" drop the pages from the index. (But this also gives a bad experience for users.) – MrWhite Feb 08 '23 at 00:27
  • I tried your code but it doesn't work for me. – Petar Cvetic Feb 08 '23 at 10:45
  • @PetarCvetic If it doesn't work for you, then probably you have mod_rewrite not enabled in the Apache config. Something similar can be probably achieved using `Redirect` directives which are part of mod_alias that is almost always enabled, but since I stopped using them long ago and currently use only `RewriteRule`, I cannot provide the equivalent for you. – raj Feb 08 '23 at 20:37
  • I solved this redirection by the plugin All In One SEO / Redirects and regex code: ^/page-(.*) – Petar Cvetic Feb 10 '23 at 10:32
  • `RewriteRule ^/page-.*` - This rule is made for the main Apache config, not `.htaccess` (which I tried to drop a _hint_ in my first comment). This rule will never match in `.htaccess` due to the slash prefix on the URL-path. It would need to be simply `RewriteRule ^page- / [R,L]` (the trailing `.*` is not necessary and just makes the regex less efficient). You could not use `Redirect` since that only matches whole path-segments (you could use `RedirectMatch`), however, since this appears to be a WordPress site you should avoid mod_alias. @PetarCvetic – MrWhite Feb 10 '23 at 11:32
  • @PetarCvetic And if this is WordPress then the rule would need to go near the top of the `.htaccess` file, _before_ the `# BEGIN WordPress` comment marker (the order of the directives is important). – MrWhite Feb 10 '23 at 11:33