使url重写动态

时间:2014-02-20 14:38:51

标签: .htaccess url-rewriting

以下是我的网址重写示例。

# Redirect /page1.php?parameter-view=value to /page1/value
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s/+page1\.php\?parameter-view=([^\s]+) [NC]
RewriteRule ^ /page1/%1? [R=302,L]

# Internally forward /page1/value to /page1.php?parameter-view=value
RewriteRule ^page1/(.*)/(.*)/?$ /page1.php?parameter-view=$1&parameter-id=$2 [L,QSA,NC]

# Internally forward /page1/value to /page1.php?parameter-view=value
RewriteRule ^page1/(.*)?$ /page1.php?parameter-view=$1 [L,QSA,NC]

# Redirect /page2.php?parameter-view=value to /page2/value
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s/+page2\.php\?parameter-view=([^\s]+) [NC]
RewriteRule ^ /page2/%1? [R=302,L]

# Internally forward /page2/value to /page2.php?parameter-view=value
RewriteRule ^page2/(.*)/(.*)/?$ /page2.php?parameter-view=$1&parameter-id=$2 [L,QSA,NC]

# Internally forward /page2/value to /page2.php?parameter-view=value
RewriteRule ^page2/(.*)?$ /page2.php?parameter-view=$1 [L,QSA,NC]

我有一组page1和page2的重写。有没有办法结合这些重写,所以每次我创建一个新页面我可以添加页面“page3”而不是添加第三组重写?我想要的例子:

RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s/+(page1|page2|page3)\.php\?parameter-view=([^\s]+) [NC]
RewriteRule ^ /(page1|page2|page3)/%1? [R=302,L]


RewriteRule ^(page1|page2|page3)/(.*)/(.*)/?$ /(page1|page2|page3).php?parameter-view=$1&parameter-id=$2 [L,QSA,NC]

RewriteRule ^(page1|page2|page3)/(.*)?$ /(page1|page2|page3).php?parameter-view=$1 [L,QSA,NC]

最好的情况是,我不仅要改进为所有页面使用相同重写的想法,而且甚至不必使用3来完成这项工作?

1 个答案:

答案 0 :(得分:0)

确定你可以这样做:

RewriteCond %{THE_REQUEST} \s/+(page1|page2|page3)\.php\?parameter-view=([^\s]+) [NC]
RewriteRule ^ /%1/%2? [R=302,L]

RewriteRule ^(page1|page2|page3)/(.*)/(.*)/?$ /$1.php?parameter-view=$2&parameter-id=$3 [L,QSA,NC]

RewriteRule ^(page1|page2|page3)/(.*)/?$ /$1.php?parameter-view=$2 [L,QSA,NC]