17

Whether I'm using the Reroute plugin or plugging it in .htaccess, changing the path elsewhere appends the query in the end. How can I remove?

Redirect 301 /blog/category /blog/archive

Is the line I have in my .htaccess

So if I go to:

http://domain.com/blog/category/category-name

it takes me to:

http://domain.com/blog/archive/category-name?p=blog/category/category-name

What's going on here?

0

4 Answers 4

21

The issue here is that your Redirect directive is conflicting with Craft’s index.php-hiding RewriteRule directive. (Redirect is a mod_alias directive and RewriteRule is a mod_rewrite directive. They’re two separate modules that can be used for the same thing, but lead to unexpected consequences when used together.)

To fix this, use a RewriteRule directive for your own custom redirects as well, rather than Redirect.

RewriteRule ^blog/category(/.*)?$ /blog/archive$1 [R=301,L]

Make sure that you place that before Craft’s redirect.

7
  • That gives me a 404 :(
    – lealea
    Commented Jul 11, 2014 at 16:15
  • Is this meant to be in ADDITION instead of replacement? If it's in addition, I added the original Redirect above and then the one you have up there with the rewrite it still appends the URL
    – lealea
    Commented Jul 11, 2014 at 16:17
  • Should be a replacement. Old code out. This is what I used on an EE project where I was having this problem. Commented Jul 11, 2014 at 16:41
  • 3
    Make sure you place that before Craft’s redirect. Commented Jul 11, 2014 at 16:46
  • Yes, mine is right after "RewriteEngine on". Adding that to my answer. Commented Jul 11, 2014 at 17:16
6

One way you can avoid this is by making a small change to the htaccess rules for removing index.php from Craft URLs. You can remove the query string from RewriteRule (.+) index.php?p=$1 [QSA,L] and replace it with /$1

So the full set of rules to remove index.php would be

# Send would-be 404 requests to Craft
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !^/(favicon\.ico|apple-touch-icon.*\.png)$ [NC]
RewriteRule (.+) index.php/$1 [QSA,L]

This seems to work fine in my test and prevents the query string from being appended to the URL when using Redirect 301 in htaccess.

2
  • 1
    In my tests, this seems to work! However, what are the possible side-effects of removing the ?p=
    – lealea
    Commented Jul 11, 2014 at 16:34
  • 1
    The only side effect of that change is it won’t work on servers that don’t support PATH_INFO. If it looks like it’s working for you, then great! Commented Jul 11, 2014 at 16:40
2

What happens if you add a ? to the rule:

RewriteRule ^blog/category?$ /blog/archive [R=301,L,NE]
3
  • Still gave me a 404
    – lealea
    Commented Jul 11, 2014 at 16:44
  • When you put a ? right after another character in a regular expression, you’re just saying that whatever came before it is optional. So that pattern would match both “blog/category” and “blog/categor”. (Usually it’s good idea to put a ? right before a trailing slash, so you’re matching both “blog/category/” and “blog/category”.) Commented Jul 11, 2014 at 17:40
  • s/right before a trailing slash/right after a trailing slash/ Commented May 25, 2016 at 9:11
0

If you need to remove these from search results, you may find this helpful: Remove Query Strings from URLs

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.