简体   繁体   中英

Clean/Pretty URL with PHP menu

I'm using a PHP menu for my website. There are only max. one subpage per menu item eg mypage.com/services and never more subpages eg mypage.com/services/blub.

index.php:

<?php require_once("navigation.php"); ?>
...
<?php include('sites/'.$_GET['p'] . '.php'); ?>

navigation.php

<ul class="nav navbar-nav">
    <li>
        <a href="?p=home">Home</a>
    </li>
    <li>
        <a href="?p=services">Services</a>
    </li>
    ...
</ul>

Now I want to realize pretty/clean urls. Currently my URLs look like this:

https://www.example.com/?p=services

and I want this:

https://www.example.com/services

I've written the following in my htaccess, but my page just looked blank:

RewriteEngine On
RewriteCond %{QUERY_STRING} (.*)
RewriteRule ^(.*) index.php?url=$1&query=%1 [B,L]

Put this is your .htaccess file

#turn on url rewriting 
RewriteEngine on

#remove the need for .php extention 
RewriteCond %{REQUEST_FILENAME} !-d 
RewriteCond %{REQUEST_FILENAME}\.php -f 
RewriteRule ^(.*)$ $1.php

This allows you to access .php files without the extension, so your links should read

href="/somepage"

and this will direct to

href="/somepage.php" 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM