Modify htaccess redirect so it uses a wildcard
New here? Learn about Bountify and follow @bountify to get notified of new bounties! x

I have the following redirect:

RewriteRule ^spring16(.*)$ http://school.edu/~course/archive/course_spring16/public_html$1 [R=302,NC,L]

It works as expected, e.g.:

http://school.edu/~course/spring16/
redirects to:
http://school.edu/~course/archive/course_spring16/public_html/

http://school.edu/~course/spring16/schedule.html
redirects to:
http://school.edu/~course/archive/course_spring16/public_html/schedule.html


Modification needed:

Right now it's hard-coded for spring16, but needs to be made to match fallXX or springXX.

In short semesterYY needs to be a wildcard.

awarded to Kuanysh

Crowdsource coding tasks.

5 Solutions


Hi, this should work:

RewriteRule ^spring16(.*)$ http://school.edu/~course/archive/course_spring16/public_html$1 [R=302,NC,L]
RewriteRule ^fall16(.*)$ http://school.edu/~course/archive/course_fall16/public_html$1 [R=302,NC,L]
That won't work with, for example, spring14 or fall10...The year (YY) needs to be a wildcard.
Difranco 1 month ago
yep, sorry, forgot about the year
Kuanysh 1 month ago

Hi, haven't tested but I'm fairly confident with this assuming you want just spring and fall semesters as options and there will always be 2 digits... so spring16, fall18, etc..

RewriteRule ^((?:spring|fall)[\d]{2})(.*)$ http://school.edu/~course/archive/course_$1/public_html$2

Feel free to test this regex here: https://regex101.com/r/AHMl7m/1

Winning solution

Updating:

RewriteRule ^(fall|spring)(\d+)(.*)$ http://school.edu/~course/archive/course_$1$2/public_html$3 [R=302,NC,L]

Hi Difranco,

Checked and tested

RewriteEngine On
RewriteRule ^(.*)/((spring|fall)[\d]{2})(.*)$ http://school.edu/~course/archive/course_$2/public_html$4 [R=302,NC,L]

If you have base already existing in your .htaccess here is the code

RewriteEngine On
RewriteBase /~course
RewriteRule ^((spring|fall)[\d]{2})(.*)$ http://school.edu/~course/archive/course_$1/public_html$3 [R=302,NC,L]

Hi

RewriteRule ^([a-zA-Z]+)(\d{2})(.*)$ http://school.edu/~course/archive/course_$1$2/public_html$3 [R=302,NC,L]

A small update that can manage all sorts of string preceeding your year, for example availability to manage month dynamically.

View Timeline