.htacces redirect with wildcard match - .htaccess

I'm moving an old site to a new platform and the query strings have changed so looking to 301 them but no luck yet... so an old search string by state for example I'm using:
RewriteCond %{QUERY_STRING} ^a=19&b[yes_state]=(&.*)?$ [NC]
RewriteRule ^c/system\.php$ https://thenewwebsite.com/?cp_search=1&spost_region=%1 [R=301,NE,NC,L]
to basically send:
theoldwebsite.com/c/system.php?a=19&b[yes_state]=Texas
to
thenewwebsite.com/?search=1&t_region=Texas

In regular expression [ and ] are special characters ,you need to escape them by using a backslash in front to match them litterlly.
RewriteCond %{QUERY_STRING} ^a=19&b\[yes_state\]=(.*)$ [NC]
RewriteRule ^c/system\.php$ https://thenewwebsite.com/?cp_search=1&spost_region=%1 [R=301,NE,NC,L]

Related

Redirect a wild card URL to another custom URL

I need to redirect a URL with parameters to a custom URL in htaccess.
example.com/folder?misc-params-that-can-change
should redirect to
my.customlink.com
I don't know how to write the wildcard for the parameters that start with the question mark.
This is where I'm at now:
RewriteEngine On
RewriteCond %{REQUEST_URI} ^/folder [NC]
RwriteRule ^folder?(.*)$ http://www.google.com/$1 [R=301,L]
You may use this rule:
RewriteEngine On
RewriteCond %{REQUEST_URI} ^/folder/?$ [NC]
RewriteCond %{QUERY_STRING} .
RewriteRule ^ http://www.google.com/? [R=301,NC,L]
You cannot match query string in the pattern of RewriteRule, need to use %{QUERY_STRING} variable in RewriteCond construct for this purpose.
? after target URL is used to discard previous query string.

How do I create dynamic friendly urls for a query string?

So currently I have URLs that looks like this:
http://localhost/?v=register
http://localhost/?v=profile&u=thatgerhard
I want the above to look like this:
http://localhost/register/ (without trailing /)
http://localhost/profile/thatgerhard/ (without trailing /)
I spent a ton of time trying to get this to work even though it seems like it should be a simple fix.
This is what I have atm:
RewriteBase /
RewriteEngine On
RewriteRule ^((.)*)$ /?v=$1&p=$
I would ideally like this to be dynamic so that if you do ?v=foo it will automatically do /foo/ without adding "foo" to the htaccess file.
Any help or direction will be greatly appreciated. :)
You should use RewriteCond Directive and RewriteCond backreferences. The %1 backreference matches the query string parameter v, the %2 backreference matches the query string parameter u. The RewriteRule redirects the request permanently and discard the query string entirely.
RewriteCond %{QUERY_STRING} ^v=([^&]*)$ [NC]
RewriteRule ^/?$ /%1/? [R=301,L]
RewriteCond %{QUERY_STRING} ^v=([^&]*)&u=([^&]*)$ [NC]
RewriteRule ^/?$ /%1/%2/? [R=301,L]

Htaccess redirect with parameter?

