mod_rewrite替换查询字符串键

时间:2011-02-10 00:50:50

标签: wordpress .htaccess mod-rewrite

简而言之,我正在尝试更改指定的网址:

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

使用

http://mydomain.com/category/title/attachment/randomstring_320x480

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

类别,标题和randomstring值是动态的,但它们的位置应始终相同。

我不知道这是否重要,但这是在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>

感谢任何帮助;)

1 个答案:

答案 0 :(得分:1)

好吧,经过大量的试验和错误(并阅读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>

原始解决方案的关键问题:

  • ^$
  • 中使用.代替RewriteRule
  • /?
  • 中使用?代替RewriteRule
  • ,L旗帜
  • 中不包括RewriteRule

对于任何好奇的人,包括?中的RewriteRule会阻止QUERY_STRING留在请求中。

相关问题