Apache rewrite rule percent symbol - facebook - apache

I am having this issue with my apache and rewrite rules.
My orignal url is something like this:
urbana.com.uy/core.php?m=amp&nw=MTQ2NA==
When I post it in facebook, the URL change (facebook do it) to this:
urbana.com.uy/xcore/?m=nw&nw=MTQ2NA%3D%3D
So, facebook convert the = symbol to %3D
Well, from here no problem.
BUT, I have a rewrite rule in my server that rewrite an URL that doesnt start with www to www.blabla
This are the rules:
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}$1 [R=301,L]
The work but with a problem.
When a user clic in a URL from facebook, with the %3D instead of =, my apache change that for a %25, like this
www.urbana.com.uy/xcore/?m=nw&nw=MTQ2NA%253D%253D
and this doesnt work
How can I tell my apache to not change the % symbol to %25 and redirect all the non www no www?
Thanks

I think the only reliable answer to your problem is to not send non-url safe values to as the value of a URL query. I would suggest using some form of url encode on your parameter before you send it to facebook, and then a url decode on the other side. In PHP you would generate these like so:
<?php
$parameter_value = url_encode('MTQ2NA==');
$facebook_url = "http://urbana.com.uy/core.php?m=amp&nw=" . $parameter_value;
?>
Then on your server, when you read the value, just perform a URL decode
<?php
$parameter_value = url_decode($_GET['nw']);
?>
Other languages will have similar tools to perform this. I realize its not exactly what you're asking for, but the "=" sign is not a url safe character, and sending it in a parameter is going to cause issues even if you can get the rewriting working.

Related

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

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.

mod_rewrite to redirect url not working

Cannot seem to get a mod_rewrite to work. We have a domain name that has already been printed here, there and everywhere when the website was Flash. It has a # in its trail /#login.php and we want so that when people put this in it redirects them to /login.php. I have already tried this rule but can't get it to work:
RewriteEngine On
RewriteRule ^/#login.php$ /login.php
I have also checked that the rewrite engine is working by using a redirect to google. Just need the out of date #login.php to go to the new login.php
thanks
The # in the URL (or "fragment") is not sent to the server, it's purely for the client side to point to some part of the page. If you see http://hostname.com/#login.php in your address bar, the only thing the server gets is a request for /. You may need to employ some javascript on the page to look at the browser's address bar to find a fragment and maybe send that to the server as a query string.
Try :
RewriteEngine On
RewriteBase /
RewriteRule ^#login\.php$ /login.php [QSA,L]
Mod_rewrite is enabled ? available ?

Redirect specific URLs to an alternate page

I have a few URLs that are now 404ing due to products expiring, and I need to redirect them to a related page that does exist.
e.g.
http://www.example.com/package-product.php?id=72 to http://www.example.com/best-sellers/
http://www.example.com/package-product.php?id=36 to http://www.example.com/bedroom-furniture/
etc.
As I understand it, I can't do this with a Redirect 301 because of the GET param, and the examples I've seen for rewrite seem to be a bit more generic and use a placeholder for the params - I need to send a specific URL to a specific alternative.
Thanks.
I'd probably do it the way #BobLiu suggested - i.e. to do it in PHP itself.
If that's not possible if you really want a rewrite solution, you can look at the QUERY_STRING variable available to mod_rewrite:
RewriteCond %{QUERY_STRING} id=72$
RewriteRule ^.*$ http://www.example.com/best-sellers/ [R=301,L]
Why not just say on the package-product.php page something like:
switch ($_GET['id'])
{
case 72:
[do redirect url1];
case 36:
[do redirect url2];
etc...etc..
}

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.