Apache friendly urls

2019-01-29 07:32发布

问题:

I've got a small CMS system written in PHP and running on Apache. The format of the URLs this CMS system uses/generates is:

/display.php?PageID=xxx where xxx is just some integer number. As you can see, those URLs are not very friendly, neither for users nor search engines.

I believe that using mod_rewrite (or something like that) and .htaccess files I should be able to configure Apache for URL-rewriting. I have searched for information about this before but I did not find any easy method to do this, it always involved messing with regular expressions, which I'm not very familiar with.

Since the website in question is really simple and small, just 5-10 different pages, I would really like to be able to just hard-code the configuration, without any special rules or regexps.

I'd just like to map a friendly URL to an actual URL, perhaps like this:

/about = /display.php?PageID=44
/products = /display.php?PageID=34

etc.

Is it possible to configure the mod_rewrite plugin in a basic way like this?

Could someone explain the easiest method to do this? Explain it to me as if I was a child :-)

Thanks in advance!

回答1:

well putting something like

RewriteEngine on

RewriteRule ^about$  ./display.php?PageID=44
RewriteRule ^products$ ./display.php?PageID=34

in your .htaccess-file shouldn't be the big deal I think...



回答2:

URL Rewriting for Beginners is my favorite intro article to this, it should cover what you're looking for. In fact, the first actual example where you write a .htaccess file is almost identical to what you want.



回答3:

Another way is filter by a dynamic php file with a mapping for pages or a routing strategy like frameworks like drupal code igniter .... and your URL will be like my-pages/about.html -> display.php?PageID=44 my-pages/products.html -> display.php?PageID=34

and so on

Here a suggestion for .htaccess file and the filter the action with this strategy

--- .htaccess file ---- *RewriteEngine on

RewriteRule ^my-pages/(.).html$ MY-URL.php [QSA,L,E]

---------------- MY-URL.php ---------

<?php 

$PREFIX = 'my-pages/'; //--- not used 
$mapping=array(
        'about' => 44,
        'products' => 34
);
$pathinfo=   pathinfo( $_SERVER['REQUEST_URI'] );
/*  $pathinfo['dirname']  ->  my-pages 
    $pathinfo['basename'] -> ???.html
    $pathinfo['extension']->  .html
*/
$page = substr( $pathinfo['basename'] ,0,-5);


if( isset( $mapping[$page] ){
         //  ---- redirect or include
         YUOR CODE HERE 
}  
else {
        //--- error 404
          YUOR CODE HERE  
}


?>