Skip to content
Advertisement

How should I write the rule so that it looks like this?

I’m trying to figure out how to convert url. It leads to the index page.

the constant I define define("URL_PAGE", "page.php?p=");

link: <a class="menu-link" href="'.BASE_URL.URL_PAGE.$row1['page_slug'].'">';

.htaccess RewriteRule ^page/(.*)$/?$ page.php?p=$1 [NC,L]

result: http://localhost/aione/page.php?p=about-us

I’m trying to catch the incoming link like this.

if(!isset($_REQUEST['p']))
{
    header('location: index.php');
    exit;
}
else
{
    // Check the page slug is valid or not.
    $statement = $pdo->prepare("SELECT * FROM mbo_page WHERE page_slug=? AND status=?");
    $statement->execute(array($_REQUEST['p'],'Active'));
    $total = $statement->rowCount();
    if( $total == 0 )
    {
        header('location: index.php');
        exit;
    }
}

How should I write the rule so that it looks like this? Your help is appreciated. http://localhost/aione/page/about-us

Advertisement

Answer

This seems to be a trivial question.

Try this:

<a class="menu-link" href="'.BASE_URL.'page/'.$row1['page_slug'].'">';

Or, you can try this instead:

define("URL_PAGE", "page/");

Your .htaccess file needs to look something like this:

<IfModule mod_rewrite.c>
    RewriteEngine On
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteRule ^/*page/([^/]*)$ page.php?id=$1 [L,QSA] #If static R=301
</IfModule>

Place .htaccess in the same directory (i.e. folder) as page.php. (e.g. in http://localhost/aione/)

Notes:

  • ^/*page/([^/]*)$ page.php?id=$1 only works with the following style: “http://localhost/aione/page/about-us”, if you want something like about-us/test to also be passed as page.php?id=about-us/test, use the following rule:

  • ^/*page/(.*)$ page.php?id=$1 also matches for something like “http://localhost/aione/page/about-us/test/link”, and it will rewrite to: page.php?id=about-us/test/link, meaning the following:

$_GET['id'] === 'about-us/test/link'; // true

as always, remember to validate and sanitize any user input.

User contributions licensed under: CC BY-SA
2 People found this is helpful
Advertisement