Writing some 301 rules for subdomain only - regex

I'm trying to write some 301 rules for some old backlinks, these would be placed in .htaccess within http://subdomain.domain.com (not in root .htaccess of domain.com).
Here are a some types of redirects I'd like to set up, first specific rules followed by a catch-all fallback. I would appreciate some advice on writing correct RewriteCond rules for this kind of logic, all of these have a specific final URL to redirect to:
A) request contains location=uae
= 301 to "http://www.domain.com/subfolder/sub2/sub3" (match pattern, redirect all to a specific final URL, no trailing slash).
B) request contains searchTerms=someterm1= 301 to "http://www.domain.com/subfolder2/program/url" (again match a paticular pattern and redirect all matches to a specific final URL with no trailing slash).
C) request contains reqGK=755381 or reqGK=795971 = 301 to "http://www.domain.com/another/specific/url" ... this RewriteCond would have a part of the regex that looks like reqGK=(755381|795971) i believe but i'm not totally sure.
D) [FALLBACK / CATCHALL for all requests not already matched by A-C above] ANY other request at all = 301 to "http://www.domain.com/specific/url/forfallback" (a final specific URL reference that gets sent all the remaining HTTP requests from subdomain.domain.com).
Thanks so much in advance!

Try this:
RewriteEngine On
RewriteCond %{QUERY_STRING} \blocation=uae\b
RewriteRule ^.*$ http://www.domain.com/subfolder/sub2/sub3 [L,R=301]
RewriteCond %{QUERY_STRING} \bsearchTerms=someterm1\b
RewriteRule ^.*$ http://www.domain.com/subfolder2/program/url [L,R=301]
RewriteCond %{QUERY_STRING} \breqGK=(755381|795971)\b
RewriteRule ^.*$ http://www.domain.com/another/specific/url [L,R=301]
RewriteRule ^.*$ http://www.domain.com/specific/url/forfallback [L,R=301]

Related

How to redirect any URL that contains 2 forward slashes to the homepage via .htaccess?

I need to redirect any URL that contains 2 or more forward slashes in a row back to the homepage.
I have tried:
RewriteRule example.com(.*)// https://example.com [R,L]
But it does not work and I don't understand why as it is pretty straightforward.
How can I do this?
Here is how you can achieve this (assuming /home is the path to your homepage. remove it in the RewriteRule if domain root is your homepage) :
RewriteEngine On
RewriteCond %{REQUEST_URI} //
RewriteRule ^(.*)$ %{SERVER_PROTOCOL}://%{HTTP_HOST}/home [R,L]
Explanation
RewriteCond %{REQUEST_URI} Condition is met if request uri contains a double slash. The slash does not need to be escaped (preceeded with \) as it carries no special meaning in the regex
RewriteRule ^(.*)$ %{SERVER_PROTOCOL}://%{HTTP_HOST}/home Rewrite the url and redirect to http(s)://yourdomain/home
Demo

Apache Redirect URI Requests

I'm trying to redirect http requests that contain a specific URI to a different domain with a different URI completely. Redirecting the top level domain works but I can't seem to get the URI rules to redirect.
In essence it should act as follows:
If the url request is:
www.example.com/unique-URI
it needs to redirect to:
https://example2.com/anotheruniqueURI
Currently I have this:
RewriteEngine On
#This redirect works successfully
RewriteCond %{HTTP_HOST} ^www\.example\.com$
RewriteRule ^(.*)$ http://example2.com/something [R=301,L]
#This attempt to redirect requests with the specific URI does not work.
RewriteCond %{HTTP_HOST} ^www\.example\.com
RewriteCond %{REQUEST_URI} ^/cars-application$ [NC]
RewriteRule ^/(.*)$ https://example2.com/anotherURI/ [R=301,NC,L]
I've tried many different combinations inside my RewriteRule such as explicitly stating the URI like I did in the RewriteCond above but that doesn't work. Using $1 here won't apply since I'm redirecting to a completely different domain and URI. The URI's I am expecting will be unique. Could you guys provide me some pointers. Is my regex correct or is my rewrite rule capture just wrong?
Your rule failed to work due to the leading slash in your RewriteRule's pattern . Remove the slash to fix it.
RewriteRule ^(.*)$ https://example2.com/anotherURI/ [R=301,NC,L]
Assuming you are redirecting from within a virtualhost of the first domain, you may just do the following:
Redirect permanent /unique-URI http://www.domain2.com/newlocation

