views:

181

answers:

3

I have this .htaccess file, which is not working, when I navegate to gotgage.local/category/gages/ the gages portion of this is not being passed

RewriteEngine On
RewriteRule  ^/category/(.*?)/$ category.php?p=$1 [rn]

For the sake of testing the category.php file is just dumping $_POST, $_GET and $_SERVER this is what I am seeing when I open the page.

GET
Array ( )
POST
Array ( ) 
SERVER
Array (
    [HTTP_USER_AGENT] => Mozilla/5.0 (Windows; U; Windows NT 6.0; en-US) AppleWebKit/525.19 (KHTML, like Gecko) Chrome/1.0.154.59 Safari/525.19
    [HTTP_ACCEPT_ENCODING] => gzip,deflate,bzip2,sdch
    [HTTP_ACCEPT] => text/xml,application/xml,application/xhtml+xml,text/html;q=0.9,text/plain;q=0.8,image/png,*/*;q=0.5
    [HTTP_CACHE_CONTROL] => max-age=0
    [HTTP_ACCEPT_LANGUAGE] => en-US,en
    [HTTP_ACCEPT_CHARSET] => ISO-8859-1,*,utf-8
    [HTTP_HOST] => gotgage.local
    [HTTP_CONNECTION] => Keep-Alive
    [PATH] => /usr/local/bin:/usr/bin:/bin
    [SERVER_SIGNATURE] =>  Apache/2.2.9 (Ubuntu) PHP/5.2.6-2ubuntu4.2 with Suhosin-Patch Server at gotgage.local Port 80


    [SERVER_SOFTWARE] => Apache/2.2.9 (Ubuntu) PHP/5.2.6-2ubuntu4.2 with Suhosin-Patch
    [SERVER_NAME] => gotgage.local
    [SERVER_ADDR] => 192.168.0.180
    [SERVER_PORT] => 80
    [REMOTE_ADDR] => 192.168.0.196
    [DOCUMENT_ROOT] => /home/gotgage/www/
    [SERVER_ADMIN] => [no address given]
    [SCRIPT_FILENAME] => /home/gotgage/www/category.php
    [REMOTE_PORT] => 56793
    [GATEWAY_INTERFACE] => CGI/1.1
    [SERVER_PROTOCOL] => HTTP/1.1
    [REQUEST_METHOD] => GET
    [QUERY_STRING] => 
    [REQUEST_URI] => /category/gages/
    [SCRIPT_NAME] => /category.php
    [PATH_INFO] => /gages/
    [PATH_TRANSLATED] => /home/gotgage/www/gages/
    [PHP_SELF] => /category.php/gages/
    [REQUEST_TIME] => 1241579116
    [argv] => Array
        (
        )

    [argc] => 0 )

Technically the redirect is working however I am not getting the vars the page below that is dumping is the correct from the correct page but my vars are not being passed.

A: 

I'm not totally sure that this is the problem, but putting the flags as "[rn]" is not valid. Multiple flags should be separated with commas, so if you're trying to put both the R and N flags, it should be [R,N]

Chad Birch
This made no difference, as I updated the post to say the redirect is happening but I am not getting the vars
Unkwntech
A: 

There are a couple things that are somewhat unclear:

1) What was the intended match with the regex portion:

(.*?)

That is a little redundant -- assuming that you simply want to match anything between the slashes, the following would suffice:

(.*)

2) You set a flag of 'rn', which doesn't appear to be valid. Example of valid flags are:

[R]
[N]
[R,N]

though, I'm not sure you'd really want to use R and N together.

johnvey
(.*?) is explicitly being non-greedy. In this case its saying get me everything between "category/" and the first "/" character you see after that. If you use the greedy version you recommended (.*) then you could potentially grab too much!Take the following example URL "category/foo/bar/":The non-greedy version would have $1="foo"The greedy version would have $1="foo/bar"Although it _may_ be okay in this case to get away with the greedy version I think the non-greedy is clearer.
Joseph Pecoraro
Ahh. Sorry about the formatting. I didn't know comments did not retain whitespace. I apologize.
Joseph Pecoraro
+1  A: 

Well, the following worked for me. It looks like it didn't like the leading / and the [rn] threw syntax errors for me.

RewriteEngine On
RewriteRule ^category/(.*?)/$ category.php?p=$1

Note: that it should probably have been [R,N] anyways, but again, they are not needed in this case.


RewriteRule Flags Documentation - Shows why you probably didn't want to use R or N, but there may be something we don't know.
RewriteRule Documentation - Seems to indicate no leading / in .htaccess RewriteRules (if there is a RewriteBase). Either way, try the Rule with and without the /, for me it only worked without.

Joseph Pecoraro
I copied and pasted to ensure I didn't typo anything and I am still getting the same problem...
Unkwntech
Try building up to it. Get something working and then slowly make it more complex, word by word to see what is causing your problem.
Joseph Pecoraro