I am developing a website using PHP and Apache. I wanna turn my URLs from
www.example.com/book.php?book=title
into something like this, if it is possible of course:
www.example.com/book/title
Notice that the title of the books are unique and cannot be repeated.
I`ve read about this, but none of the posts were clear enough for a beginner like me. Do you guys know any tutorial that explains that?
Thanks.
Here you will know how to change dynamic URL to static URL with the help of the . htaccess file. . htaccess will provide the features of URL rewriting. If the requirement for you to rewrite the URL (dynamic URL to static URL) is limited, then the rules described in the examples of Rodolphe in a hardcode .
Dynamic URLs have the disadvantage that different URLs can have the same content. So different users might link to URLs with different parameters which have the same content. That's one reason why webmasters sometimes want to rewrite their URLs to static ones.
URLs are classified into two types: static and dynamic. A static URL is one in which the content of the web page remains the same as long as the changes are not hard coded within the HTML. On the other hand, a dynamic URL is one which is a result of a search within a website driven by a database running on some script.
Even though Google is a dynamic website, it encourages site owners to use static parts such as Accelerated Mobile Pages (AMP). Even though Google is a dynamic website, it may have certain static parts.
Here's the way kohana (and 99.99% of php frameworks) does it
Add an .htaccess file(if using apache)
# Turn on URL rewriting
RewriteEngine On
RewriteBase /
# Allow any files or directories that exist to be displayed directly
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
# Rewrite all other URLs to index.php/URL
RewriteRule .* index.php/$0 [PT,L]
This redirects all urls to index.php. index.php will be some sort of front controller that loads scripts based on the url
So in your example:
localhost/book/title
index.php would be loaded. It would go into the url and get the page (controller) to load that will actually do all the work. in this case maybe books.php. books.php would get the title of the book from the url and then search a database or do whatever it has to with that name.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With