How to redirect page with query-string to external webpage using APACHE RewriteRule - apache

I am trying to redirect a login page to an external security service. This service, after validating the credentials, will then return user back to the originating page using the referrer url, as in the following example:
http://{IP NUMBER}/MyWiki/index.php?title=Special:UserLogin&returnto=Main_Page
or any call to a page in the site containing Special:UserLogin in the query_string needs to be redirected to:
https://login.security.server.com/test/UI/Login?service=DSSEC&goto=http://{IP NUMBER}/MyWiki/index.php/Special:UserLogin
I have been testing with RewriteCond and RewriteRule without any luck.

You want something like this?
RewriteEngine On
RewriteCond %{REQUEST_URI} Special:UserLogin [OR]
RewriteCond %{QUERY_STRING} Special:UserLogin
RewriteCond ?#%{QUERY_STRING} ([^#]+)#([^#]+)
RewriteRule ^ https://login.security.server.com/test/UI/Login?service=DSSEC&goto=http://%{SERVER_ADDR}%{REQUEST_URI}%1%2 [L,B,NE]
Ok, this is going to seem a little confusing, but here's what's going on.
Check if Special:UserLogin is in the request URI or the query string.
Create backreference matches for the ? mark, the URI and the query string (this is very important)
Redirect the request to https://login.security.server.com/test/UI/Login, but using the back references from the previous condition to build the goto= param, and using the B flag, which URL encodes the backreferences. This way, the result is an entire URL, along with query string, that's been URL encoded. (The NE flag is there to make sure the % signs themselves don't get double encoded).
With these rules, a request for:
/MyWiki/index.php?title=Special:UserLogin&returnto=Main_Page
Will get redirected to:
https://login.security.server.com/test/UI/Login?service=DSSEC&goto=http://123.45.67.89/MyWiki/index.php%3ftitle%3dSpecial%3aUserLogin%26returnto%3dMain_Page
As you can see, the query string ?title=Special:UserLogin&returnto=Main_Page gets encoded into %3ftitle%3dSpecial%3aUserLogin%26returnto%3dMain_Page, so that the login.security.server.com doesn't mistake it for its own query string. Instead, their login service will see the goto parameter as:
http://123.45.67.89/MyWiki/index.php?title=Special:UserLogin&returnto=Main_Page
entirely intact.

Related

How to redirect a url to home page if the input value has "!"?

If any user submit the ! character as input value I want to redirect the URL to the home page.
example.com/index.php?c%5B%5D=%21&c%5B%5D=&c%5B%5D=&c%5B%5D=&c%5B%5D=&ic=&submit=find
If multiple input values are submitted with the character ! I want to be redirected to the main page else the code works on the same page.
For example, in this URL:
https://www.example.com/currencyconverter/convert/?Amount=!&From=USD&To=EUR
the value ! is contained so I want to redirect it to my page example.com. However, if the URL is like this:
https://www.example.com/currencyconverter/convert/?Amount=!3&From=USD&To=EUR
in this URL I submit input value !3 so I don't want to redirect it. The only thing I want is if the URL contains only ! value it is redirected to home page else code works normally. How can I do that?
If any URL parameter contains exactly ! only (URL encoded or not) then redirect to the document root. You can do this using mod_rewrite near the top of the root .htaccess file:
RewriteEngine On
RewriteCond %{QUERY_STRING} \=(!|%21)(&|$)
RewriteRule ^ / [QSD,R=302,L]
UPDATE: The regex \=(!|\%21)(&|$) matches any URL parameter value where the value is either ! or %21 (! URL encoded) exactly. Whilst the = is not a special regex meta character in this context, it needs to be backslash-escaped to avoid being interpreted as the = Apache mod_rewrite prefix-operator. Alternatively, you could match a single character before the literal =, eg. .=.
The QSD flag is necessary to discard the query string from the redirect response.
Note that this rule currently applies to any URL-path. If it should only apply to /currencyconverter/convert/ (for instance) then be specific in the rule. For example:
:
RewriteRule ^currencyconverter/convert/$ / [QSD,R=302,L]
NB: The URL-path matched by the RewriteRule pattern does not start with a slash.
Aside:
But why not block the request with a 403 Forbidden, rather than issue a redirect? ie. use the following RewriteRule instead:
:
RewriteRule ^ - [F]

htaccess url redirect with get parameters ID and reduce value

