Rewrite url of php page using .htaccess

39 views Asked by At

I have one url like this

http://localhost/project/product_detail.php?pro_id=4&&pro_name=D.H._Tiffin_with_B.B._Packing

I want to rewrite url like this

http://localhost/project/product_detail/4/D.H._Tiffin_with_B.B._Packing

Also I had changes done in xamp\apache\conf\httpd.conf.

LoadModule rewrite_module modules/mod_rewrite.so
AllowOverride All

I tried .htaccess code follow:

RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^project/([a-zA-Z0-9-]+)$ project/product_detail.php?pro_id=1&&pro_name=$1 [L,QSA]

I tried above code, but not work.

1

There are 1 answers

0
arkascha On

The rewriting rule you yourself posted will never get applied to the requested URLs you cite, since the pattern won't match.

Here is something that should do what you ask:

RewriteEngine on
RewriteRule ^/?project/product_detail/(\d+)/(.+)$ /project/product_detail.php?pro_id=$1&&pro_name=$2 [END]

That rule should work likewise in the http servers host configuration or in a dynamic configuration file (".htaccess" style file). If you need to use a dynamic file then take care that the interpretation of such files is enabled and that above rule is placed in such a file located in the http hosts DOCUMENT_ROOT folder.

And a general remark: you should always prefer to place such rules in the http servers host configuration instead of using dynamic configuration files (".htaccess"). Those dynamic configuration files add complexity, are often a cause of unexpected behavior, hard to debug and they really slow down the http server. They are only provided as a last option for situations where you do not have access to the real http servers host configuration (read: really cheap service providers) or for applications insisting on writing their own rules (which is an obvious security nightmare).