Problem in using htaccess file to control single php file
on my website, to search and compare prices of different things, I've form having 'search' button. When user enter keyword(thing for which he want to compare price), category etc. and click 'search' button, URL in address bar is exactly the same that i want (i.e /sp/searchresults/camera/115/Search/bla/bla/////) using following code:
RewriteEngine on
RewriteRule ^searchresults(.*)$
searchresults.php?keyword=$1&categories=$2&t=$3&btnsearch=$4&manuf=$5&brname=$6&pricerange=$7&h=$8开发者_JAVA百科&k=$9&merchantid=$10&sort=$11 [L]
Now the problem is when i want to have that 'keyword','merchantid' and 'sort' on any other page, it gives me '.php' instead of giving me item name,merchant id and sort result.
Any help will be appreciated
yea your problem is a few,
For starters with ^searchresults(.*)$ you will only get $1 no others as you saying capture everything after searchresults as one (),
The second i'm not sure about is that your have got $11 i'm sure your only allowed 10 but will have to wait for another user to say if that is the case.
Try this
RewriteRule ^searchresults_(.*?)_(.*?)_(.*?)_(.*?)_(.*?)_(.*?)_(.*?)_(.*?)_(.*?)_(.*?)_(.*?)_(.*?)$ searchresults.php?keyword=$1&categories=$2&t=$3&btnsearch=$4&manuf=$5&brname=$6&pricerange=$7&h=$8&k=$9&merchantid=$10&sort=$11 [L]
But still not sure it will work as you have more than 10 $vars
For Comment RewriteRule ^serachresults(.*?)$ search.php [L]
P.S i found another problem your ^searchresults(.*)$ would have acted on the searchresults.php and created a redirect loop
RewriteRule ^(.?)/(.?)/(.?)/(.?)/(.?)/(.?)/(.?)/(.?)/(.*?).searchresults$
searchresults.php?keyword=$1&categories=$2&t=$3&btnsearch=$4&manuf=$5&brname=$6&pricerange=$7&h=$8&k=$9&merchantid=$10&sort=$11 [L]
Best suggestion I would have is to use a HTACCESS rule like the following:
RewriteRule searchresult/(.*)$ searchresults.php?fullparam=$1
Then, within your PHP Script, parse the $_GET['fullparam']
value.
$params = array();
if( isset( $_GET['fullparam'] ) )
$params = explode( '/' , $_GET['fullparam'] );
# You can use any delimiter, '/' or '_' or '-' or whatever you like
OR, using the Parameter Order and Names mentioned in the Original Question
if( isset( $_GET['fullparam'] ) )
list( $keyword , $categories , $t , $btnsearch , $manuf , $brname ,
$pricerange , $h , $k , $merchantid , $sort , )
= explode( '/' , $_GET['fullparam'] , 12 );
The benefit of this solution is that you have the simplest possible HTACCESS rule, and you can use the flexibility of PHP to allow for any number of elements in your URLs.
精彩评论