Unable to get 10th variable in url string through .htaccess - php

I want to get get sort_by in my php code but its seems to be that this is returning value of variable 1 with digit 0.
RewriteRule ^abc/([^/]+)/([^/]+)/([^/]+)/([^/]+)/([^/]+)/([^/]+)/([^/]+)/([^/]+)/([^/]+)/([^/]+)/?$ search.php?counrty=abc&state=$1&gender=$2&r_city=$3&r_mstatus=$4&r_religion=$5&r_ethnicity=$6&r_cast=$7&r_profession=$8&r_education=$9&sort_by=$10 [NC]

According to Apache documentation, for $N, the value of N can only be 0-9. So trying to get $10 is not going to work. You will likely need to go through two rewrite steps. So, perhaps you use something like this:
RewriteRule ^([^/]+)/([^/]+)/([^/]+)/([^/]+)/([^/]+)/([^/]+)/([^/]+)/([^/]+)/([^/]+/[^/]+/[^/]+)/?$ search.php?country=$1&state=$2&gender=$3&r_city=$4&r_mstatus=$5&r_religion=$6&r_ethnicity=$7&r_cast=$8&r_profession=$8&profession_and_education_and_sort=$9 [NC]
RewriteCond %{REQUEST_FILENAME} ^search\.php$
RewriteCond %{QUERY_STRING} ^(country=.*&state=.*&gender=.*&r_city=.*&r_mstatus=.*&r_religion=.*&r_ethnicity=.*&r_cast=.*)&profession_and_education_and_sort=([^/]+)/([^/]+)/([^/]+)
RewriteCond ^search\.php$ search.php?%1&r_profession=%2&r_education=%3&sort_by=%4 [NC,L]

Related

Rewrite Dynamic URL In PHP Using .htaccess

I wanted to rewrite url in following pattern
My current url is:
xyz.com/news_details.php?id=120&title=hello-world
and I want the url as is:
xyz.com/hello-world
also I wanted to fetch the values of id on this page like:
$id = $_REQUEST['id'];
please help
I did use the following code:
RewriteCond %{THE_REQUEST} /news_details.php\?id=([^&\s]) [NC]
RewriteRule ^ /%2\.html? [NC,R,L,NE]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^-])\.html$ /news_details.php?id=$2 [QSA,L,NC]
You could do something like this :
RewriteEngine On
RewriteRule ^/(.*)/(.*)$ /news_details.php?title=$1&id=$2
To achieve links like http://example.net/hello-world/120.
The ^/(.*)/(.*)$ part tells apache the pattern that it will follow. The second part (news_details.php?title=$1&id=$2) gives apache the real link that it will apply the pattern on.
You can't exactly hide arguments from the user, because you need a way to get the id that you want to have as the id argument. So that's the only possible way to do that.
You can read more about it here.

PHP - URL with multiple parameters and same name causing unexpected behaviour

I have a Rewrite Rule as
RewriteRule settings/([a-zA-Z0-9_]+)/?$ settings/?path=$1 [NC,L,QSA]
It basically takes path parameter and rewrite url like this
http://localhost/settings/?path=abc => http://localhost/settings/abc
But there's a problem! when I provide something like http://localhost/settings/abc/?path=xyz the path parameter is overwritten with xyz and it opens http://localhost/settings/abc/ with data of xyz
After further digging I noticed that the problem is caused by the parameters in URL itself. By default its picking up the value of last parameter with same name http://localhost/settings/account/?path=profile&path=account&path=profile
so the value that i can get in this case for $_REQUEST['path'] is profile. How can I just pickup the first value from the same parameters and ignore the rest of them?
One thing I can do is to remove [QSA] from the RewriteRule, that will do the fix but I need other parameters passed in url as well.
How can I achieve this?
PHP recognizes brackets [] appended to a parameter, e.g.
http://localhost/settings/account/?path[]=profile&path[]=account&path[]=profile
will be translated into an array
$path = $_GET['path'];
print_r($path);
shows
Array ( [0] => profile [1] => account [2] => profile )
This way, you can access any of the "paths" you like.
I could imagine some RewriteCond surgery, in order to remove part of a query string
RewriteCond %{QUERY_STRING} ^(.*)&path=.*?&(.*)$
RewriteRule ^ %{REQUEST_URI}?%1&%2
RewriteCond %{QUERY_STRING} ^path=.*?&(.*)$
RewriteRule ^ %{REQUEST_URI}?%1
RewriteCond %{QUERY_STRING} ^(.*)&path=.*$
RewriteRule ^ %{REQUEST_URI}?%1
Depending on where the path is, this would remove it from the query string. And finally, the last rule would add the new path again
RewriteRule settings/([a-zA-Z0-9_]+)/?$ settings/?path=$1 [NC,L,QSA]
Although, I don't know, if this is working at all, and how (in)efficient this is.
There is one caveat, you must check for REDIRECT_STATUS to prevent an endless loop.
You can use these 2 rules in your .htaccess:
# remove path= parameter from query string, if it exists
RewriteCond %{THE_REQUEST} \?(.*&)?path=[^&]*(?:&(.*))?$ [NC]
RewriteRule ^settings/. %{REQUEST_URI}?%1%2 [L,R=301,NE,NC]
# your existing rule
RewriteCond %{THE_REQUEST} \?(.*&)?path=[^&]*(?:&(\S*))? [NC]
RewriteRule ^settings/ %{REQUEST_URI}?%1%2 [L,R=302,NE,NC]
RewriteCond %{QUERY_STRING} !(?:^|&)path= [NC]
RewriteRule ^(settings)/(\w*)/?$ $1/?path=$2 [NC,L,QSA]

