Rewrite URL when parameter is missing - regex

I'm looking for an Apache Rewrite Condition that fires if a certain URL parameter is not set and then rewrites the URL by adding this parameter.
So basically if my URL looks like http://www.heco.de/index.php?id=123&catId=456, I want it to redirect to http://www.heco.de/index.php?id=123&catId=456&cHasH=456
The first parameter is a static page ID, but the catId varies from a single digit up to 6 digits.
I tried to achieve this by adding this code snippet to my .htaccess:
RewriteEngine On
RewriteBase /
RewriteCond %{QUERY_STRING} catId=([0-9]{1,6})
RewriteCond %{QUERY_STRING} !cHash=
RewriteRule ^(.*)/?id=123&catId=([0-9]{1,6})$ index.php?id=123&catId=$2&cHash=$2
#RewriteRule ^(.*)/?id=123&catId=([0-9]+)$ http://www.stackoverflow.com
Alas, it's not working. I tried testing this by uncommenting the last line, but no redirect takes place at all. Hence I would be really grateful for any insights, ideas or further advice...!
Thx in advance,
Martin

You can't match QUERY_STRING in RewriteRule.
You can use this rule:
RewriteCond %{QUERY_STRING} !(?:^|&)cHash=[^&]+ [NC]
RewriteCond %{QUERY_STRING} (?:^|&)catId=([0-9]{1,6}) [NC]
RewriteRule ^index\.php$ $0?cHash=%1 [L,QSA,NC]

Related

Handle one query parameter specifically

I need to handle one query parameter specifically.
The url can look like the following:
https://example.com/?app=egov&map=1337
https://example.com/?app=egov&map=1337&showoptions=true
https://example.com/?app=egov&map=1337&showoptions=true&anotherparam=helloWorld
The redirect should point to
https://example.com/contextName/resources/apps/egov/index.html?map=1337
https://example.com/contextName/resources/apps/egov/index.html?map=1337&showoptions=true
https://example.com/contextName/resources/apps/egov/index.html?map=1337&anotherparam=helloWorld
As you can see, the app-Parameter needs to be used as an URL-part; the others need to be used like traditional query-params.
I've figured out, how to implement the first part, where https://example.com/?app=egov points to https://example.com/contextName/resources/apps/egov/index.html using the following
RewriteCond %{REQUEST_URI} ^/$
RewriteCond %{QUERY_STRING} ^app\=(.+)
RewriteRule ^/$ "/contextName/resources/apps/%1/index.html" [R,L,QSA]
but I'm struggeling how to realize the correct handling of the other params.
All these combination of query string can be handled by a single rule loke this:
RewriteEngine On
RewriteCond %{QUERY_STRING} ^app=([\w-]+)(?:&(.*))?$ [NC]
RewriteRule ^$ contextName/resources/apps/%1/index.html?%2 [L]
RewriteCond matches app query parameter and captures it in %1 while rest of the query string after & is captured in %2.
With your shown samples, please try following htaccess Rules file. Please make sure to clear your browser cache before testing your URLs.
RewriteEngine ON
####Apache documentation link https://httpd.apache.org/docs/trunk/rewrite/remapping.html for more info.
##Internal rewrite rule for URL https://example.com/?app=egov&map=1337
RewriteCond %{QUERY_STRING} ^app=([^&]*)&map=(\d+)$ [NC]
RewriteRule ^/?$ contextName/resources/apps/%1/index.html?map=%2 [L]
##Internal rewrite rule for URL https://example.com/?app=egov&map=1337&showoptions=true
RewriteCond %{QUERY_STRING} ^app=([^&]*)&map=(\d+)&showoptions=(.*)$ [NC]
RewriteRule ^/?$ contextName/resources/apps/%1/index.html?map=%2&showoptions=%3 [L]
##Internal rewrite rule for https://example.com/?app=egov&map=1337&showoptions=true&anotherparam=helloWorl
RewriteCond %{QUERY_STRING} ^app=([^&]*)&map=(\d+)&showoptions=([^&]*)&anotherparam=(.*)$ [NC]
RewriteRule ^/?$ contextName/resources/apps/%1/index.html?map=%2&anotherparam=%4 [L]
All of your URLs uri part is NULL, so rules are written based on that.

.htaccess redirect based on conditions and parameters

