My URL is:
https://example.com/detail.php?id=56&subcat=11
I need like this:
https://example.com/detail/56/11
Using .htaccess file i am not able to remove the parameter names (id, subcat) and question mark(?). I have removed only .PHP extension.
Options +FollowSymLinks -MultiViews
# Turn mod_rewrite on
RewriteEngine On
RewriteBase /
## don't touch /forum URIs
RewriteRule ^forums/ - [L,NC]
RewriteCond %{THE_REQUEST} s/+products(?:.php)??id=([0-9]+) [NC]
RewriteRule ^ products/%1? [R,L]
RewriteRule ^products/([0-9]+)/?$ products.php?id=$1 [L,QSA]
## hide .php extension snippet
# To externally redirect /dir/foo.php to /dir/foo
RewriteCond %{THE_REQUEST} s([^.]+).php [NC]
RewriteRule ^ %1 [R,L]
# To internally forward /dir/foo to /dir/foo.php
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^(.+?)/?$ $1.php [L]
Thank you.
3
Answers
This is a bit complex problem. You won’t achive it only by .htaccess. You have to parse variables from url to array and make it accessible from php code. In this link in section ‘Using php’ there is some example how to do this.
Because you don’t want to use “normal” link, we have to handle requests for pages and manually parse them. First define in you htaccess redirect:
Then create
index.php
which will handle redirects.Now in detail.php you can use
$params
and you get array with you values. So ourhttp://example.com/detail/56/1
goes likehttp://example.com/index.php?path=detail/56/1
. With this aproach we will write our logic in details.php and we can use$params
, which equalsArray ( [0] => 56 [1] => 1 )
.Your link could be more readable in code if you change it to
/detail/id/56/subcat/11
. Just add this before check in_arrayThis is only simple example of idea. Better use some framework with that routing, like symphony.
Just place the following into your .htaccess file (and make sure that mod_rewrite is enabled):
you can use following .htaccess sample file
Note : replace filename1 and filename2 with your php file names