大网数据广告活动
网站运营与故障问答

.htaccess redirect dynamic URL (with numeric ID) to remove "track" from the end

· 52CE 我爱测

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 num…

问题原文

原帖链接 · Jakub Staněk · CC BY-SA 4.0

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?

已采纳解答

原帖链接 · MrWhite · CC BY-SA 4.0

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.

摘自公开归档,保留原文语言,版式经整理,外部媒体与邮箱地址已移除。

资料来源与适用范围

资料来源
Webmasters Stack Exchange
适用范围
以原帖系统、软件版本与发布日期为准(2018-08-14)
内容语言
中文;命令、路径和错误标识保留技术原文
本地收录日期
2026-11-15
授权说明
CC BY-SA 4.0

正文与所需资源由本站本地存储。查看来源及更新记录 ↗

请按适用版本和实际日志确认原因;本站没有对每一种系统或设备逐项执行文中的操作。