Reputation: 423
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?
Upvotes: 42
Views: 74842
Reputation: 1315
For stripping entire query string this will be enough:
RewriteRule ^(.*) http://example.com/$1? [R=301,L]
Upvotes: 6
Reputation: 41249
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 remove 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.
Upvotes: 14
Reputation: 2823
You can also use the QSD
flag (Query String Discard) to redirect somewhere without passing the query string.
Upvotes: 31
Reputation: 4139
This should do it:
RewriteEngine On
RewriteCond %{QUERY_STRING} ^page=1$
RewriteRule (.*) $1? [R=permanent]
Line by line:
page=1
for the following rules to apply.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.
Upvotes: 51