I have tried many answers but none of them seems to work.
WHAT I WANT?
1). I want to redirect this(if it contains the parameters from & to and both of them are not empty):
https://arc.abc.com/mconvertar.php?from=gbp&to=jpy
to
https://arc.abc.com/gbpjpy/
As you can see, we combined the values of from & to
2). And this(if it contains the parameter x along with from & to):
https://arc.abc.com/mconvertar.php?from=gbp&to=jpy&x=13
to
https://arc.abc.com/gbpjpy/x=13
As you can see the 13 is the same as the above url.
3). And if x is empty then:
https://arc.abc.com/mconvertar.php?from=gbp&to=jpy&x=
to
https://arc.abc.com/gbpjpy/x=1
As you can see we set the x value default to 1
WHAT I HAVE TRIED?
RewriteCond %{QUERY_STRING} (^|&)from=[^&\s]&to=[^&\s]&x=[0-9]
RewriteRule ^(.*)$ https://arc.abc.com/gbpjpy/x=%3 [R=301,L]
RewriteCond %{QUERY_STRING} !(^|&)x=($|&)
RewriteRule ^(.*)$ https://arc.abc.com/gbpjpy/x=1 [R=301,L]
RewriteRule ^([^/]*)([^/]*)/([^/]*)\.html$ /mconvertar.php?from=$1&to=$2 [L]
I really appreciate your help, Spent a lot of time
Assuming from parameter is always 3 characters long. You may try these rules in site root .htaccess:
RewriteEngine On
# external redirect from actual URL to pretty one with x=<digit>
RewriteCond %{THE_REQUEST} /mconvertar\.php?from=(\w+)&to=([^\s&]+)&(x=\d+)\s [NC]
RewriteRule ^ /%1%2/%3? [R=302,L]
# external redirect from actual URL to pretty one with x=<empty>
RewriteCond %{THE_REQUEST} /mconvertar\.php?from=(\w+)&to=([^\s&]+)&(x)=\s [NC]
RewriteRule ^ /%1%2/%3=1? [R=302,L]
# internal forward from pretty URL to actual one
RewriteRule ^(\w{3})([^/]+)/(x=\d+)/?$ mconvertar.php?from=$1&to=$2&$3 [L,QSA,NC]

Htaccess: remove query from urls and redirect

I have a Forum on my site and need to redirect weirdly generated urls.
Every url contains ?id=, eg:
https://www.example.com/forum/topic/casualthread/page/25?id=casualthread
and I need to remove the ?id= and everything that follows in order to have:
https://www.example.com/forum/topic/casualthread/page/25
I am trying to modify this code I found here on Stackoverflow with very scarce results:
RewriteEngine On
RewriteBase /
# Make sure there is a query string
RewriteCond %{QUERY_STRING} .
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule .* /?id= [R=301,L]
The htaccess file i am editing is in the forum directory:
https://www.example.com/forum/
and it redirects everything to the homepage https://www.example.com: what am I doing wrong?
You can use this
RewriteEngine on
RewriteCond %{QUERY_STRING} id=
RewriteRule ^ %{REQUEST_URI}? [L,R]
Since not just id, but also some other parameter(s) may be present in the query string, use:
RewriteEngine On
RewriteBase /
RewriteCond %{QUERY_STRING} ^(.*)(^id=[^&]*&?|&id=[^&]*)(.*)$
RewriteRule ^(.+) /$1?%1%3 [R=301,L]

htaccess Rewrite Rules appending parameter?

Our original urls began with a parameter and the following rewrite works to redirect them (thanks to Jon Lin).
However, the original parameter is being appended to redirect, so that
RewriteEngine On
RewriteCond %{QUERY_STRING} ^old-page$
RewriteRule ^$ /newpage [R=301,L]
ends up going to mydomain.com/newpage?old-page
Any idea how to fix? thanks again,
Geoff
Have it like this to strip off existing query string:
RewriteEngine On
RewriteCond %{QUERY_STRING} ^old-page$ [NC]
RewriteRule ^$ /newpage? [R=301,L]
Take note of ? at the end of target URI, that is used to strip-off existing query string.

How to redirect a URL with a specific parameter to a URL without one?

So basically, I am trying to remove a parameter from all URLs within my site.
For example:
http://www.mydomain.com.au/thispage.html?view=form
http://www.mydomain.com.au?view=form
http://www.mydomain.com.au/this-is-a-page.html?view=form
I require all the pages above to go to their version without this parameter, such as:
http://www.mydomain.com.au/thispage.html
http://www.mydomain.com.au
http://www.mydomain.com.au/this-is-a-page.html
The redirect must only occur when view=form, and not when view is equal to anything else.
Very new to regex and apache, so I am not to sure if this is even possible.
So far I have just been using the following code to point them to a 404, although this has been causing a few problems within webmaster tools:
RewriteCond %{HTTP_HOST} ^www\.mydomain\.com\.au [NC]
RewriteCond %{QUERY_STRING} (^|&)view=
RewriteRule ^ - [L,R=404]
Use the following htaccess code
RewriteEngine on
RewriteBase /
RewriteCond %{QUERY_STRING} (^|&)view=form(&|$) [NC]
RewriteRule ^ http://%{HTTP_HOST}%{REQUEST_URI}? [R=301,L]
In case you want to preserve other query parameters (like ?foo1=bar1&view=form&foo2=bar2) use
RewriteCond %{QUERY_STRING} ^(?:(.*)&)?view=form(?:(&.*))?$ [NC]
RewriteRule ^ http://%{HTTP_HOST}%{REQUEST_URI}?$1$2 [R=301,L]