1

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]
Amit Verma
  • 40,709
  • 21
  • 93
  • 115
Thomas Miller
  • 115
  • 2
  • 16

1 Answers1

2

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]
RavinderSingh13
  • 130,504
  • 14
  • 57
  • 93
  • Thanks! That seems like its working but there seems to be a problem: it it rewriting the urls with the root folders as well, like: mywebsite.com/data/webs/public_html/en/page-1 so it is adding "data/webs/public_html/" in the address bar, and that is causing problems. – Thomas Miller Dec 04 '20 at 14:22
  • @ThomasMiller, I am sorry didn't get it fully, in case you don't want to see urls(backend ones) in browser then remove `R=301` from above rules and clear browser cache and try again, if I get your question correctly. – RavinderSingh13 Dec 04 '20 at 14:24
  • And another question that I don't really understand: I know how to clear my own cache, but how long will this take for other users that regularly visit the website to see the correct redirects? Is there a way to break the cache so that every user gets the new htaccess? edit: I've seen that you cannot ever force browsers to clear their cache. With versioning (different css file names for example), that works. But how can I do this for the .htaccess file? – Thomas Miller Dec 04 '20 at 14:24
  • 2
    How long? https://stackoverflow.com/a/21396547/1427878 How to clear it (for other users)? Not at all, you simply _can’t_. – CBroe Dec 04 '20 at 14:41
  • Ok. Well actually I'm thinking that using the htaccess to do this is probably not the best solution for my problem. I will look for some other way to do this. Thanks! – Thomas Miller Dec 04 '20 at 14:58
  • @RavinderSingh13 You seem to have forgotten a leading slash in front of the rewritten URL – Thakkie Dec 04 '20 at 20:45
  • @Thakkie, its intentional, because `/` will consider that path is always in root, so its advised by experts not to place it always. – RavinderSingh13 Dec 04 '20 at 20:50
  • 1
    Yeah, but for an external redirect it gives the complete document root path, which can not be intentional because it does not work. That is why ThomasMiller is complaining about ```data/webs/public_html/``` in his path – Thakkie Dec 04 '20 at 21:04
  • @Thakkie, Thank you, somehow missed that comment(dealing with multiple answers), changed it now, thank you. – RavinderSingh13 Dec 04 '20 at 21:05
  • 2
    We are all here to help each other – Thakkie Dec 04 '20 at 21:15