htaccess change url parameter

冷暖自知 提交于 2021-01-28 05:39:07

问题


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?


回答1:


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.




回答2:


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



来源:https://stackoverflow.com/questions/49366468/htaccess-change-url-parameter

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!