.htaccess RewriteRule(s) not working

Multi tool use
.htaccess RewriteRule(s) not working
I am trying to learn how to use RewriteRule.
I have 2 'pages' (wordpress)-domain.com/webinars/
(shows list of all webinars)
anddomain.com/webinar/
(shows specific webinar details)
domain.com/webinars/
domain.com/webinar/
I have trying to set the following conditions -
(1) domain.com/webinars/{Year}/{Month}/
will load /webinar/?year={Year}&month={Month}
domain.com/webinars/{Year}/{Month}/
/webinar/?year={Year}&month={Month}
(2) domain.com/webinar/
(with no /{Year}/{Month}
) will load /webinars/
domain.com/webinar/
/{Year}/{Month}
/webinars/
(3) domain.com/webinar/?year={Year}&month={Month}
will redirect to /webinars/{Year}/{Month}/
and then apply condition #1
domain.com/webinar/?year={Year}&month={Month}
/webinars/{Year}/{Month}/
This is my attempted code
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]
Condition #1 results in a 404 page not found
Condition #2 shows /webinar/
and not /webinars/
/webinar/
/webinars/
Condition #3 stays on domain.com/webinar/?year={Year}&month={Month}
and does not redirect
domain.com/webinar/?year={Year}&month={Month}
What am I doing wrong? Only other code in my htaccess file is the default wordpress block.
3 Answers
3
Try the following :
Options +FollowSymlinks
RewriteEngine on
RewriteRule ^webinars/([^/]+)/([^/]+) /webinar/?year=$1&month=$2 [NC]
The rest could be done the same way .
([0-9]+)
([A-Za-z0-9]+)
([^/]+)
@Sean
([^/]+)
means any character not matching /
, at least one .– Ashraf
Jul 3 at 4:19
([^/]+)
/
@Sean I tried it and it seems to be working , try to test the first rule only as there might be a conflict with the rules . pasteboard.co/HsGX4xR.png
– Ashraf
Jul 3 at 4:25
hmm... I have removed the other 2 rules, and have tried it both before and after the wordpress code block. still getting a 404 not found
– Sean
Jul 3 at 4:30
My suggestion is to check if there's any other htaccess file somewhere else . Also please double check if you put your htaccess file in the root . Finally please review the server log to see what's the 404 path you are being redirected to .
– Ashraf
Jul 3 at 4:39
Try with below rules,
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]
unfortunately no change as well. still getting
404 GET /webinars/2018/May/ HTTP/1.0
in logs and in the browser– Sean
Jul 3 at 5:32
404 GET /webinars/2018/May/ HTTP/1.0
what happen if you use directly webinar/?year=2018&month=May is it working fine?
– Abhishek Gurjar
Jul 3 at 5:34
yes,
https://example.com/webinar/?year=2018&month=May
loads correctly, as does https://example.com/webinars/
.– Sean
Jul 3 at 5:36
https://example.com/webinar/?year=2018&month=May
https://example.com/webinars/
You can use this
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]
getting a
500 [core:alert] RewriteCond: cannot compile regular expression '/webinar/?year=([^s&]+)&month=([^s&)'
.– Sean
Jul 3 at 6:39
500 [core:alert] RewriteCond: cannot compile regular expression '/webinar/?year=([^s&]+)&month=([^s&)'
Sorry there was a typo. I have edited the code
– starkeen
Jul 3 at 6:43
unfortunately no change. going to sleep on this, and see if I can figure it out in the morning. thanks for your help
– Sean
Jul 3 at 6:56
By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.
No change. What is the difference between
([0-9]+)
/([A-Za-z0-9]+)
and([^/]+)
?– Sean
Jul 3 at 4:14