Ready to stab .htaccess in its fucking face... Help would be appreciated...

Status
Not open for further replies.

PlexXoniC

New member
Mar 17, 2008
26
0
0
Ok, I've got an .htaccess file setup like so:

Code:
<IfModule mod_rewrite.c>
Options +FollowSymlinks
RewriteEngine on

#THIS SECTION WORKS
#query
RewriteRule ^find/([^/]+) /list-products.php?q=$1 [NC]

#THIS SECTION WORKS
#category
RewriteRule ^find/c/([^/]+) /list-category.php?c=$1 [NC]

#THIS POS DOESNT FUCKING WORK I'M READY TO SHOOT THE DAMN SERVER
#compare sku
RewriteRule ^compare/sku/([^/]+) /compare.php?sku=$1 [NC]

#THIS WORKS FINE AS WELL
#buy sku
RewriteRule ^buy/sku/([^/]+) /buy.php?sku=$1 [NC]

#404
ErrorDocument 404 /index.php

</IfModule>
Maybe its just from staring at it for too long but I can't figure out why the compare rewrite rules aren't working, my querystring isn't being passed to the page it redirects to but on all the other ones it works fine... It's probably some stupid period or some random character I'm not seeing but any ideas as to why just that one section isn't working would be great.

Thanks
 


This may sound crazy, but try putting the compare line above the other rules.

Also I usually put a trailing / on the end of the rewritten URLs. so that compare/sku/392902/ would work (and looks better that way, as opposed to looking like a file without an extension). Not sure if that would help either. What's confusing is each line looks identical to the last cept with a different word.

Also make sure your find one works. I've found that if using rules with similar beginnings, you want to put the longer one on top, so that the shorter one doesn't automatically meet the criteria. (kinda like if you have /g/(.*)/(.*) below /g/(.*)/ well the shorter one will always be true when it comes first)
 
This may sound crazy, but try putting the compare line above the other rules.

Tried that already but TY though it doesn't make sense why it's not working considering the line below it works fine and its the exact same line except for the swap of the words "buy" and "compare"....
 
Tried that already but TY though it doesn't make sense why it's not working considering the line below it works fine and its the exact same line except for the swap of the words "buy" and "compare"....
I noticed something different between yours and mine though.

Code:
RewriteEngine On
Options +FollowSymlinks
RewriteBase /

ErrorDocument 404 /index.php?page=404

RewriteRule ^-(.*)/(.*)/(.*)/ /index.php?page=$1&xtra=$2&xtrab=$3 [L]
RewriteRule ^-(.*)/(.*)/(.*) /index.php?page=$1&xtra=$2&xtrab=$3 [L]
RewriteRule ^-(.*)/(.*)/ /index.php?page=$1&xtra=$2 [L]
RewriteRule ^-(.*)/ /index.php?page=$1 [L]
RewriteRule ^show/(.*)/(.*).png /blank.gif [L]

RewriteCond %{REQUEST_FILENAME} !-f 
RewriteCond %{REQUEST_URI} \.(gif|jpg|jpeg|png)$ 
RewriteRule .* /shares/notfound.gif [L]

I don't use [NC] after each, but rather [L], which denotes last-rule, meaning if the conditions are met, it doesn't continue onto the next one.
 
I noticed something different between yours and mine though.

Code:
RewriteEngine On
Options +FollowSymlinks
RewriteBase /

ErrorDocument 404 /index.php?page=404

RewriteRule ^-(.*)/(.*)/(.*)/ /index.php?page=$1&xtra=$2&xtrab=$3 [L]
RewriteRule ^-(.*)/(.*)/(.*) /index.php?page=$1&xtra=$2&xtrab=$3 [L]
RewriteRule ^-(.*)/(.*)/ /index.php?page=$1&xtra=$2 [L]
RewriteRule ^-(.*)/ /index.php?page=$1 [L]
RewriteRule ^show/(.*)/(.*).png /blank.gif [L]

RewriteCond %{REQUEST_FILENAME} !-f 
RewriteCond %{REQUEST_URI} \.(gif|jpg|jpeg|png)$ 
RewriteRule .* /shares/notfound.gif [L]
I don't use [NC] after each, but rather [L], which denotes last-rule, meaning if the conditions are met, it doesn't continue onto the next one.


I've got the [NC] there to ignore case but i'll change it to [NC,L] and see if that works
 
where does it go when you try compare, just a 404 page?

It goes to the correct page but the querystr vars are not being passed. On the other pages, the querystr vars are being passed... makes no sense...

Just in case, do you see anything wrong with this, It's the same code I use on all the other pages and they work fine hell, it's the same code I use on every php site I pass query vars with but just in case lol


