.htaccess redirect based on conditions and parameters - regex

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]

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: 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]

Rewrite URL when parameter is missing

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]

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]

Handle query parameters recursively using htaccess

I am here again with the query of re-writing URL :(.
I am using htaccess for re-writing, below is my htaccess code:
RewriteEngine On
RewriteBase /
RewriteCond %{ENV:REDIRECT_STATUS} 200
RewriteRule ^ - [L]
RewriteCond %{REQUEST_URI} !-f
RewriteCond %{REQUEST_URI} !\.\w+$
RewriteCond %{REQUEST_URI} !/$
RewriteRule (.*) /$1/ [R,L]
RewriteCond %{REQUEST_URI} ^/([\w\d-]+)/$ [NC]
RewriteRule ^ /?file_name=%1 [L,QSA]
RewriteCond %{QUERY_STRING} !^$
RewriteCond %{QUERY_STRING} (?:(.*)&)?file_name=([\w\d-]+)(.*) [NC]
RewriteRule ^ %2/?%1%3 [L,R]
Before asking my queries let me give you short explanation about how above code is working. I have a URL like http://www.mysite.com/ (for index) and other pages could be executed by using a parameter called file_name=page_name like http://www.mysite.com/?file_name=my_blog. My htaccess is helping me to exclude the file_name parameter and make a new URL like http://www.mysite.com/my_blog/.
Now I do have a query:
Right now there is already a parameter file_name and now I want to give extra parameters like http://www.mysite.com/?file_name=my_blog&blog_alias=welcome-to-new-generation and wanted this to look like http://www.mysite.com/my_blog/welcome-to-new-generation/. So if I add any number of parameter there parameter name should be removed and only parameter value comes with slash.
Please note: Parameter name could be anything.
Please please please help.
This is a very interesting problem that requires recursive implementation of mod_rewrite rules. Put this code in your .htaccess:
Update II As per further comments, redirects /?file_name=my_blog&blog_alias=welcome-to-new-generation&foo=bar&n=v URI to /file_name+my_blog/blog_alias+welcome-to-new-generation/foo+bar/n+v
Options +FollowSymLinks -MultiViews
# Turn mod_rewrite on
RewriteEngine On
RewriteBase /
# forwards ?file_name=my_blog&blog_alias=welcome-to-new-generation to
# /file_name=my_blog&blog_alias=welcome-to-new-generation
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s/+\?([^\s]+)\s [NC]
RewriteRule ^$ %1? [L]
# redirects /file_name=my_blog&blog_alias=welcome-to-new-generation&foo=bar&n=v
# to /file_name+my_blog/blog_alias+welcome-to-new-generation/fo+barn+/v
RewriteRule ^(.*/)?([^=]+)=([^&]+)&(.*)$ $1$2+$3/$4 [L]
RewriteRule ^(.*/)?([^=]+)=(.*)$ $1$2+$3 [L,R]
# internal forward from /file_name+my_blog/blog_alias+welcome-to-new-generation to
# /?file_name=my_blog&blog_alias=welcome-to-new-generation
RewriteRule ^([^\+]+)\+([^/]+)/(.*)$ $3?$1=$2 [L,QSA]
RewriteRule ^([^\+]+)\+([^/]+)/?$ /?$1=$2 [L,QSA]
Once you're sure it is working change R to R=301