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.