Any idea why this regex doesn't work? - regex

I'm using the code below in my .htaccess
RewriteEngine on
RewriteCond %{QUERY_STRING} (?:^|&)prod=\d+(?:&|$) [NC]
RewriteRule ^dumbwaiters/proddetail\.php\?* https://www.example.com/dumbwaiters/compare-dumbwaiters/ [NC,R=301,L]
It redirects to:
https://www.example.com/dumbwaiters/compare-dumbwaiters/?prod=143
But I don't want the trailing ?/prod=143
Any idea what I'm doing wrong?

http://httpd.apache.org/docs/current/rewrite/flags.html#flag_qsd
When the requested URI contains a query string, and the target URI
does not, the default behavior of RewriteRule is to copy that query
string to the target URI. Using the [QSD] flag causes the query string
to be discarded.
This flag is available in version 2.4.0 and later.
So if your Apache version is sufficient, I think the below untested code would work:
RewriteRule ^dumbwaiters/proddetail\.php.* https://www.example.com/dumbwaiters/compare-dumbwaiters/ [QSD,NC,R=301,L]

Related

How to remove Query Strings such as date= or author= from url in Wordpress using htaccess

I used following htacces code for removing date= query string from the URL
RewriteCond %{QUERY_STRING} (?:^|&)date=(.*)$
RewriteRule ^paivamaara/(.*)$ /paivamaara/$1?date=%1 [L,R]
its working on simple PHP file but when applied to Wordpress; its not working anymore.
Please help
That rule doesn't look right for removal of date parameter because you are adding it back in target.
You can use this redirect rule just below RewriteEngine On line to remove a specific query parameter:
RewriteEngine On
RewriteCond %{QUERY_STRING} ^(.*&)?date=[^&]*(?:&(.*))?$ [NC]
RewriteRule ^ %{REQUEST_URI}?%1%2 [R=302,NE,L]
# all WP rules come below this line
Note that this rule allows your query parameter to be positioned anywhere in a query string.
Here is regex demo for the regex used above.

.htaccess: Redirecting subfolder root for specific request only AND keep parameter

I'm struggling to implement a conditional redirect.
All requests example.com/app/?s={some string} (and nothing else) should be redirected to example.com/?s={some string}.
I tried many things and looking up StackOverflow. My final draft was this:
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_URI} ^\/app\/\?s=
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /? [R=301]
But it didn't work :-/ Any ideas?
UPDATE:
My approach now is
RewriteCond %{REQUEST_URI} ^\/app\/
RewriteCond %{QUERY_STRING} ^s=(.*)
RewriteRule (.*) / [R=301,L]
Still not working :D
UPDATE 2:
All requests example.com/app/s={some string} should be redirected to example.com/s={some string}
You have no ? in your example URLs, consequently there is no query string. s= is simply part of the URL-path (it's not strictly a URL parameter). (This does, however, contradict your code sample where you are trying to match a ? and query string? It is this that I was trying to clarify in comments.)
If s={some string} is really part of the URL-path then try the following instead:
RewriteEngine On
RewriteRule ^app/(s=.*) /$1 [R=302,L]
UPDATE: I forgot the ? in the exaple above
In that case your first "update" should have worked (depending on where you've put the directives). However, it would be better to write it like this:
RewriteCond %{QUERY_STRING} ^s=
RewriteRule ^app/$ / [R=302,L]
It's more efficient to check the URL-path in the RewriteRule pattern, since this is processed first. The query string is passed through by default (no need to capture {some string}).
Test with 302 (temporary) redirects to avoid caching issues. Change to a 301 (permanent) - if that is the intention - only once you have confirmed it is working OK.
You will need to clear your browser cache before testing.
The documentation tells us:
Modifying the Query String
By default, the query string is passed through unchanged
However, you are explicitly setting it to be empty.

Apache redirect only part of a query

I'm trying to redirect a query string to a non query url. It looks something like this.
Original
http://searchtest.help.org/search?client=Cool_Sub&site=Cool_Sub&proxystylesheet=Cool_Sub&q=stackoverflow
Expected result
https://searchtest.help.org/cool-search/stackoverflow
I only want to grab the last part of the query. In this case stackoverflow.
This part will always be static.
search?client=Cool_Sub&site=Cool_Sub&proxystylesheet=Cool_Sub&q=
I'm using this in my apache vhost
RewriteEngine On
RewriteCond %{REQUEST_URI} ^\/search$
RewriteCond %{QUERY_STRING} ^client=Cool_Sub&site=Cool_Sub&proxystylesheet=Cool_Sub&q=(.*)
RewriteRule ^(.*)$ https://searchtest.help.org/cool-search/%1 [R=301,L]
However the results with that is
https://searchtest.help.org/cool-search/stackoverflow?client=Cool_Sub&site=Cool_Sub&proxystylesheet=Cool_Sub&q=stackoverflow
I've tried many variations of the above rule but no dice. Any assistance would be grateful.
Add QSD flag to your RewriteRule
RewriteRule ^(.*)$ https://searchtest.help.org/cool-search/%1 [R=301,QSD,L]
According to Apache 2.4 documentation:
When the requested URI contains a query string, and the target URI
does not, the default behavior of RewriteRule is to copy that query
string to the target URI. Using the [QSD] flag causes the query string
to be discarded.

htaccess change url parameter

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?
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.
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

Generic URL rewrite keeping query string

What is the proper Apache mod_rewrite rule to change any
mydomain.com/?var1=val1&var2=val2&...&mode=app_mode&varN=valN&...
type query to
mydomain.com/app_mode/?var1=val1&var2=val2&...&varN=valN&...
(If the mode variable is not present in the QUERY_STRING, the URL should be kept as is.)
You can use this rule in your root .htaccess:
RewriteEngine On
RewriteCond %{QUERY_STRING} ^(.*&)?mode=([^&]+)(?:&(.*))?$ [NC]
RewriteRule ^/?$ /%2/?%1%3 [L,R=302,NE]
This allows mode parameter to be anywhere in the query string.