Change the stroke width without changing the width and height of the rectangle
In my opinion, "width of a rectangle" in your question can mean two things, leading to two different answers; it is not clear for me which one you actually ask for. Which one do you want to have constant:
- The visual size of the rectangle, i.e. from the outer line edge left to the outer line edge right.
- The nominal (geometric) size of the rectangle, i.e., from the center of the left line to the center of the right line.
You might rather care for (1) if you do graphic design and for (2) if you do technical drawings. Inkscape by default seems to show (1) as the width of an object (e.g., in the "W:" field in the toolbar). The answers already given here indicate on how to keep this visual size (1) constant.
However, if you are actually interested in (2), Inkscape can also to show this as the width. For this, go to Preferences -> Tools and set "Bounding box to use" to "Geometric bounding box" instead of "Visual bounding box". Now the geometric size (2) will be indicated by Inkscape. You can now change the line width as you like, and the indicated rectangle width is not affected.