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(.*)$$1 [R=302,NC,L]

It works as expected, e.g.:
redirects to:
redirects to:

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(.*)$$1 [R=302,NC,L]
RewriteRule ^fall16(.*)$$1 [R=302,NC,L]
That won't work with, for example, spring14 or fall10...The year (YY) needs to be a wildcard.
Difranco 3 months ago
yep, sorry, forgot about the year
Kuanysh 3 months 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})(.*)$$1/public_html$2

Feel free to test this regex here:

Winning solution


RewriteRule ^(fall|spring)(\d+)(.*)$$1$2/public_html$3 [R=302,NC,L]

Hi Difranco,

Checked and tested

RewriteEngine On
RewriteRule ^(.*)/((spring|fall)[\d]{2})(.*)$$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})(.*)$$1/public_html$3 [R=302,NC,L]


RewriteRule ^([a-zA-Z]+)(\d{2})(.*)$$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