htaccess将重写条件合并/减少为一个(或至少更少)

时间:2014-07-16 11:57:26

标签: apache .htaccess mod-rewrite url-rewriting rewrite

我有一个课程预订组件,不幸的是,它为同一页面提供了三个不同的URL,具体取决于您是通过菜单链接,面包屑还是组件内的链接进入。

因此,我按照以下方式为htaccess文件添加了一个新的重写条件,如下所示:

RewriteCond %{REQUEST_URI} ^/citb/smsts/book-now$ [NC,OR]
RewriteCond %{REQUEST_URI} ^/citb/smsts/book-now/$ [NC,OR]
RewriteCond %{REQUEST_URI} ^/citb/smsts/book-now/1$ [NC]
RewriteRule .* /citb/smsts/book-now/1-smsts [R=301,NC,L]

RewriteCond %{REQUEST_URI} ^/citb/smsts-refresher/book-now$ [NC,OR]
RewriteCond %{REQUEST_URI} ^/citb/smsts-refresher/book-now/$ [NC,OR]
RewriteCond %{REQUEST_URI} ^/citb/smsts-refresher/book-now/2$ [NC]
RewriteRule .* /citb/smsts-refresher/book-now/2-smsts-refresher [R=301,NC,L]

RewriteCond %{REQUEST_URI} ^/citb/sssts/book-now$ [NC,OR]
RewriteCond %{REQUEST_URI} ^/citb/sssts/book-now/$ [NC,OR]
RewriteCond %{REQUEST_URI} ^/citb/sssts/book-now/3$ [NC]
RewriteRule .* /citb/sssts/book-now/3-sssts [R=301,NC,L]

RewriteCond %{REQUEST_URI} ^/citb/sssts-refresher/book-now$ [NC,OR]
RewriteCond %{REQUEST_URI} ^/citb/sssts-refresher/book-now/$ [NC,OR]
RewriteCond %{REQUEST_URI} ^/citb/sssts-refresher/book-now/4$ [NC]
RewriteRule .* /citb/sssts-refresher/book-now/4-sssts-refresher [R=301,NC,L]

RewriteCond %{REQUEST_URI} ^/citb/health-and-safety-awareness/book-now$ [NC,OR]
RewriteCond %{REQUEST_URI} ^/citb/health-and-safety-awareness/book-now/$ [NC,OR]
RewriteCond %{REQUEST_URI} ^/citb/health-and-safety-awareness/book-now/7$ [NC]
RewriteRule .* /citb/health-and-safety-awareness/book-now/7-health-and-safety-awareness [R=301,NC,L]

RewriteCond %{REQUEST_URI} ^/iosh/managing-safely/book-now$ [NC,OR]
RewriteCond %{REQUEST_URI} ^/iosh/managing-safely/book-now/$ [NC,OR]
RewriteCond %{REQUEST_URI} ^/iosh/managing-safely/book-now/8$ [NC]
RewriteRule .* /iosh/managing-safely/book-now/8-iosh-managing-safely [R=301,NC,L]

RewriteCond %{REQUEST_URI} ^/iosh/working-safely/book-now$ [NC,OR]
RewriteCond %{REQUEST_URI} ^/iosh/working-safely/book-now/$ [NC,OR]
RewriteCond %{REQUEST_URI} ^/iosh/working-safely/book-now/9$ [NC]
RewriteRule .* /iosh/working-safely/book-now/9-iosh-working-safely [R=301,NC,L]

RewriteCond %{REQUEST_URI} ^/first-aid/3-day-first-aid-at-work/book-now$ [NC,OR]
RewriteCond %{REQUEST_URI} ^/first-aid/3-day-first-aid-at-work/book-now/$ [NC,OR]
RewriteCond %{REQUEST_URI} ^/first-aid/3-day-first-aid-at-work/book-now/5$ [NC]
RewriteRule .* /first-aid/3-day-first-aid-at-work/book-now/5-3-day-first-aid-at-work [R=301,NC,L]

RewriteCond %{REQUEST_URI} ^/first-aid/1-day-first-aid-at-work/book-now$ [NC,OR]
RewriteCond %{REQUEST_URI} ^/first-aid/1-day-first-aid-at-work/book-now/$ [NC,OR]
RewriteCond %{REQUEST_URI} ^/first-aid/1-day-first-aid-at-work/book-now/6$ [NC]
RewriteRule .* /first-aid/1-day-first-aid-at-work/book-now/6-1-day-first-aid-at-work [R=301,NC,L]

这显然不是一种理想的方法,可以在每次添加新课程时不断重复(是的,我知道最好的解决方案实际上是破解组件,以便它不会做这个)所以我想知道是否有一种方法可以使这个更通用,无论是一个重写条件,还是每个类别一个,或者它可能是最好的推广?

每个所需/目标网址将采用以下格式:

/[category]/[course]/book-now/[id]-[alias]

从上面的第一个例子开始:

category = citb
course = smsts
id = 1
alias = smsts 

非常感谢提前!

1 个答案:

答案 0 :(得分:0)

实际上,我不认为(或者我不知道如何)你可以减少所有的重定向(你实际上无法猜测你的第一个条件中缺少的数字)。 但你实际上可以通过以下方式处理所有第三个条件:

RewriteEngine On
RewriteRule ^([^/]*)/([^/]*)/book-now/([0-9]+)$ /$1/$2/book-now/$3-$2 [R=301,NC,L]

并在最后使用(/?)结合您的第一个条件。

这会使:

RewriteEngine On
RewriteRule ^([^/]*)/([^/]*)/book-now/([0-9]+)$ /$1/$2/book-now/$3-$2 [R=301,NC,L]

RewriteCond %{REQUEST_URI} ^/citb/smsts/book-now(/?)$ [NC]
RewriteRule .* /citb/smsts/book-now/1-smsts [R=301,NC,L]
... 

PS :您写道:

每个所需/目标网址将采用以下格式:

/[category]/[course]/book-now/[id]-[alias]

从上面的第一个例子开始:

category = citb
course = smsts
id = 1
alias = smsts 

但实际上,对于您的/iosh/working-safely/book-now/9网址,您的格式为:

alias = [category]-[course]

因为它变成

/iosh/working-safely/book-now/9-iosh-working-safely

不确定你的htaccess是否有误,似乎很重要。

希望它有所帮助!