Search code examples
php.htaccessurl-rewriting

.htaccess PHP Parameter Friendly URL


I would like to make the URLs of my Store URL-friendly.

Current URL Structure

https://my-domain.com/store/store.php?page=packages&id=1

Desired URL Structure

https://my-domain.com/store/packages/1

And also for direct access to the PHP files such as:

https://my-domain.com/store/profile.php to https://my-domain.com/store/profile

How would I need to go after this? I really appreciate any help you can provide.

Also might be note worthy that in the base directory a WordPress site is running with its own .htaccess file.

I already tried it with this

Options +FollowSymLinks
RewriteEngine on
RewriteRule ^store/store/page/(.*)/id/(.*) /store/store.php?page=$1&id=$2
RewriteRule ^store/store/page/(.*)/id/(.*)/ /store/store.php?page=$1&id=$2

But that didn't work


Solution

  • This code will work.

    RewriteEngine will remove .php from all PHP Files RewriteRule will rewrite url like page/id

    For Removing .php extension

    RewriteEngine On
    RewriteCond %{THE_REQUEST} /([^.]+)\.php [NC]
    RewriteRule ^ /%1 [NC,L,R]
    RewriteCond %{REQUEST_FILENAME}.php -f
    RewriteRule ^ %{REQUEST_URI}.php [NC,L]
    
    

    For page/id

    
    <IfModule mod_rewrite.c>
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule ^([a-zA-Z0-9]+)/([a-zA-Z0-9]+)? store.php?page=$1&id=$2 [L] 
    </IfModule>