So this is really stumping me....Ive used this same method many times before without error, but its not working now.... Im simply using htaccess to rewrite URLs and using PHP to $_GET the variables... Problem is, when I echo out the $_GET variable its echoing the word "index" instead of the word in the URL My htaccess: RewriteEngine on RewriteRule ^(.*)/(.*)\.php$ index.php?Category=$1&SubCat=$2 [L] RewriteRule ^(.*)\.php$ index.php?Category=$1 [L] My PHP file: $CategoryGet = $_GET['Category']; $SubCatGet = $_GET['SubCat']; echo $CategoryGet; Now, if I go to http://mydomain.com/Jerseys.php the echo outputs "index" Now, if i go http://mydomain.com/Jerseys/Youth.php the echo again, outputs "index" This also effects it even when I use index.php?Categoy=Jerseys When I remove the lines from the htaccess, it magically starts working again.... What the hell is going on.. INTERESTING UPDATE ------ When I change the following in my htaccess it works... RewriteRule ^(.*)\.html$ index.php?Category=$1 [L] Changing the destination URL from PHP to HTML fixes it.... However, I want them to .php... Another update.... I tried changing it to the following: RewriteRule ^(.*)\.php4$ index.php?Category=$1 [L] That works as well, for whatever reason, it seems that this thing is retarded and cant handle a plain .php
It's not retarded in the slightest. You're telling it to rewrite all requests for .php files to another .php file (the index.php file). The request for index.php?Category=originalcat will in turn be rewritten again to index.php?Category=index You can either put the rewrite code into your httpd.conf and the [L] flag will stop processing after the first rewrite (as .htaccess are only interpreted perdir, Apache has to make another request for the new location in case any more rewrites apply). Or add in a RewriteCond to check your filename against already being the index script. I.e. RewriteCond %{SCRIPT_FILENAME} !index\.php$ RewriteRule ^(.*)/(.*)\.php$ index.php?Category=$1&SubCat=$2 [L] RewriteCond %{SCRIPT_FILENAME} !index\.php$ RewriteRule ^(.*)\.php$ index.php?Category=$1 [L]
rodney88, what your saying makes sense, however I have multiple other sites using the code posted above in some variation that works flawlessly... Also, from looking at your rewritecode its telling the code if the filename is not index.php then run this code. Now what happens when the file IS index.php Edit**** It works, thank you!
As you said, a RewriteCond tells the rewrite engine to only apply the following RewriteRule if the specified condition is true. In this case, if the filename is not already index.php. When the RewriteCond is not met (i.e. it is index.php), the RewriteRule does not apply so in this case nothing happens. I'm not sure how you can have the same code working on other sites without encountering this problem - unless the script name which processes the requests does not also match the regex pattern, (i.e. by using different file extensions). Glad to hear its working now.
Well, come to think of it, the one of the others uses .html extension and another puts some other junk in front of it...
Now im having a new problem.... I have links for each product on the page, that go to a cart.php file, everytime I click one, it keeps redirecting back to the index.php, I know the htaccess is the issue because I removed the lines and it works...
".php" doesn't work because you used the rule "(.*)" which includes "index" --> It'll cause an endless loop.
Is there anyway I can get around that? I want to keep all of my pages extensions as php, I really dont want to have some pages as html, or php4, or whatever, and then some have normal .php Edit - Fine fine fine, ive decided to use the following as my permanent solution... RewriteEngine on RewriteRule ^Gear/(.*)/(.*)\.php$ index.php?Category=$1&SubCat=$2 [L] RewriteRule ^Gear/(.*)\.php$ index.php?Category=$1 [L] Prefix the URL with Gear to alleviate the issue.. Thank you to all that helped