How to do rewrite rule for PayPal IPN (htaccess)? - php

Been at this for some hours now.
I need to redirect
http://www.mywebsite.com/index.php?option=com_acctexp&task=paypal_subscriptionnotification&Itemid=
to
http://www.mywebsite.com/dir/lib/ipn.php
So far I have tried all kind of combinations like these:
RewriteCond %{QUERY_STRING} ^option=com_acctexp&task=paypal_subscriptionnotification&Itemid=$
RewriteRule ^(.*)$ /dir/lib/ipn.php [L]
[L] has also been replaced with [R=301,L] with little success
I am somewhat stumbling in the dark here as I do not know entirely what I am doing. I have read the Apache documentation but it is cryptic for me. I do not wish to keep the query string, however I would like to retain the json data sent with the ipn.

The $ at the end of your pattern means that it has to be an exact match - the querystring must end on the =, appending an itemid value will cause the match to fail. To pass the request through, you don't want to do a 301 redirect, that will result in a new request. To drop the querystring and prevent a loop, add a ? to the end of your rewrite.
RewriteBase /
RewriteCond %{QUERY_STRING} ^option=com_acctexp&task=paypal_subscriptionnotification&Itemid=([^&]+?)
RewriteRule (.*) /dir/lib/ipn.php? [L]
Test on http://htaccess.madewithlove.be/
Input: http://www.example.com?option=com_acctexp&task=paypal_subscriptionnotification&Itemid=123
1 RewriteBase /
2 RewriteCond %{QUERY_STRING} ^option=com_acctexp&task=paypal_subscriptionnotification&Itemid=([^&]+?)
This condition was met
3 RewriteRule (.*) /dir/lib/ipn.php? [L]
This rule was met, the new url is http://www.example.com/dir/lib/ipn.php
The tests are stopped because the L in your RewriteRule options
Output: http://www.example.com/dir/lib/ipn.php

Related

How can I create a 301 redirect to remote domain without question marks (?) and other unimportant text in the request?

I am wanting to start from http://fubar.com/subpage.php?pageId=99
And end on another domain http://newdomain.org/fubar/99
Here's what I have at the top of my .htaccess
RewriteEngine On
RewriteBase /
RewriteCond %{THE_REQUEST} /subpage.php\?pageId=(.*)$
RewriteRule ^ http://newdomain.org/fubar/?%2 [R=301,L,NE]
This is what I am getting.
from
http://fubar.com/subpage.php?pageId=99
to
http://newdomain.org/fubar/?pageId=99
I do not understand the ? at the end of the RewriteRule in ?%2. I hoped to remove it but ended up with just http://newdomain.org/fubar/
Bonus question, how can things be reset after testing something like this? I keep having to switch browsers and incognito modes when I break things.
THE_REQUEST variable represents original request received by Apache from your browser and it doesn't get overwritten after execution of other rewrite directives. Example value of this variable is GET /index.php?id=123 HTTP/1.1. That means your regex to capture query parameter pageId is not correct besides %2 will always be empty since you are capturing only one value.
You may use this rule:
RewriteEngine On
RewriteBase /
RewriteCond %{THE_REQUEST} /subpage\.php\?pageId=([^&\s]*) [NC]
RewriteRule ^ http://newdomain.org/fubar/%1? [R=301,L,NE]
? in the end will strip off previous query string which gets forwarded automatically to new URL.

Redirect rule to php query but remove any query string in the requested url

My url is being redirected using .htaccess as follows:
RewriteRule ^b/([^/]+)/([^/]+)? b/view.php?id=$2&name=$1
Friendly url -> translates to php url
domain.com/b/hello/2 -> b/view.php?id=2&name=hello
BUT when someone comes to the site as follows:
domain.com/b/hello/2?query=xyz
I don't know how to get rid of the ?query=xyz
I have tried everything including [QSD] and I can't seem to get it to work.
Update
I have managed to get it to work with the following but it does two 301 redirects instead of one:
RewriteCond %{THE_REQUEST} \?[^\ ]+
RewriteRule ^b/(.*)$ /x/$1? [R=301,L]
RewriteRule ^b/([^/]+)/([^/]+)/([^/]+)?$ b/view.php?id=$2&&name=$1
Check if adding the question mark at the end of the rule will cancel appending query string from left side
RewriteRule ^b/([^/]+)/([^/]+)? b/view.php?id=$2&name=$1? [QSD,L]
You can use an additional Rule to catch for GET parameters and strip them off.
RewriteCond %{QUERY_STRING} .+
RewriteRule ^(.*)$ /$1? [R=301,L]
RewriteRule ^b/([^/]+)/([^/]+)? b/view.php?id=$2&name=$1
To make it only work on the /b/ subfolder, use this:
RewriteCond %{QUERY_STRING} .+
RewriteRule ^b/(.*)$ b/$1? [R=301,L]
RewriteRule ^b/([^/]+)/([^/]+)? b/view.php?id=$2&name=$1
The first rule will redirect everything that matches your rule to the URL without any GET parameters (note the ? at the end of the rewrite rule, it will strip off the parameters).
The second rule will match in the case the first rule cannot be applied, i.e., when there are no paramaters