I want to do an url redirect to a new domain by retrieving the ID parameter but only taking the first 4 characters. Anyone know how to do this?
For example, an original url:
http://www.original.example/see/news/actualite.php?newsId=be9e836&newsTitle="blablabla"
To :
https://www.new.example/actualites/be9e
I have tested :
RewriteCond %{QUERY_STRING} ^newsId=(.*)$ [NC]
RewriteRule ^$ https://www.new.example/actualites/%1? [NC,L,R]
RewriteCond %{QUERY_STRING} ^newsId=(.*)$ [NC]
RewriteRule ^$ https://www.new.example/actualites/%1? [NC,L,R]
There are a couple of problems with this:
The regex ^$ in the RewriteRule pattern only matches the document root. The URL in your example is /see/news/actualite.php - so this rule will never match (and the conditions are never processed).
The regex ^newsId=(.*)$ is capturing everything after newsId=, including any additional URL parameters. You only need the first 4 characters of this particular URL param.
As an aside, your existing condition is dependent on newsId being the first URL parameter. Maybe this is always the case, maybe not. But it is relatively trivial to check for this URL parameter, regardless of order.
Also, do you need a case-insensitive match? Or is it always newsId as stated in your example. Only use the NC flag if this is necessary, not as a default.
Try the following instead:
RewriteCond %{QUERY_STRING} (?:^|&)newsId=([^&]{4})
RewriteRule ^see/news/actualite\.php$ https://www.new.example/actualites/%1 [QSD,R,L]
The %1 backreference now contains just the first 4 characters of the newsId URL parameter value (ie. non & characters), as denoted by the regex ([^&]{4}).
The QSD flag (Apache 2.4) discards the original query string from teh redirect response. No need to append the substitution string with ? (an empty query string), as would have been required in earlier versions of Apache.
UPDATE:
I have an anchor link (#) which is added at the end of the link, is there a possibility of deleting it to make a clean link? Example, currently I have: https://www.new.example/news/4565/#title Ideally : https://www.new.example/news/4565
The "problem" here is that the browser manages the "fragment identifier" (fragid) (ie. the "anchor link (#)") and preserves this through the redirect. In other words, the browser re-appends the fragid to the redirect response from the server. The fragid is never sent to the server, so we cannot detect this server side prior to issuing the HTTP redirect.
The only thing we can do is to append an empty fragid (ie. a trailing #) in the hope that the browser discards the original fragment. Unfortunately, you will likely end up with a trailing # on your redirected URLs (browser dependent).
For example (simplified):
:
RewriteRule .... https://example.com/# [R=301,NE,L]
Note that you will need the NE flag here to prevent Apache from URL-encoding the # in the redirect response.
Like I say above, browsers might handle this differently.
Further reading:
URL Fragment and 302 redirects
redirect is keeping hash
How to clear fragment identifier on 302 redirect?

Rewrite an url get parameter name

I would like to rewrite one parameter in my url and at the same time direct the traffic to another URL. The url should be called with an external program, so it does not actually need to redirect the user.
Original URL:
http://URL1/?parameter1=1234&monkey=12345
Should go into (changing URL1 > URL2 and monkey > abe:
http://URL2/?parameter1=1234&abe=12345
Searched alot of for mod_rewrite examples, but didn't find anything that rewrites the parameter name it self (not the value) and at the same time redirects to a different url.
With mod_rewrite and whithout a client redir you cannot change the host.
If you try the following, you can change the query string:
RewriteCond %{QUERY_STRING} ^parameter1=([0-9]+)&monkey=([0-9]+)$
RewriteRule / /?parameter1=%1&abe=%2
But, in order to change the host, you must do a client redir:
RewriteCond %{QUERY_STRING} ^parameter1=([0-9]+)&monkey=([0-9]+)$
RewriteRule / http://URL2/?parameter1=%1&abe=%2 [R]
Another option without client redir could be using a proxy; this way URL1's apache server, would proxy requests to URL2's web server and return the response to the client

Remove query string from redirected URL with htaccess

I'm using the following code to redirect traffic to a spesific page (this traffic is coming via google from an old site which used to use my servers ip)
RewriteRule ^viewtopic.php?/?$ http://www.myurl.org.uk/ [L,R=301]
As I understand it this ^viewtopic.php?/?$ should strip away the query string but it isn't working. Any help appreciated.
Example URL
http://www.myurl.org.uk/viewtopic.php?f=3&t=44207&start=2265
Output when redirected
http://www.myurl.org.uk/?f=3&t=44207&start=2265
You were close to the answer... You have the ? on the wrong side. Put it on the redirect side to strip off the query string:
RewriteRule ^viewtopic.php http://www.myurl.org.uk/? [L,R=301]
In a 301 redirect, mod_rewrite will normally append the full query string. But placing a ? at the end of your rewritten URL without a corresponding [QSA] ("querystring append") flag will instruct it instead to use the blank query string you supplied.

Redirect URL using Query String parameter in URL

I have a bunch of URLs from an old site that I recently updated to EE2. These URLs look like this:
http://www.example.com/old/path.asp?dir=Folder_Name
These URLs will need to redirect to:
http://www.example.com/new_path/folders/folder_name
Not all Folder_Name strings match up with folder_name URL segments, so these will most likely need to be static redirects.
I tried the following Rule for a particular folder called "Example_One" which maps to a page on the new site called "example1":
Redirect 301 /old/path.asp?dir=Example_One
http://www.example.com/new_path/folders/example1
But this doesn't work. Instead of redirecting I just get a 404 error telling me that http://www.example.com/old/path.asp?dir=Example_One cannot be found.
EDIT:
There's a secondary problem here too which may or may not be related: I have a catch-all rule that looks like this:
redirect 301 /old/path.asp http://www.example.com/new_path
Using the rule, requests like the first one above will be redirected to:
http://www.example.com/new_path?dir=Folder_Namewhich triggers a 404 error.
Just had to scour Google a bit more to find the proper syntax for mod_rewrite.
Using the example from above:
RewriteCond %{QUERY_STRING} ^dir=Example_One$
RewriteRule ^/old/path\.asp$ /new_path/folders/example1? [R=301,L]
This fixes both of the problems above -- as to why EE is 404ing with one parameter in the Query String, that problem is still unsolved, but this works as a workaround to that problem.
You can also redirect URLs to a specific page where the parameter may have a different value each time. One example of this is Google UTM Campaign tracking (in situations like this where the tracking query string triggers a 404):
Link: http://www.example.com/?utm_source=xxx&..... (triggers 404)
Should Redirect to: http://www.example.com
RewriteCond %{QUERY_STRING} ^utm_source=
RewriteRule ^$ http://www.example.com? [R=301,L]
Note: This will only redirect those requests to the homepage, as defined by ^$. If you want to redirect utm requests to a different page, you'll need to change the first part of that RewriteRule.