views:

37

answers:

1

Hi There,

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?

Many thanks!

A: 

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
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
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
@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
@Tim - You're right, I'll edit the answer to reflect this.
Fabian Fagerholm
Thanks Guys, that's working perfectly!
Freddy