1

I want to redirect all subpages to main page. I tried with code:

RewriteEngine On
RewriteRule .+ http://www.example.com [r=301,nc,l]

All subpages are redirected except these that include question mark, for example, http://www.example.com/?123 is not redirected. How to modify my code to redirect also those URLs?

MrWhite
  • 43,179
  • 8
  • 60
  • 84
Marian
  • 27
  • 6

2 Answers2

0

You need to redirect any non-empty URL-path OR the homepage (empty URL-path) with a query string. You should also remove the query string as part of the redirect (your rule currently preserves the query string from the initial request).

For example, try the following instead:

# Redirect everything to the homepage (same domain)
RewriteCond %{REQUEST_URI} ^/. [OR]
RewriteCond %{QUERY_STRING} .
RewriteRule ^ http://www.example.com/ [QSD,R=301,L]

The above states... for all URL-paths where the URL-path consists of at least one character (after the initial slash) OR contains a query string then redirect to the root.

The QSD flag discards the original query string from the request.

The NC flag on the rule is superfluous, since you aren't matching specific letters anyway.

Aside: However, I would question the motives for doing something like this. Search engines (Google) will see mass redirects to the homepage as soft-404s, so there is no SEO benefit in doing this and it can often be confusing for users if they are following a link that previously existed. A meaningful 404 response is usually the preferred option in this scenario.


UPDATE:

If i would like to use this code also to redirect to other domain what should i change or add to redirect also main page?

Assuming the other domain also points to a different server then you just need to remove the two conditions on the above rule to redirect everything and remove the query string.

For example:

# Redirect everything to the homepage on an external domain
RewriteRule ^ http://www.example.com/ [QSD,R=301,L]
MrWhite
  • 43,179
  • 8
  • 60
  • 84
  • your code works fine. If i would like to use this code also to redirect to _other_ domain what should i change or add to redirect also main page? – Marian May 22 '22 at 09:42
  • @Marian You basically just need to remove the two _conditions_. I've updated my answer. – MrWhite May 22 '22 at 10:04
  • update works fine, i tried myself to modify first line deleting /. it also works but removing 2 first lines is easier. I have always used before the rules RewriteEngine On and RewriteBase / lines. I put your code and forgot to add those 2 lines but rewrites work fine. Is it not necessary to use them? – Marian May 22 '22 at 10:34
  • @Marian "modify first line deleting /. it also works" - If you only removed the first _condition_ then it would only redirect URLs with a query string (ie. with a question mark followed by something). It wouldn't redirect `http://www.example.com/foo` for instance. The `RewriteBase` directive is certainly not required here (there are no relative substitutions). However, the `RewriteEngine On` directive is generally required, unless it has already been enabled in a parent config (the default is `Off`). – MrWhite May 22 '22 at 16:53
  • I did not remove the first condition, only /. from first condition so left RewriteCond %{REQUEST_URI} ^ [OR]. Without RewriteEngine On works for me, so i guess it is enabled, but maybe it is better to add this directive. – Marian May 22 '22 at 18:51
  • @Marian Ah I see, yes that would have worked, but it just makes the 2nd condition redundant, since it would always be successful. Yes, you should always have an instance of `RewriteEngine On` in the `.htaccess` file (preferably at the top, for readability). – MrWhite May 22 '22 at 22:26
  • Just one more question, when rule RewriteBase / should be used? – Marian May 23 '22 at 09:05
  • @Marian The `RewriteBase` directive sets (overrides) the URL-path that is prefixed to **relative** _substitution_ strings. (The _substitution_ string is the 2nd arg to the `RewriteRule` directive.) That is all it does. It is often not required (despite what the [Apache docs](https://httpd.apache.org/docs/current/mod/mod_rewrite.html#rewritebase) state). In fact, `RewriteBase /` is rarely required. `RewriteBase` seems to cause a lot of confusion, but it's actually quite simple. See the following question: https://stackoverflow.com/questions/704102/how-does-rewritebase-work-in-htaccess – MrWhite May 23 '22 at 14:55
0

Try this, it works in my system.

RewriteRule ^(.*)$ http://www.example.com/ [L,R=301]
Damiano93
  • 19
  • 6
  • (Why would you need to do something like this in your "ERP system"?) However, the directive you've posted cannot possibly "work" as written - it would create an endless redirect loop and it would fail to remove the query string as stated in the question. – MrWhite May 21 '22 at 18:21
  • It redirects users from another, not working yet server. Maybe You may be right but it works great. – Damiano93 May 21 '22 at 18:31
  • Ok, but the question would seem to be about redirecting to the _same_ domain. – MrWhite May 21 '22 at 20:20
  • @DamianChlebica your code doesn't work properly. If i use it to _same_ domain it doesn't work at all: www.example.com/?1 is still www.example.com/?1 , if you use it to _other_ domain www.example1.com/?1 redirects to www.example2.com/?1 – Marian May 22 '22 at 09:35
  • so, how to fix it? RewriteCond %{HTTP_HOST} ^prod\.ab-cb\.name\.pl$ [NC] RewriteRule ^(.*)$ http://ab-cb.net/ [L,R=301] – Damiano93 Jun 30 '22 at 19:13