39

I'm trying to make the following redirection (301) using .htaccess

*?page=1 redirects to *

(where * is a wildcard).

Basically, I just want to prevent anyone accessing a page with ?page=1 at the end of the URL, and instead direct them to the same url minus ?page=1.

Is there a quick way of doing this?

Philip Pittle
  • 11,821
  • 8
  • 59
  • 123
Freddy
  • 393
  • 1
  • 3
  • 4
  • See this "How to remove query strings from URLs" https://helponnet.com/2021/06/07/remove-specific-query-string-with-htaccess/ – Amit Verma Jan 18 '22 at 18:05

4 Answers4

50

This should do it:

RewriteEngine    On
RewriteCond      %{QUERY_STRING}    ^page=1$
RewriteRule      (.*)               $1?     [R=permanent]

Line by line:

  1. You turn on the rewriting functionality.
  2. You specify as a condition ("if statement") that the query string has to be exactly page=1 for the following rules to apply.
  3. Then you specify a rule that says substitute the entire path (.*) with itself ($1), but make the query string empty (?), and make the result a permanent redirect (301).

If you want the redirect to be temporary (302) then you can just remove the =permanent part. Moved Temporarily is the default for the R flag.

Fabian Fagerholm
  • 4,099
  • 1
  • 35
  • 45
  • Thanks for your reply! Can't seem to get that working however, it does not seem to have any effect when I try to access ../?page=1 any ideas? – Freddy Aug 11 '10 at 10:48
  • Check that your server has mod_rewrite loaded, and that your server settings allow you to specify those rewrite settings in a .htaccess file. – Fabian Fagerholm Aug 11 '10 at 11:00
  • @Fabian - The `RewriteRule` won't match because the input to the test pattern in a per-directory (`.htaccess`) context won't contain a leading slash. I suspect that's the issue here. – Tim Stone Aug 11 '10 at 12:12
  • What if there are other queries present and the only one you want to remove is `page=1`? For example: `www.domain.com?a=5&page=1&b=2` becomes `www.domain.com?a=5&b=2?` All queries are retained except that **page=1** gets stripped off. – enchance Nov 16 '11 at 05:05
  • 1
    @enchance That's a different question, you may want to open a new question for that. But here goes: `RewriteEngine On` `RewriteCond %{QUERY_STRING} ^(.*)page=1(.*)$` `RewriteRule (.*) $1?%1%2?` – didn't test it but something like that should work. – Fabian Fagerholm Nov 18 '11 at 06:06
  • Is it possible to remove ? from redirected url? – podarok Jun 29 '16 at 13:58
28

You can also use the QSD flag (Query String Discard) to redirect somewhere without passing the query string.

Didier Sampaolo
  • 2,566
  • 4
  • 24
  • 34
  • 10
    QSD is available in Apache version 2.4.0 and later: http://httpd.apache.org/docs/trunk/rewrite/flags.html#flag_qsd – waraker Apr 01 '14 at 20:44
12

If you are on Apache 2.4 You can simply use the QSD (Query String Discard flag) to discard the specific query strings from your destination url.

Here is an example for Apache 2.4 users :

Old url : - /foo/bar/?page=1

new url : - /foo/bar/

Htaccess code :

 RewriteEngine on

 RewriteCond %{THE_REQUEST} \?page=1\sHTTP [NC]
RewriteRule ^ %{REQUEST_URI} [L,R,QSD]

The Rule above will redirect any uri with ?page=1 to remove the query strings. This example will return 500 error on Apache versions bellow 2.4 as They don't support QSD.

On lower versions of Apache, you may use an empty question mark ? at the end of the destination url to remover query strings.

An example :

 RewriteEngine on

 RewriteCond %{THE_REQUEST} \?page=1\sHTTP [NC]
RewriteRule ^ %{REQUEST_URI}? [L,R]

The example above works almost on all versions of apache.

(Hope this helps!)

Amit Verma
  • 40,709
  • 21
  • 93
  • 115
5

For stripping entire query string this will be enough:

RewriteRule ^(.*) http://domain.com/$1? [R=301,L]
Krzysztof Przygoda
  • 1,217
  • 1
  • 17
  • 24