Wordpress mod_重写替换查询字符串键

Wordpress mod_重写替换查询字符串键,wordpress,.htaccess,mod-rewrite,Wordpress,.htaccess,Mod Rewrite,简而言之,我正在尝试更改给定的URL: http://mydomain.com/category/title/attachment/randomstring_?resolution=320x480 与 本质上,从查询字符串中删除?resolution=,并将值添加到路径中 类别、标题和随机字符串值是动态的,但它们的位置应始终相同 我不知道这是否重要,但这正被用于WordPress 我尝试了以下方法,但只得到了404: <IfModule mod_rewrite.c> Rewrite

简而言之,我正在尝试更改给定的URL:

http://mydomain.com/category/title/attachment/randomstring_?resolution=320x480

本质上,从查询字符串中删除
?resolution=
,并将值添加到路径中

类别、标题和随机字符串值是动态的,但它们的位置应始终相同

我不知道这是否重要,但这正被用于WordPress

我尝试了以下方法,但只得到了404:

<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /

RewriteCond %{QUERY_STRING} ^resolution=([0-9]{3,4}x[0-9]{3,4})$
RewriteRule ^$ ${REQUEST_URI}%1/? [R]

RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>

重新启动发动机
重写基/
RewriteCond%{QUERY_STRING}^分辨率=([0-9]{3,4}x[0-9]{3,4})$
重写规则^$${REQUEST_URI}%1/?[R]
重写规则^index\.php$-[L]
重写cond%{REQUEST_FILENAME}-F
重写cond%{REQUEST_FILENAME}-D
重写规则/index.php[L]

感谢您的帮助;)

经过多次尝试和错误(以及读取RewriteLog输出),我终于通过以下方式实现了:

<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]

RewriteCond %{QUERY_STRING} ^resolution=(.*)$
RewriteRule . %{REQUEST_URI}%1? [R=301,L]

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>

重新启动发动机
重写基/
重写规则^index\.php$-[L]
RewriteCond%{QUERY_STRING}^resolution=(.*)$
重写规则%{请求URI}%1?[R=301,L]
重写cond%{REQUEST_FILENAME}-F
重写cond%{REQUEST_FILENAME}-D
重写规则/index.php[L]
原始解决方案的关键问题:

  • 重写规则中使用
    ^$
    而不是
  • 重写规则中使用
    /?
    而不仅仅是
  • 重写规则
    标志中不包括
    ,L
对于任何好奇的人,在
重写规则中包括
可以防止查询字符串保留在请求中

<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]

RewriteCond %{QUERY_STRING} ^resolution=(.*)$
RewriteRule . %{REQUEST_URI}%1? [R=301,L]

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>