Jump to content

Query issue in file with .htaccess entry


son

Recommended Posts

I have at top of page:

	if (isset($_GET['id'])) 	{	$id = $_GET['id'];	}		else 	{	$id = 'normal';	}$getQuery = "SELECT styleName FROM mmstyles WHERE styleID = '$id'";

I have also an entry within .htaccess as:

Options +FollowSymlinksRewriteEngine OnRewriteRule ^([^/]+)\/index.php styles/index.php?id=$1 [NC]

Now my query does not work as it looks for 'styles' instead of 'normal'. I have use this sort of coding before, only that then I had all in root folder. Now, it has to go in separate folder and as this is the differentiating factor I wonder if the folder name requires a change in .htaccss that I am not aware of. Can you see where I am going wrong?Thanks,Son

Link to comment
Share on other sites

I think the problem is that after each rewrite, then all of the rewriting rules are by default applied again to the re-written URL, so I think what's happening is something like this:Someone visits mystyle/index.phpThis matches your rewrite rule, and is rewritten to styles/index.php?id=mystyleBUT, this also matches your rewrite rule, and so gets rewritten again to styles/index.php?id=stylesWhich then matches again, but I'd imagine that at this point then the web server sees that it would rewrite to the same URL and so stops to avoid an infinite loop.There's a couple of options:1. Change the rule so that it won't match if ?id is already present. The $ sign means "end of input", so the rule will only match if index.php is the very last thing in the URL:

RewriteRule ^([^/]+)\/index.php$ styles/index.php?id=$1 [NC]

2. Prevent multiple rewrites from happening, so that as soon as one re-write has occured, rewriting will stop. You do this by adding the L (for Last) option to your rewrite flags:

RewriteRule ^([^/]+)\/index.php styles/index.php?id=$1 [NC,L]

Link to comment
Share on other sites

  • 2 weeks later...
I think the problem is that after each rewrite, then all of the rewriting rules are by default applied again to the re-written URL, so I think what's happening is something like this:Someone visits mystyle/index.phpThis matches your rewrite rule, and is rewritten to styles/index.php?id=mystyleBUT, this also matches your rewrite rule, and so gets rewritten again to styles/index.php?id=stylesWhich then matches again, but I'd imagine that at this point then the web server sees that it would rewrite to the same URL and so stops to avoid an infinite loop.There's a couple of options:1. Change the rule so that it won't match if ?id is already present. The $ sign means "end of input", so the rule will only match if index.php is the very last thing in the URL:
RewriteRule ^([^/]+)\/index.php$ styles/index.php?id=$1 [NC]

2. Prevent multiple rewrites from happening, so that as soon as one re-write has occured, rewriting will stop. You do this by adding the L (for Last) option to your rewrite flags:

RewriteRule ^([^/]+)\/index.php styles/index.php?id=$1 [NC,L]

thanks trevelluk,option 1 does the rewrite ok (option 2 causes an internal server error, so I refrain from using it;-)). Although this did this trick, it showed me that there is another issue now with the rewrite. I run a query as:SELECT styleName FROM mmstyles WHERE styleID = '$id'instead of running this as:SELECT styleName FROM mmstyles WHERE styleID = 'normal' for example it runs it now as SELECT styleName FROM mmstyles WHERE styleID = 'styles'Is there a remedy for this? Although I need 'neat' urls I also need to run my queries ok...Son
Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...