1

I have a number of links with similar structure:

example.com/index.php/path/some-path/some-subpath?parameter=1

Which I want to redirect just to example.com/category/

But I want to match only the URL-path, ie. /index.php/path/some-path/some-subpath, because the query parameter is irrelevant.

I have tried the following rule:

RewriteRule ^/index.php/path/some-path/some-subpath?parameter=\dd\? /category/ [L,R=301]
  • 1
    You state that the "query parameter is irrelevant", yet you are trying to check for it in your code example - why? Must the parameter= part be present, but the value does not matter? Or can the query string be ignored altogether? – MrWhite Jan 26 '16 at 22:50
  • @w3dk complete part '?parameter=value' can be ignoner, but URL should be rewritten. If I use redirect 301 only, I don't get URL rewritten to domain.com/category – Alan Kis Jan 26 '16 at 22:53
2

If the query string is irrelevant then it can simply be ignored. (You can't match the query string anyway with the RewriteRule directive.)

Try the following in your root .htaccess file to redirect the request:

RewriteEngine On
RewriteRule ^index.php/path/some-path/some-subpath$ /category/? [L,R=301]

In per-directory .htaccess files, the URL-path matched by the RewriteRule pattern has the directory-prefix removed. In other words, it does not start with a slash, ^index.php.... instead of ^/index.php.....

The ? on the end of the RewriteRule substitution is required in order to remove the query string (if any) from the request. Otherwise parameter=1 will be passed through and appended to the target URL.

This will match any query string, including an empty one.

  • @wkd, what if I want to rewrite this URL to root, should I use only '/'? – Alan Kis Jan 26 '16 at 23:57
  • 1
    Yes, just use /?. You'll need to clear your browser cache if you have previously tested the rule above. 301 redirects are cached by the browser. For this reason it is often a good idea to test with 302 (temporary) redirects. – MrWhite Jan 26 '16 at 23:59
0

Answer by @MrWhite did not worked for me. Here is what is working with my Wordpress site.

Example:

OLD URL: http://website.com/xxx/yyy/picture.png
NEW URL: http://website.com/wp-content/picture.png

So, I want to replace /xxx/yyy/ with the normal /wp-content/ path of WordPress.

Put this in your .htaccess file.

RewriteEngine on
RedirectMatch 301 /xxx/yyy/(.*) http://website.com/wp-content/$1

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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