skip to Main Content

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


  1. 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:

    RewriteEngine on
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule ^(.*)$ index.php?path=$1 [NC,L,QSA]
    

    Then create index.php which will handle redirects.

    // your available sites
    $sites = [ 'detail' ];
    
    $path = $_GET['path'];
    $params = explode( "/", $path );
    $site = array_shift($params);
    
    if( in_array( $site, $sites ) ){
        include($site.".php");
    }
    

    Now in detail.php you can use $params and you get array with you values. So our http://example.com/detail/56/1 goes like http://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 equals Array ( [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_array

    $x = [];
    for( $i=0; $i<count($params); $i+=2 )
        $x[$params[$i]] = $params[$i+1];
    $params = $x;
    

    This is only simple example of idea. Better use some framework with that routing, like symphony.

    Login or Signup to reply.
  2. Just place the following into your .htaccess file (and make sure that mod_rewrite is enabled):

    RewriteEngine on
    RewriteCond %{SCRIPT_FILENAME} !-d
    RewriteRule ^([^.]+)$ $1.php [NC,L]
    RewriteRule ^index.php/([^/]+)/?([^/]*) /index.php?id=$1&subcat=$2 [NC]
    
    Login or Signup to reply.
  3. you can use following .htaccess sample file
    Note : replace filename1 and filename2 with your php file names

    RewriteEngine On
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule ^(filename1.*)$ filename1.php?path=$1 [NC,L,QSA]
    RewriteRule ^(filename2.*)$ filename2.php?path=$1 [NC,L,QSA]
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search