用动态查询字符串将旧格式重写为新的动态等效格式

时间:2018-07-29 01:08:25

标签: .htaccess mod-rewrite

实际上我有一个网站,但我不知道该如何解决这种问题。

所以,我的网站是qapaper.com,并且有一些链接使用PHP GET方法在单个页面上重定向,这些方法是:

from django.urls import path
from videos import views

urlpatternes = [
path('', views.index , name = 'index'),
]

每个都有不同年份的试卷,现在出现了一个问题,我该如何将它们更改为以下内容:

 https://qapaper.com/first.php?id=1
 https://qapaper.com/first.php?id=2
 https://qapaper.com/first.php?id=3
 https://qapaper.com/first.php?id=4

1 个答案:

答案 0 :(得分:0)

如果只有四个条目,则可以静态

Options -MultiViews
RewriteEngine On

# condensated way to redirect /first.php?id=[1-4] to /first /second /third or /fourth
RewriteCond %{THE_REQUEST}::first \s/first\.php\?id=1\s.+::(first)$ [OR,NC]
RewriteCond %{THE_REQUEST}::second \s/first\.php\?id=2\s.+::(second)$ [OR,NC]
RewriteCond %{THE_REQUEST}::third \s/first\.php\?id=3\s.+::(third)$ [OR,NC]
RewriteCond %{THE_REQUEST}::fourth \s/first\.php\?id=4\s.+::(fourth)$ [NC]
RewriteRule ^ /%1? [R=301,L]

# internally rewrite back
RewriteRule ^first$ /first.php?id=1 [L,NC]
RewriteRule ^second$ /first.php?id=2 [L,NC]
RewriteRule ^third$ /first.php?id=3 [L,NC]
RewriteRule ^fourth$ /first.php?id=4 [L,NC]

否则,您可以使用RewriteMap(请参阅here