Thomas Miller
Thomas Miller

Reputation: 115

How to redirect based on different conditions with htaccess?

I can't seem to figure out how to do my redirects correctly with my htaccess.

I need to redirect somes pages like this (xxx means any string):

  1. website.com/fr => OK
  2. website.com/en => website.com/en/page-1 [R 301]
  3. website.com/en/category-1/xxx => OK
  4. website.com/en/category-2/xxx => website.com/en/page-1 [R 301]
  5. website.com/en/category-3/xxx => website.com/en/page-1 [R 301] (I have multiple different categories with subpages that all need to be redirected).

I've tried to figure this out with Apache's module_rewrite by looking at other examples online and also the docs. I've tried something like this (but it didn't work):

RewriteCond %{REQUEST_URI} ^en/$ [OR]
RewriteCond %{REQUEST_URI} ^en/category-2$ [OR]
RewriteCond %{REQUEST_URI} ^en/category-3$ [OR]
RewriteRule (.*) https://website.com/en/page-1 [L,R=301]

Upvotes: 1

Views: 246

Answers (1)

RavinderSingh13
RavinderSingh13

Reputation: 133428

Could you please try following, written as per shown samples. Please clear your browser cache after placing these in your htaccess file.

RewriteEngine ON
RewriteCond %{REQUEST_URI} ^/(en)/?$ [NC]
RewriteRule ^(.*)$ /%1/page-1 [R=301,L]

RewriteCond %{REQUEST_URI} ^/en/(category-[23])/[\w-]+/?$ [NC]
RewriteRule ^(.*)$ /en/page-1 [R=301,L]

Upvotes: 2

Related Questions