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 4 months ago
yep, sorry, forgot about the year
Kuanysh 4 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]
Kuanysh, this is George. Can you please update me on our Drag and drop bounty and contact me via email? I wish to hire you for additional projects if you are interested. I have provided you my email in the other commetns. Thank you.
georgefountain 1 month ago
The bounty was about to expire and I have awarded it to you instead of to the other developer, trusting that you will finish it. Please update me and let me know if you will finish it or not and if you are working on it or not . Thank you. Remember to contact me via email also. thanks
georgefountain 1 month ago
Sorry for late response, see my email and comments on the original bounty
Kuanysh 1 month ago

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