htaccess redirect if url contains specific string - .htaccess

We've installed a new shop software and I've set up redirects for all of the old pages, but there's one that I just can't get to work. If the url includes
cp_tpl=productlist.html
then I want to redirect to the main website. So for example the old url could be
http://www.gocellular.ca/index.php?cp_tpl=productlist.html&cp_cat=803&cp_sid=1xxxxxx
and I want to redirect to
www.gocellular.ca
The 'cp_tpl=productlist.html' can be anywhere in the url - basically I just want to check if the string 'cp_tpl=productlist.html' is included anywhere in the url and then redirect. I've tried about 100 different .htaccess re-writes but just can't get this to work! I'd be very grateful for any ideas.... THANK YOU!

In order to catch that string in the query string as you have shown it there you have to use a RewriteCond, as the regular expression doesn't get checked against the query string in a redirect rule. Something like this should do the trick:
RewriteCond %{QUERY_STRING} cp_tpl=productlist.html
RewriteRule .* / [R,L]
The above will keep the query string intact. If yow want to remove the query string, just add a ? after /, i.e.:
RewriteRule .* /? [R,L]

Related

Redirect a URL in .htaccess, but keep the query string

I want to redirect a URL in .htaccess, but want to keep the (dynamic) parameters from the query string at the end of the URL (e.g. ?id=1660, ?id=1661, etc.)
E.g.
https://mywebsite.example/service/viewinvoice.php?id=1660
I want to redirect it to:
https://mywebsite.example/whmcs-bridge/?ccce=viewinvoice.php?id=1660
So basically: https://mywebsite.example/service/viewinvoice.php?id=... needs to be redirected to https://mywebsite.example/whmcs-bridge/?ccce=viewinvoice.php?id=...
I tried this below, without any success
RewriteEngine On
RewriteCond %{QUERY_STRING} (^|&)/service/viewinvoice.php?id= [NC]
RewriteRule ^/?$ /whmcs-bridge/?ccce=viewinvoice.php [L,R=301]
I think this is not the right solution.
Does someone has suggestions?
You need to use the QSA (Query String Append) flag on your rewrite rule. From the documentation:
When the replacement URI contains a query string, the default behavior of RewriteRule is to discard the existing query string, and replace it with the newly generated one. Using the [QSA] flag causes the query strings to be combined.
From your example URLs, you don't need to match the query string in a rewrite condition. You are matching the URL path which is done as the first part of the rewrite rule itself.
Your rule should be:
RewriteEngine On
RewriteRule ^/?service/viewinvoice\.php$ /whmcs-bridge/?ccce=viewinvoice.php [L,R=301,QSA]

Htaccess remove word and some string from url

I have discovered that some visitors to my site connect with a weird URL:
Example: http://www.example.com/article/?epik=3xFD_XXs
The trouble is, when "?epik=3xFD_XXs" is added to a URL, it causes me to lose ads on my website!
So, I would really like to find a solution.
I'm presuming that the solution would be to redirect them to the original URL, http://www.example.com/article/, whenever ?epik=Random_char is found.
Thank you.
This RewriteCond is to check whether the request query string contains epik=. If it contains the query string epik=, the RewriteRule redirect is then applied. The ? character is used to remove query string if any.
RewriteCond %{QUERY_STRING} epik=
RewriteRule ^ %{REQUEST_URI}? [R,L]

htaccess query string redirect not working correctly

I have a situation where I want to actually see the url variable even though the rest of my htaccess site uses readable URLS.
The issue is that it is simply showing up as a page not found...
This works...
RewriteRule ^files/(.+)/from_all_files/$ pages/file.php?slug=$1&from=all-files
This does not work
RewriteRule ^files/(.+)?from=all-files$ pages/file.php?slug=$1&from=all-files
Im looking for the second one to work.
You cannot check the query string in a RewriteRule, which can only see the REQUEST_URI. You need to use the following instead:
RewriteCond %{QUERY_STRING} ^from=all-files$ [NC]
RewriteRule ^files/(.+)$ pages/file.php?slug=$1 [QSA,L]
When you request http://example.com/files/some-file?from=all-files, the request will be internally rewritten to pages/file.php?slug=some-file&from=all-files. The Query String Append flag (QSA) will append the current query string to the one you're rewriting to.

Htaccess URL redirect by matching url string and parameters

I need to match the URL format and depends on URL matches need to redirect the incoming requests to different pages.
For example
http://www.domain.com/path1/path2/ wrong-url -1/ ?var1=val1
http://www.domain.com/path1/path2/ another-wrong-url -1/ ?var1=val1
http://www.domain.com/path1/path2/ third-wrong-url -1/?var1=val1
http://www.domain.com/path1/path2/ fourth-wrong-url -1/?var1=val1
See the High lighted URL Matches. It always having -1 as the url string. . That needs to be redirected one static page.
And some other URL's always have var1 as URL Query String parameter. So if URL have var1 as Query string then those URL's needs to be redirected to another Static page.
So i tried this but didn't worked. Please help me in this redirecting script
RewriteEngine on
RewriteRule ^(path1/path2/[^-1]*)$ http://www.domain.com/target-page [L,R=301]
1. Rule for URL that ends with -1/:
RewriteRule ^path1/path2/([^/]+)-1/$ http://www.domain.com/target-page [L,R=301]
2. Rule for having var1= parameter in query string:
RewriteCond %{QUERY_STRING} (^|&)var1=([^&]*)(&|$)
RewriteRule .* http://www.domain.com/another-target-page [L,R=301]
NOTE:
With these rules existing query string will be passed to a new URL as well (e.g. /path1/path2/wrong-url-1/?say=meow will become http://www.domain.com/target-page?say=meow). To drop it, add ? at the end of target URL (e.g. http://www.domain.com/another-target-page? [L,R=301]

htaccess rewrite urls

I have a link http://mywebsite.com/?view=3457373673863568
everything after the view= will change depending on who gets it.
how can I redirect them to another page on the site i.e. http://mywebsite.com/mypage
something like:
RewriteRule http://mywebsite.com/?view=(?) http://mywebsite.com/mypage/
mod_rewrite parses the URL and the rules only see the path, not the query string. The query string is stored in the variable QUERY_STRING, which needs to be matched separately.
RewriteCond %{QUERY_STRING} ^view=
RewriteRule ^$ /mypage [L]
This matches the empty path (i.e., / on your site) and any query string that starts with view=. The query string will be passed implicitly to the target page.
If you want an external redirect so the user browser shows /mypage, use the [R] flag (change [L] to [L,R]).

Resources