.htaccess RewriteRule(s)不起作用

时间:2018-07-03 03:54:26

标签: .htaccess mod-rewrite

我正在尝试学习如何使用RewriteRule。

我有2个“页面”(wordpress)-
domain.com/webinars/(显示所有网络研讨会的列表)

domain.com/webinar/(显示特定的网络研讨会详细信息)

我试图设置以下条件-

(1)domain.com/webinars/{Year}/{Month}/将加载/webinar/?year={Year}&month={Month}

(2)domain.com/webinar/(无/{Year}/{Month})将加载/webinars/

(3)domain.com/webinar/?year={Year}&month={Month}将重定向到/webinars/{Year}/{Month}/,然后应用条件#1

这是我尝试的代码

RewriteEngine On

RewriteRule ^/webinars/([0-9]+)/([A-Za-z0-9]+)/$ /webinar/?year=$1&month=$2 [NC]

RewriteRule ^/webinar/$ /webinars/

RewriteRule ^/webinar/year=(\d+)$month=([\w-]+)$ /webinars/%1/%2? [R=301,L]

条件1导致找不到404页

条件2显示/webinar/而不显示/webinars/

条件#3停留在domain.com/webinar/?year={Year}&month={Month}上,并且不重定向

我在做什么错?我的htaccess文件中只有其他代码是默认的wordpress块。

3 个答案:

答案 0 :(得分:0)

尝试以下方法:

Options +FollowSymlinks
RewriteEngine on
RewriteRule ^webinars/([^/]+)/([^/]+) /webinar/?year=$1&month=$2 [NC]

其余的方法也可以一样。

答案 1 :(得分:0)

尝试以下规则,

RewriteCond %{REQUEST_URI} ^/webinar$
RewriteCond %{QUERY_STRING} ^year=([^/]+)&month=([^/]+)
RewriteRule ^ http://example.com/webinars/%1/%2/? [R=302]


RewriteCond %{REQUEST_URI} ^/webinars/([^/]+)/([^/]+)/$
RewriteRule ^ webinar/?year=%1&month=%2 [L,END]

RewriteCond %{REQUEST_URI} ^/webinar/$
RewriteRule ^ webinars/ [L]

答案 2 :(得分:0)

您可以使用

RewriteEngine on
# if /webinar/ is requested skip the rules and serve /webinar/ directory
RewriteRule ^webinar/?$ -  [L]
#redirect /webinar/?year={year}&month={month}
#To /webinar/year/month
RewriteCond %{THE_REQUEST} /webinar/\?year=([^\s&]+)&month=([^\s&]+) [NC]
RewriteRule ^.+$ /webinar/%1/%2? [L,R]
# rewrite new url to the old one
RewriteRule ^webinar/([^/]+)/([^/]+)/?$ /webinar/?year=$1&month=$2 [L,NC]