Code:
//querystr vars
if ( isset ($_GET["sku"]) ) {
    $sku = $_GET["sku"];
} elseif ( isset ($_GET['upc']) ) {
    $upc = $_GET["upc"];
} elseif ( isset ($_GET["isbn"]) ) {
    $isbn =  $_GET["isbn"];
} else {
    //none are set go back to main page
}

I've also tried just pulling it with
Code:
$sku = $_GET["sku"];
and still nothing.
 
Try this for debugging purposes

Code:
[COLOR=#990000]echo[/COLOR] [COLOR=#000088]$_SERVER[/COLOR][COLOR=#009900][[/COLOR]'REQUEST_URI'[COLOR=#009900]][/COLOR];
just to see the entire uri being passed.

Tried that earlier, its:

/compare/sku/2184548

Which for example is

/find/xbox+260+controller

on the find redirect.
 
Ok, compare that to $_SERVER['QUERY_STRING']; since that'll be the string being passed to the script itself.

That is empty on both the find page and the compare page but the find page works?

Code:
//post vars
if ( isset ($_GET['q']) ) {
    $query = $_GET["q"];
} else {
    $query = '';
}

That returns what ever I passed to it via /find/keywords

But the compare page has nothing passed to it in the query strings, I've already looped through them to see if anything at all was being passed but no luck.
 
Odd it shouldn't be empty $_SERVER["QUERY_STRING"] should return "q=whatever" especially if $_GET['q'] is actually returning a result.

:P guess you could always create php.php with <? phpinfo(); ?> in it, and make the destination to php.php in the htaccess so that you can get a full readout of everything you passed to it.

Also have you tried (.*) instead of ([^/]+) ? not sure if it would make a big difference unless there's something bout the structure of the sku number that throws it off. Then again the htacces should be fine, if it's still reaching it's destination (but as you say for some reason it's not passing sku).
 
Odd it shouldn't be empty $_SERVER["QUERY_STRING"] should return "q=whatever" especially if $_GET['q'] is actually returning a result.

:P guess you could always create php.php with <? phpinfo(); ?> in it, and make the destination to php.php in the htaccess so that you can get a full readout of everything you passed to it.

Also have you tried (.*) instead of ([^/]+) ?

Oops, it does return a value on the find page but not the compare page, and I just tried the (.*) and same thing.
 
By the way I added your htaccess rules to mine. But directed it to my php.php file so I could see what was being passed.

phpinfo()

Seems to pass sku just fine.

This is my whole htaccess and as you can see i'm not using the <ifmodule> and such.

Code:
RewriteEngine On
Options +FollowSymlinks
RewriteBase /

ErrorDocument 404 /index.php?page=404

RewriteRule ^-(.*)/(.*)/(.*)/ /index.php?page=$1&xtra=$2&xtrab=$3 [L]
RewriteRule ^-(.*)/(.*)/(.*) /index.php?page=$1&xtra=$2&xtrab=$3 [L]
RewriteRule ^-(.*)/(.*)/ /index.php?page=$1&xtra=$2 [L]
RewriteRule ^-(.*)/ /index.php?page=$1 [L]

RewriteCond %{REQUEST_FILENAME} !-f 
RewriteCond %{REQUEST_URI} \.(gif|jpg|jpeg|png)$ 
RewriteRule .* /shares/notfound.gif [L]

#THIS SECTION WORKS
#query
RewriteRule ^find/([^/]+) /php.php?q=$1 [L]

#THIS SECTION WORKS
#category
RewriteRule ^find/c/([^/]+) /php.php?c=$1 [L]

#THIS POS DOESNT FUCKING WORK I'M READY TO SHOOT THE DAMN SERVER
#compare sku
RewriteRule ^compare/sku/([^/]+) /php.php?sku=$1 [L]

#THIS WORKS FINE AS WELL
#buy sku
RewriteRule ^buy/sku/([^/]+) /php.php?sku=$1 [L]

So based on this, the htaccess is sound, and it's something on the php side not collecting the value correctly.
 
Maybe I am a bit off here, but I would suggest another approach:

Code:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^.+$ - [S=40,L]
RewriteRule ^/*([^/]+/+)*([^.]*[^/])$ http://%{HTTP_HOST}%{REQUEST_URI}/ [L,R=301]
RewriteRule . index.php [L,NE,QSA]
This cakes care of nearly everything. The last line is important:

Code:
RewriteRule . index.php [L,NE,QSA]
Why not to redirect *all* incoming requests (which are not already processed by previous htaccess rules, like existing dirs/files, slash at the end of URL etc.) to PHP and let it take care of it:

Code:
$segments = explode('/', $_SERVER['REQUEST_URI']);
Then just take necessary action based on what the $segments array looks like (its first member could be the "controller", the second one "method", the other ones "parameters" etc.) I find it much more easier and efficient than putting all the rules into .htaccess. Also, in cooperation with regular expressions, this could be a very efficient and flexible way of routing *any* incoming URL to passing "action"
 
Status
Not open for further replies.