URL Rewriting using .htaccess

software development


A have a very strange problem.

When I do this in .htaccess:

RewriteEngine on
RewriteRule ^products/([0-9])/$ http://mydomain.com/script.php?lid=$1 [R]

with the [R] flag to change the url in the user’s browser I have no problem with the php script executing as expected.

However, when I do this:

RewriteRule ^products/([0-9])/$ http://mydomain.com/script.php?lid=$1

(without the [R]) so that the user doesn’t see the rewrite, the script.php runs but only my images require absolute links (my includes still work with relative links).

All I am doing is not using the [R] flag which shouldn’t change the directory paths. With the [R] flag it works fine.

thanks in advance


Looks like you forgot the RewrtieBase directive.

eg, if the .htaccess file is in /home/username/example.com, and the URL is http://example.com/ then you need

RewriteBase /or if the .htaccess file is in /home/username/example.com/subdir, and the URL is http://example.com/subdir/, then you need

:cool: Perl / MySQL / HTML CSS


RewriteBase is only needed if you’re not using complete paths, eg:

RewriteBase /dir RewriteRule foo.html foo.php When using complete paths as the original poster is, you don’t need it, as the path is already part of the rule.

Also, to the OP, you don’t need the scheme or hostname if you aren’t doing external redirects:

RewriteRule ^products/([0-9])/$ /script.php?lid=$1 Are you writing your images paths relative to the root directory or the re-written pseudo-directory? Remember, the browser is going to resolve those URLs relative to where it thinks it is, in this case, /products/1/. So if your images are in /images, you’ll need to use …/…/…/images. If you want them to work both with and without the URL re-writing, use absolute paths.

If you want useful replies, ask smart questions.