The .htaccess redirect with parameters $1 and $2 to form URL

I'm trying to complete the following rule in my .htaccess.
I have this .htaccess in root directory of domain1.com and want to arrange the following redirect domain1.com/index.php?domain2.com?page.html which will bring all visitors to domain2.com/page.html.
RewriteEngine On
RewriteCond %{HTTP_HOST} ^
RewriteRule (.+)\.php\?(.+)\?(.+) http://$2/$3 [R=301,L]
Solved.
domain1.com/domain2.com/page.html
RewriteEngine On
RewriteCond %{HTTP_HOST} ^
RewriteRule (.+)\/(.+) http://$1/$2 [R=301,L]
Instead of constructing your initial URLs like domain1.com/index.php?domain2.com?page.html - which requires you to capture two separate backreferences (the second ? will be URL encoded). It would be better to format your URL more conventionally, like the following instead:
domain1.com.com/index.php?redirect=domain2.com/page.html
(Not sure why you were substituting a ? for the first slash in the URL being passed?)
Then you could do something like the following. Note that you need to use a condition that checks against the QUERY_STRING server variable.
RewriteCond %{QUERY_STRING} ^redirect=([^&]+)
RewriteRule ^index\.php$ http://%1 [QSD,R,L]
%1 is a backreference to the last matched CondPattern.
The QSD flag is required to discard the original query string from the request.
HOWEVER, you need to perform additional validation on the domains that can be redirected to, otherwise this will likely be abused for malicious intent once it is discovered.
UPDATE:
RewriteCond %{HTTP_HOST} ^
RewriteRule (.+)\/(.+) http://$1/$2 [R=301,L]
The RewriteCond directive here isn't doing anything. If you are wanting to validate that a Host header is present (ie. is not a HTTP/1.0 request) then you should change the regex ^ to . (simply a dot). However, if you are on a shared server then this test is probably redundant.
By using the URl-path, this will "break" if the destination hostname is the same as the source.

htaccess mod_rewrite rule for one parameter

how can I write something like this:
RewriteCond %{QUERY_STRING} s=(\w+)
RewriteRule ^ wordpress/?s=%1 [L]
I need do rewrite rule from domain.com?s=something to domain.com/wordpress/?s=something.
Folder with wordpress is by symlink but It is not interesting.
Rule what I was send works but makes error 500 on other URLs like domain.com/something?company=1
Thanks
Btw: I must send this "body" twice for successful validation. Wtf #stackoverflow? And why are you cut greeting?
You just need this single rule:
RewriteEngine On
RewriteRule ^/?$ wordpress/ [L]
Original query string is automatically copied over.

Unable to remove query strings from URL using .htaccess

My URLs look like http://example.com/?n=x. I want the URL to show as http://example.com/. I have used to approaches so far and none of them works.
First one is based on this question:
RewriteEngine On
RewriteCond %{QUERY_STRING} ^n=1$
RewriteRule (.*) $1? [R=permanent]
After the answer below I modified the .htaccess file:
```RewriteEngine On
RewriteCond %{QUERY_STRING} ^n=(.*)$
RewriteRule (.*) $1? [R=permanent]```
but it still did not work. Here is the debugging info:
RewriteCond %{QUERY_STRING} ^n=(.*)$
This condition was met.
RewriteRule (.*) $1? [R=permanent]
The new url is http://example.com/blog/cat/??
Test are stopped, a redirect will be made with status code permanent
Second approach is:
RewriteEngine On
RewriteCond %{QUERY_STRING} !^$
RewriteRule ^(.*)$ $1 [QSD]
None of them seem to work.
I have a few more questions:
Does rewriting only work if the URL is not typed manually?
Can I still access the values of query strings in PHP?
What happens when a user copies the rewritten URL?
The first approach is correct if you go to http://example.com/?n=1, if I am correct you should change ^n=1$ to ^n=(.*)$.
And the other questions:
It works for all kind. It doesn't matter if it was a robot or a human writing it, when you access a page, .htaccess will be read.
Yes you can. Use $_SERVER["QUERY_STRING"]. If you use the htaccess redirection before explained, you will lose them because you are redirecting.
What do you mean ?

Categories