How to create directory like urls for each page on a website
I have a content site that spreads across multiple pages but there is only 1 index.php file which retrieves the data from the database based on the page no.
Currently the url direction has to be something like domainname.com/Page/content.php?page=3
I have noticed, quite a few sites have directory like structure for the urls like:
domainname.com/Page/3
I know how to change it to domainname.com/Page/?page=3
but I am looking to remove the ?page
part.
How 开发者_运维百科can I do this, without individually creating a directory for each page, the content keeps growing, hence changes for each page.
Thanks
These sites use mod_rewrite to do a "rewrite" on the requested URL using a regular expression
EDIT:
rewriting this: domainname.com/Page/3
to that: domainname.com/Page/content.php?page=3
would look like this in the .htaccess
file:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([a-zA-Z]*)/(.*)$ /content.php/$1/page=$2 [L]
</IfModule>
<IfModule !mod_rewrite.c>
ErrorDocument 404 /error_with_mod_rewrite.html
</IfModule>
here i also made a restriction for the var name to be a letter either capital or small.
"URL Rewrites" is the feature that performs a custom URL structure. This is a very common and important feature in most common web servers. URL rewrites are not just available with Apache. For example,
Apache uses the Mod_Rewrite Module.
Nginx uses the 'Rewrite' and "Try_Files" directives.
Cherokee has the feature built-in, configurable from admin web panel.
there are many more examples, these are just some I have worked with. In addition, I have heard some projects do the rewrites in the programming code. This has become more prevalent with the new language specific web-servers.
精彩评论