Home > Software engineering >  .htaccess PHP Parameter Friendly URL
.htaccess PHP Parameter Friendly URL

Time:11-29

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

CodePudding user response:

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>

CodePudding user response:

You can use this for the first part:

RewriteRule ^store/((?!store)[^/] )/([^/] )$ /store/store.php?page=$1&id=$2 [L]

CodePudding user response:

Although nothing is wrong with anyone else's answers, the more modern way to do this (including WordPress, Symfony and Laravel) is to send non-existent URLs to a single router script. By doing this, you only have to mess with an htaccess file once to set things up, and never touch it again if you add more "sub-folders", you can do all of that in just PHP. This is also more portable which means you can bring it to other server platforms such as Nginx with little changes, and don't need to deal with RegEx.

The htaccess is fairly straightforward. Route all requests that start with /store/ and don't exist as a file (such as images, JS and CSS) or directory to a single new file called router.php in your /store/ folder. This is an internal redirect, which means it isn't a 301 or 302.

RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^store/ /store/router.php [L]

Then in your new router.php file you can parse $_SERVER['REQUEST_URI'] to determine the URL that was actually requested, and you can even rebuild the global $_GET variable:

// Parse the originally requested URL into parts
$requestUrlParts = parse_url($_SERVER['REQUEST_URI']);

// Parse the query string into parts, erase the old global _GET array
parse_str($requestUrlParts['query'], $_GET);

// Handle 
switch($requestUrlParts['path']){
    case '/store/store.php';
        include '/store/store.php';
        exit;
        
    // Custom 404 logic here
    default:
        http_response_code(404);
        echo 'The page you are looking for cannot be found';
        exit;
}

I'd also recommend putting the htaccess rule into the site root's htaccess folder, above WordPress's. There's nothing wrong with creating multiple files, this just keeps things in a central place and makes it easier (IMHO) to debug.

  • Related