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 10 months ago
yep, sorry, forgot about the year
Kuanysh 10 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})(.*)$ 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]
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 7 months 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 7 months ago
Sorry for late response, see my email and comments on the original bounty
Kuanysh 7 months ago

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