internal server error with htaccess rewrite rule

I'm trying to take a URL with multiple querystring parameters into a folder structure URL and am getting a 500 internal server error.
My incoming URL looks like
www.fivestarprofessional.com/ag?PY=16&PF=wm&MKT=Delaware
My destination URL I need written as
delaware.fivestarprofessional.com/16/wm/index.html
I created 3 RewriteCond statements to capture the parameters and a RewriteRule to create the output
RewriteCond %{QUERY_STRING} !^(PY=.*)$ [NC]
RewriteCond %{QUERY_STRING} !^(PF=.*)$ [NC]
RewriteCond %{QUERY_STRING} !^(MKT=.*)$ [NC]
RewriteRule %3.fivestarprofessional.com/%1/%2/index.html
Any assistance would be greatly appreciated.
You can use this rule for your redirection:
RewriteCond %{QUERY_STRING} (?:^|&)PY=([^&]+) [NC]
RewriteCond %1::%{QUERY_STRING} (.*?)::(?:|.*&)PF=([^&]+) [NC]
RewriteCond %1/%2::%{QUERY_STRING} (.*?)::(?:|.*&)MKT=([^&]+)
RewriteRule ^ag/?$ http://%2.fivestarprofessional.com/%1/index.html? [L,NC,NE,R=302]
% variables are captured only from the most recent condition using same %{QUERY_STRING}
Try this one:
RewriteCond "%{QUERY_STRING}" "^PY=([^&]+).*&PF=([^&]+).*&MKT=([^&]+)" [NC]
RewriteRule "www.fivestarprofessional.com/ag" "%3.fivestarprofessional.com/%1/%2/index.html"
The following issues have been corrected:
RewriteRule takes 3 arguments instead of 2.
Arguments to RewriteCond hadn't been enclosed in quotes.
The backreferences into the RewriteCond rules ...
... only refer to the last condition matched (pointed out by #anubhava, being documented in the apache httpd docs). Therefore, the rules need to be collapsed in one.
... referred to the parameter name plus the complete remainder of the query string
... would have matched against query string starting with the respective ul parameter due to the use of the ^ anchor.
... would have been empty anyway, as the rules fired when the patterns did not match ( use of ! prefix )
Recommended reading is the proper section of the apache httpd documentation.
The solution as it stands assumes that...
the query string starts with the url parameter PY
the order in which the url parameters PY, PF, MKT appear in the query string is fixed.

Apache rewrite for query string value not equal to a digit

I'm trying to determine the correct syntax for an Apache rewrite to send visitors to a different page if a query string value does not equal a certain format.
URL 1: http://www.foo-bar.xqx/?video=123 (route one way)
URL 2: http://www.foo-bar.xqx/?video=fgh (route differently)
The format for the value should be all digits \d{0,x} but nothing I've tried so far seems to be returning as I'm expecting. The most current version for the rewrites is as follows:
RewriteCond %{QUERY_STRING} ^video=[^\d](\d{0,})$
RewriteRule - /404.php$1 [NC,L]
Use ! to negate a rule
RewriteCond %{QUERY_STRING} ^video=
RewriteCond %{QUERY_STRING} !^video=\d+$
RewriteRule - /404.php$1 [NC,L]
That will only rewrite requests that have ?video=, but only if there's no number after the =.

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

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

Categories