Score:0

How to redirect all pages that contain a specific word using the .htaccess file

tv flag

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/

kz flag
If the pages don’t exist, why do you want to redirect them to the homepage?
Petar Cvetic avatar
tv flag
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.
kz flag
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.
Score:0
ye flag
raj

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).

kz flag
"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.)
Petar Cvetic avatar
tv flag
I tried your code but it doesn't work for me.
raj avatar
ye flag
raj
@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.
Petar Cvetic avatar
tv flag
I solved this redirection by the plugin All In One SEO / Redirects and regex code: ^/page-(.*)
kz flag
`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
kz flag
@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).
I sit in a Tesla and translated this thread with Ai:

mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.