Redirect dynamic url with single non-numeric parameter to static extension-less root level url with name consisting of parameter value text

Viewed 39

I have an .htaccess that allows me to use .php files urls without extension:

    # if not a directory and .php file is present then add .php extension
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{DOCUMENT_ROOT}/$1.php -f
    RewriteRule ^(.+?)/?$ $1.php [L]

I am trying to create a controller page.php file that will allow me to generate multiple dynamic pages depending on the parameter value, such as:

https://www.example.com/page.php?pid=some-page-slug-1

https://www.example.com/page.php?pid=some-page-slug-2

and so on.

I would like to turn the above dynamic links to static in the next fashion:

https://www.example.com/page.php?pid=some-page-slug-1

turns to:

https://www.example.com/some-page-slug-1

I've tried to modify some template I found on internet but its not working:

RewriteRule (.*)\.php$ page.php?pid=$1

What is the correct markup to rewrite dynamic url with single non-numeric parameter to static extension-less root level url with name consisting of parameter value text in a SEO friendly way?

1 Answers

You may try these rules:

RewriteEngine On

# if not a directory and .php file is present then add .php extension
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{DOCUMENT_ROOT}/$1.php -f
RewriteRule ^(.+?)/?$ $1.php [L]

# singe parameter static URL handlinng
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([\w-]+)/?$ page.php?pid=$1 [L,QSA]
Related