.htaccess redirects aren't respecting my regex

I need to redirect any requests with query strings from a set of origin URLs back to a thank you page.
For example, I need to redirect:
http://example.com/test1/test2/[origin]/?id=1
back to
http://example.com/thank-you
The way I've got it set up in my .htaccess file is as such:
RewriteEngine On
RedirectMatch 302 ^/test1/test2/(.*)/.+ /thank-you
I've tested the regex I'm using in an online regex tester and it appears to work as expected, so I'm confused as to why the redirect isn't taking place. Here's the link to that.
Obviously, I had to add backslashes to escape the slashes in the URL in the regex tester, but based on my understanding of how .htaccess evaluates regex, these aren't necessary.
My question is: the redirect works perfectly from the page without the query string if I remove the .+ from the end of the regex string, meaning that the beginning part of the regex works fine. I don't understand why the query string isn't matching the regex I've created.
I have also tried:
RewriteCond %{REQUEST_URI} ^/test1/test2/(.*)/
RewriteCond %{QUERY_STRING} id=([0-9]+) [NC]
RewriteRule (.*) /thank-you [R=302,L]
For your RedirectMatch, you may use:
RedirectMatch 302 ^/test1/test2/(.*)/(.*)+ /thank-you?
For your RewriteRule section, you may use:
RewriteCond %{REQUEST_URI} ^/test1/test2/(.*)/
RewriteCond %{QUERY_STRING} id=([0-9]+) [NC]
RewriteRule (.*) /thank-you [R=302,L,QSD]
First , no need to RewriteEngine On with mod_alias which is RedirectMatch at your rules use it with mod_rewrite , the second rules .
Try this :
RewriteEngine On
RewriteCond %{QUERY_STRING} ^id=([0-9]+)$ [NC]
RewriteRule ^test1/test2/[^\/]+/$ /thank-you? [R=302,L]
I use ^id=([0-9]+)$ to restrict query string for a one that start with id and end with numerical value.
I remove this line RewriteCond %{REQUEST_URI} ^/test1/test2/(.*)/ becasue you could match against URI in RewriteRule as well.
If this rules wrok , change [R=302,L] to [R=301,L] to be permanent redirection.
Note: clear browser cache then test

htaccess rewrite from url query

I need some help trying to redirect a query to a url.
www.example.com/?categoryID=79
needs to redirect to
www.example.com/catname
where catname is just a string, it has no variables.
Here's what I tried so far:
I began with a humble approach that failed horribly:
redirect 301 /?CategoryID=79 http://www.example.com/catname/
then i moved on to mod_rewrite :
RewriteCond %{QUERY_STRING} CategoryID=79$
RewriteRule (.*) /catname/? [R=301,L]
Both did not work and I'm actually stomped.
any help would be appreciated.
Just to be clear, In the end i'll have many of these rules redirecting to various category names.
important - it's not enough for /catname to display the proper page, the request with the query parameter must redirect to the new url.
This rule should work for you as first rule in your .htaccess:
RewriteEngine On
RewriteCond %{THE_REQUEST} \s/+\?CategoryID=79[&\s] [NC]
RewriteRule ^ /catname/? [R=302,L]

HTACCESS 301 redirect URL with query string with no request URI

I have a site that has a bunch of URLs indexed in Google that look like this -
http://www.domain-namr.com/?option=filter&option2=&option3=
I am trying to redirect all of these to a new URL in HTACCESS with this code -
RewriteCond %{REQUEST_URI} ^/$
RewriteCond %{QUERY_STRING} ^option=filter$
RewriteRule ^(.*)$ /new-url.html [R=301,L]
Of course it is not working. On all of the pages that have a filename I can use in the request uri condition, the redirects work. What am I missing?
It is not working due to the wrong regex pattern in your rule.
Try this rule as your very first rule in root .htaccess:
RewriteCond %{QUERY_STRING} ^option=filter(&|$) [NC]
RewriteRule ^$ /new-url.html? [R=301,L]