URL将旧URL重写为新URL

时间:2014-02-28 02:59:56

标签: regex apache .htaccess rewrite

我正在尝试改写这个:

index.php?action=dlattach;topic=2745.0;attach=14374;image

到此:

attachments/opening-inauguracion-png.14374/

到目前为止我已经构建但是它不起作用是这样的:

RewriteRule ^index\.php?=dlattach;topic=([0-9]+);attach=([0-9]+);image /attachments/$2/ [R=301,L]

2 个答案:

答案 0 :(得分:3)

要操纵查询字符串,您需要使用%{QUERY_STRING}重写条件:

Options +FollowSymLinks -MultiViews

RewriteEngine On
RewriteBase /

RewriteCond %{QUERY_STRING} attach=([0-9]+) [NC]
# if you need a more exact match of your current query string
# comment the above RewriteCond and uncomment the below RewriteCond
#RewriteCond %{QUERY_STRING} ^action=dlattach;topic=[0-9\.]+;attach=([0-9]+);image$ [NC]
RewriteRule ^index.php$ attachments/opening-inauguracion-png.%1/ [R=302,NC,L]

一旦确认规则正在努力避免在测试规则时缓存浏览器,只有在规则不能正常工作以缓解规则时,才将其更改为301。

答案 1 :(得分:0)

我认为你的意思恰恰相反:

RewriteEngine On
RewriteRule ^attachments/opening-inauguracion-png.([0-9]+)$ /index.php?action=dlattach;topic=2745.0;attach=$1;image [R=301,L]