Question

I know this has been asked a couple of times, but I've been wraking my brain about this for hours now and I just can't seem to figure it out.

So I have a url: http://example.com/product.php?id=123 which I'd like to rewrite to http://example.com/product/123

I have this code:

RewriteRule ^product/([^/\.]+)/?$ product.php?id=$1 

RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f 
RewriteRule ^([A-Za-z-\s0-9]+)$ /product.php?id=$1 [L]

And it makes the http://example.com/product/123 url work, but the images are not loaded, I guess because the php tries to find them in the /product directory, and also if I enter the orignal url it doesn't get rewritten.

So can you help me how to solve these?

Any help is much appreciated!

Était-ce utile?

La solution

Put this code in your DOCUMENT_ROOT/.htaccess file:

RewriteEngine On
RewriteBase /

RewriteCond %{THE_REQUEST} \s/+product\.php\?id=([^\s&]+) [NC]
RewriteRule ^ product/%1? [R=301,L]

RewriteCond %{REQUEST_FILENAME} -d [OR]
RewriteCond %{REQUEST_FILENAME} -f 
RewriteRule ^ - [L]

RewriteRule ^product/([^/.]+)/?$ product.php?id=$1 [L,QSA]

RewriteRule ^([A-Za-z\s0-9-]+)/?$ /product.php?id=$1 [L,QSA]

For problems with css/js/images use absolute path in your css, js, images files rather than a relative one. Which means you have to make sure path of these files start either with http:// or a slash /.

You can try adding this in your page's HTML header: <base href="/" />

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top