I've been trying to work my head around this but my .htaccess powers just aren't as good as I thought. I have this sample query string:
http://mycoolsite.com/store.php?a=apples&type=fresh&b=banana
Is it possible to do this using .htaccess:
Check if type=fresh exists. If not then redirect to page index.php
If type=fresh exists, remove it but retain the rest of the query string
You can use mod_rewrite to match against the query string. In your .htaccess:
RewriteEngine On
RewriteCond %{QUERY_STRING} ^(.*)type=fresh&?(.*)$
RewriteRule ^(.*)$ /$1?%1%2 [L,R]
This will make it so if someone enters http://mycoolsite.com/store.php?a=apples&type=fresh&b=banana in the browser, their browser will get redirected to http://mycoolsite.com/store.php?a=apples&b=banana . If you want the redirect to happen internally (so the browser's location bar doesn't change), remove the ,R in the brackets at the end of the RewriteRule.
Related
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]
I used this rule, but nothing happen when visiting pages starting with that string.
RewriteEngine on
RewriteRule ^index.php?controller=allproducts(.*)$ / [R=301,L]
What I'm doing wrong?
Query strings aren't considered to be a part of the URI, instead you need to use a condition search for the query string itself:
RewriteEngine on
RewriteCond %{QUERY_STRING} ^controller=allproducts$
RewriteRule (.*) $1? [R,L]
This would redirect http://website.com/index.php?controller=allproducts to http://website.com/index.php
Or if you're on Apache 2.4 you can use the [QSD] flag which discards the query. You would need to setup backreferences if you wanted to keep part of the query or use an absolute URL on the substitution if you wan't everything to go to the homepage.
I have a few url that have appeared in google search console with "?" and under scores "_"in the URL. I have tried many ways to redirect them but i have failed. I believe it has some thing to do with using the %{QUERY_STRING}
this is the URL that i need redirecting from
repairs-blog?journal_blog_tag=iPhone
to
blog?journal_blog_tag=iPhone
Maybe someone could write the solution?
When a query string is involved, you use RewriteCond with %{QUERY_STRING}, e.g.
RewriteCond %{QUERY_STRING} journal_blog_tag=
and then rewrite with RewriteRule
RewriteRule ^repairs-blog$ /blog [L]
Because you don't change the query string, there's nothing more to do.
When you want to rewrite this URL, no matter what query string is given, you can also omit the RewriteCond and just use the RewriteRule.
If you want to redirect instead of rewrite, e.g. change the URL in the client, you add the R flag
RewriteRule ^repairs-blog$ /blog [R,L]
I'm trying to use .htaccess but i'm a bit lost at this point. I was wondering how would you do
a rewriting for multiple pages.
RewriteRule ^your-order/$ /page1.php,page2.php,page3.php [L]
or should i just do this:
RewriteRule ^your-order/$ /page1.php [L]
RewriteRule ^your-order/$ /page2.php [L]
RewriteRule ^your-order/$ /page3.php [L]
also i was wondering if rewriterule would still execute if the page has a parameter:
URL: page1.php?test=hello
RewriteRule ^your-order/$ /page1.php [L]
I am assuming from your question about the query string params that you actually have the concept of the rewrites backward. The first expression is the submitted URL (pageN.php) and the second one is where it should be redirected or rewritten (your-order). In that case, you need only one rule.
Unless you need to take special action if a query string parameter is present (like go to a different page entirely), you don't need to match the query string. Query strings are matched in separate RewriteCond conditions rather than in the RewriteRule.
RewriteEngine On
# Rewrite page1, page2, page3 to your-order
# Add as many other pages as necessary separated by |
# The QSA appends any additional querystring to your-order
RewriteRule ^(page1|page2|page3)\.php your-order [L,QSA]
If your pages are actually named with the number at the end (which I doubt), you could use this expression instead:
RewriteRule ^page[0-9]+\.php your-order [L,QSA]
In either case, if you want the end user's browser to be redirected to the your-order URL, rather than an internal and invisible rewrite, change [L,QSA] to [L,R,QSA]
I'm trying to come up with some mod_rewrite to translate http://example.com/?7gudznrxdnu into http://example.com/view.php?id=7gudznrxdnu
But any other page will function properly such as http://example.com/contact and so on.
I think this will work:
RewriteEngine On
RewriteCond %{QUERY_STRING} ^[a-z0-9]+$
RewriteRule ^$ view.php?id=%{QUERY_STRING} [L]
If you want the rewrite to be shown in the browser's address field, you'll have to replace [L] with [L,R=301].
Explanation: The query-string (what's following the question mark) is not part of the URL that RewriteRule sees in its matching-pattern, therefore you can't check for question mark there. In my solution, I run the rule if and only if (RewriteCond) the query string consists solely of a-z and/or 0-9, and my rule only rewrites URLs ending with a slash (except for the query string). I redirect this to view.php?id=, and then append the query string to that.
Edit: Tested on my Apache-server, and I haven't found any bugs (yet).
You should try (in your .htaccess):
RewriteEngine On
RewriteRule ^\?([^/\.]+)?$ view.php?id=$1 [L]