Rewrite URL's with variable query string placement with .htaccess - regex

I'm trying to rewrite a few different URL's so they redirect to different places, however they aren't static URL's.
I need to redirect:
/index.php?app=core&section=register
to for example:
htttp://www.mysite.com/register.php
However, what I want to do is just make sure that /index.php?app=core&section=register starts with /index.php? but there may be more parameters included and also want to make sure it's still redirected if they are in a different position, so basically it just needs to match it if app=core and section=register exists in the URL.
Lastly, if I wanted to add a second one such as..
/index.php?app=core&section=login to htttp://www.mysite.com/login.php how would I add a second one?

Try this rule:
RewriteCond %{QUERY_STRING} (?:^|&)app=core(?:&|$) [NC]
RewriteCond %{QUERY_STRING} (?:^|&)section=(register|login)(?:&|$) [NC]
RewriteRule ^index\.php$ /%1.php? [NC,L,R=302]
As per comments:
RewriteCond %{QUERY_STRING} (?:^|&)app=core(?:&|$) [NC]
RewriteCond %{QUERY_STRING} (?:^|&)section=(somepath)(?:&|$) [NC]
RewriteRule ^index\.php$ /mypath.php? [NC,L,R=302]

Related

.htaccess redirect with attributes

I am trying to make a .htaccess redirect rule to achieve the following
https://www.example.net/index.php?a=in&u=testUser&api_key=4r98f4r98f&ip_address=127.0.0.1
TO
https://www.example.net/api.php?ip=127.0.0.1&username=testUser
but i can't make it work, any hint?
RewriteEngine on
RewriteCond %{QUERY_STRING} ^a=([^&]+)&u=([^&]+)&api_key=([^&]+)&api_key=([^&]+)&ip_address=([^&]+)$ [NC]
RewriteRule ^index\.php$ https://www.example.net/api.php?ip=%5&username=%2? [NC,L,R]
You have api_key parameter twice in your condition, moreover don't capture a value that you don't need to reuse in the target. You also have an extra misplaced ? at the end.
Have your rule like this:
RewriteCond %{QUERY_STRING} ^a=[^&]*&u=([^&]+)&api_key=[^&]*&ip_address=([^&]+)$ [NC]
RewriteRule ^index\.php$ /api.php?ip=%2&username=%1 [NC,L,NE,R=302]

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.

301 Redirect to URL with query strings

I'm trying to redirect one URL to another URL. They are different domains, but both have query strings in their URLs. The sample URLs are:
Redirect:
olddomain/faculty/profile?uID=123
to:
newdomain/Faculty?id=024
This is what I've placed in the .htaccess file and it does not work (all modules necessary are installed and activated as there are other 301's without query strings that work without issue):
RewriteCond %{HTTP_HOST} ^www\.olddomain\.tld$ [NC]
RewriteCond %{QUERY_STRING} ^uID=123$ [NC]
RewriteRule ^faculty/profile$ http://newdomain/Faculty?id=024 [R=301,NE,NC,L]
What am I doing wrong?
You need to add http:// also:
RewriteCond %{HTTP_HOST} ^www\.olddomain\.tld$ [NC]
RewriteCond %{QUERY_STRING} ^uID=123$ [NC]
RewriteRule ^faculty/profile$ http://newdomain/Faculty?id=024 [R=301,NE,NC,L]
And make sure this rule is placed at top just below RewriteEngine On.

Replacing search query strings to make it SEF

I tried the below .htaccess rewrite to internally rewrite the urls and make the overall urls SEF.
RewriteCond %{QUERY_STRING} ^searchword=(.*)&(.*)$ [NC]
RewriteRule ^searchword=(.*)&(.*)$ searchword=$1 [L]
My url is of the following form.
http://www.domain.com/?searchword=search+term&Search=&searchphrase=all&limit=50&ordering=newest&view=search&option=com_search
I want to make it of the form
http://www.domain.com/?searchword=search+term
You can use this rule:
RewriteCond %{QUERY_STRING} ^(searchword=[^&]+)$ [NC]
RewriteRule ^ /?%1&Search=&searchphrase=all&limit=50&ordering=newest&view=search&option=com_search [L,NE]
The best way to do this is by adding a router.php file to your component. Take a look at http://docs.joomla.org/Supporting_SEF_URLs_in_your_component

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]