1

I have a WP site which uses a calendaring plugin - currently the url the calendar system creates to change the month view of the calendar is hitting a url which fails to advance the month view of the calendar... I have worked out what the correct url should be - but need a way of redirecting from the incorrect url to the correct one...

So...

The incorrect url is: /calendar/?date=2018-04

The correct url is /calendar/?tribe-bar-date=2018-04

So, I am basically looking for a way to redirect / rewrite the url so that "?date=2018-04" becomes "?tribe-bar-date=2018-04" bearing in mind the year-month after the "=" element in the parameter will change all the time. So need to change "?date" to become "?tribe-bar-date"...

I have had a go using the redirection WP plugin with a rule as below:

/calendar/?date=(.*)
/calendar/?tribe-bar-date=(.*)

but it doesn't work... not sure why... I thought it would but I don't know regex very well!

Any ideas?

2 Answers 2

3

Try:

RewriteCond %{QUERY_STRING} (?:^|&)date=(.*)$
RewriteRule ^calendar/(.*)$ /calendar/$1?tribe-bar-date=%1 [L,R]

This assumes that the "date" parameter will be the first in the query string, otherwise you can add an additional grouping in front to try to capture that as well. This is doing a 302 (not permanent) redirect, you can change that if you want by changing the R to R=301.

Make sure this rule is before any wordpress rules.

Sign up to request clarification or add additional context in comments.

7 Comments

Thanks - added it to the htaccess file above the WP bits but doesnt change the URL when I hit the old /calendar/?date=2018-04 one...
@dubbs is there anything else in front of the URL before /calendar/? Where is the htaccess file at?
Thanks - works first time - but then the next time one clicks to view the next month the url becomes: /calendar/?tribe-bar-date=2018-04&date=2018-05
@dubbs see edit, I've added (?:^|&) as part of the condition's regex
Thanks @jon-lin That works now yes... many thanks - last issue... hopefully this is also doable... is that this site is multilingual - and so when the calendar is viewed in Chinese - the url string is "/calendar/?lang=zh-hans?tribe-bar-date=2018-04 - is it possible to amend the htaccess code to respect the "?lang=zh-hans" part of a url and not drop it out?
|
1

If you want to redirect externally only , do this :

RewriteEngine On
RewriteCond %{QUERY_STRING} ^date=(.+)$
RewriteRule ^calendar/$ /calendar/?tribe-bar-date=%1 [QSD,R=301,L,NE]

Or this :

RewriteEngine On
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s(/calendar/)\?date=(.+)\sHTTP.*$
RewriteRule ^         %1?tribe-bar-date=%2 [QSD,R=301,L,NE]

If it is externally for that target then internally to same path , do this :

RewriteEngine On
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s(/calendar/)\?date=(.+)\sHTTP.*$
RewriteRule ^         %1?tribe-bar-date=%2 [QSD,R=301,L,NE]
RewriteCond %{QUERY_STRING} ^tribe-bar-date=(.+)$
RewriteRule ^calendar/$ /calendar/?date=%1 [L,NE]

To prevent query string from being appended you should put ? at the end of RewriteRule substitution

In apache 2.4 and later you could discard query string by this flag [QSD] https://httpd.apache.org/docs/2.4/rewrite/flags.html

5 Comments

Hi - 2nd approach works first time... but as above then the next time one clicks to view the next month the url becomes: /calendar/?tribe-bar-date=2018-04&date=2018-05
looks like you updated the 1st option - but that didnt seem to work.. any chance for an update to the 2nd option to make it work?
The 2nd option has not changed though? RewriteEngine On RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s(/calendar/)\?date=(.+)\sHTTP.*$ RewriteRule ^ %1??tribe-bar-date=%2 [L,R=301] - this is still as your first post...? It doesnt work..?
Sorry - I think the issue here is more that the calendar a href is adding a new paramater to the old one - and making a url like /calendar/?tribe-bar-date=2018-01&date=2018-02
@dubbs does your apche 2.4 or later , try to change this [L,R=301] in 2nd solution by this [QSD,L,R=301]

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.