1

It seems to be easy, but I can't figure out how to solve it, nothing worked for me. I have a dynamic URL like: http://www.example.com/user/46/track and I want to redirect from that URL to http://www.example.com/user/46/. The number 46 is a user ID so it's dynamic.

I have already tried:

RewriteRule ^/track(.*)$ $1
RewriteRule ^/user/*(.*)/(\?.*)?$ /user/*$1$2 [R=301,L]

Can somebody help me with this solution? What did I do wrong?

1
  • 1
    Is "track" fixed? Or is that dynamic also?
    – MrWhite
    Commented Aug 14, 2018 at 11:12

1 Answer 1

1
RewriteRule ^/track(.*)$ $1

In .htaccess, the URL-path that the RewriteRule pattern matches against never starts with a slash, since the directory-prefix - that notably ends with a slash - is first removed. However, unless this directive was physically located in a .htaccess file at /user/46/.htaccess then this would never match anyway.

RewriteRule ^/user/*(.*)/(\?.*)?$ /user/*$1$2 [R=301,L]

This is closer, but again, you have the slash-prefix problem as mentioned above. You also seem to be trying to match the query string and passing this through to the target URL? The RewriteRule pattern matches against the URL-path only, which notably excludes the query string - so this will never capture anything. Besides, you don't actually need to do anything with the query string if you want this passed through to the target URL - since this is the default behaviour. You also appear to have a stray * in both the pattern and substitution arguments? This will likely break the target URL.


Try something like the following instead. This will need to go near the top of your .htaccess file, before any other rewrites:

RewriteEngine On

RewriteRule ^user/(\d+)/track$ /user/$1/ [R=302,L]

The above assumes that "track" is fixed, so specifically matches a URL of the form /user/46/track, where 46 can be any number of digits. With regex it is better to be as specific as possible, so if the ID can only consist of digits then only match digits etc.

If you simply wanted to remove anything after /user/46/ then you could change the above directive to read:

RewriteRule ^user/(\d+)/. /user/$1/ [R=302,L]

The trailing dot on the RewriteRule pattern matches any character, so ensures that there is something that follows /user/46/ (for example). It is this "something" that is being removed.

Any query string on the original request is passed through unaltered.

If this is intended to be a permanent (301) redirect then only change the above 302 when you have tested that this is working OK, to avoid any caching issues messing with your testing. As always, clear your browser cache before testing.

1
  • And if you can instead put directives in Apache main configuration instead of .htaccess, then RewriteRule directives are far simpler to write since there you really deal with the complete URL. Commented Aug 14, 2018 at 17:04

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.