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

How to match only part of URL and redirect URL with parameters in .htaccess?

· 52CE 我爱测

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…

问题原文

原帖链接 · Alan Kis · CC BY-SA 3.0

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]

已采纳解答

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

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.

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

资料来源与适用范围

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

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

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