I'm trying to do a .htaccess redirect with a parameter but it's not working. Seems like my regex are also wrong. :(
Original URL 1: http://www.example.com/?team=john-doe
Original URL 2: http://www.example.com/?team
Target URL: http://www.example.com/company/
I tried:
RewriteEngine on
RewriteCond %{QUERY_STRING} team=john-doe
RewriteRule /company/ [L,R=301]
Any help would be much appreciated.
Found a generator that works perfectly:
https://donatstudios.com/RewriteRule_Generator
# 301 --- http://www.example.com/?team=john-doe => http://www.example.com/company/
RewriteCond %{QUERY_STRING} (^|&)team\=john\-doe($|&)
RewriteRule ^$ /company/? [L,R=301]
Your RewriteRule is malformed, you are missing a pattern (first argument). Try something like:
RewriteEngine on
RewriteCond %{QUERY_STRING} ^team(?:=john-doe)?
RewriteRule ^$ /company/? [R=301,L]
The RewriteRule pattern ^$ matches requests for the domain root. The ? on the end of the RewriteRule substitution strips the query string from the redirected URL (on Apache 2.4+ you can use the QSD flag instead).
The CondPattern ^team(?:=john-doe)? matches either "team=john-doe" (URL#1) or "team" (URL#2) at the start of the query string. The (?: part just makes it non-capturing.
You will need to clear your browser cache before testing.

Multipart htaccess redirect

Hey guys I'm having a bit of trouble getting my htaccess to redirect properly and was hoping for some help.
I'm expecting DEV-domain.com?CampID=AB12345 to redirect to
http://DEV-www.domain.com/landing/external-marketing/direct-mail/AB?CampId=AB12345
RewriteCond %{HTTP_HOST} ^DEV-(www\.)?domain\.com [NC]
RewriteCond %{QUERY_STRING} ^CampID=
RewriteRule (\w{2})(\w{5})$ http://DEV-www\.domain\.com/landing/external-marketing/direct-mail/$1?CampId=$1$2 [R=301,L]
Unfortunetly I can't get it working for some reason?
Because the RewriteRule matching is meant for the url path, not query strings. Try this:
RewriteCond %{HTTP_HOST} ^DEV-(www\.)?domain\.com [NC]
RewriteCond %{QUERY_STRING} ^CampID=(\w{2})(\w{5})
RewriteRule .* http://DEV-www.domain.com/landing/external-marketing/direct-mail/%1?CampId=%1%2 [R=301,L]
also you don't need to escape dots . in the target url, only in matching patterns. And be aware that if you decide to make your target url CampID instead of CampId, you need to put in another condition:
RewriteCond %{REQUEST_URI} !^/landing/external-marketing/direct-mail/
to avoid an infinite redirect as a target with CampID would match your RewriteCond rule...

replace character in query string via .htaccess

My client wants a query string munged (by changing % to A) on certain pages.
For example, I can remove the query string completely on the desired pages via:
RewriteCond %{QUERY_STRING} !=""
RewriteCond %{REQUEST_URI} ^/SpecialPage(.*)
RewriteRule ^(.*)$ /$1? [R=301,L] #remove query string
Here's what I thought should remove % on the query string and replace with A but it's not:
RewriteCond %{QUERY_STRING} ^(.*)\%(.*)$
RewriteCond %{REQUEST_URI} ^/SpecialPage(.*)
RewriteRule ^(.*)$ /$1?%1A%2 [L]
What am I doing wrong in this? I just can't quite spot it. Thanks for the expert eyes!
You're real close.
The problem here is that you've got a condition and the match of your rule should be together. Your backreference to the previous RewriteCond is broken because it's for the REQUEST_URI and not the QUERY_STRING like you want.
RewriteCond %{REQUEST_URI} ^/SpecialPage(.*)
RewriteRule ^(.*)$ /$1?%1A%2 [L]
Here, the %1 backreference matches the (.*) at the end of the /SpecialPage URI. The backreferences from your query string match gets lost, and that's the ones you really want. You can combine the condition to match the REQUEST_URI with the regular expression pattern in the RewriteRule:
RewriteCond %{QUERY_STRING} ^(.*)\%(.*)$
RewriteRule ^SpecialPage(.*)$ /SpecialPage$1?%1A%2 [L]
Here, the %1 and %2 backreferences correctly reference the query string and the SpecialPage condition in the URI is met by the regex pattern.
Redirect Query String
RewriteEngine On
RewriteCond %{QUERY_STRING} ^(.)=(.)$
RewriteRule ^(.)?(.)$ /$1--%0? [R=301,L]
From Url: http://localhost/sholay-slide.jsp?slide=2
To Url: http://localhost/sholay-slide.jsp--slide=2

Resources