I want to make my URL clean. Below is what I want to do:
Dirty URL: www.site.com/index.php?page=products
Clean URL: www.site.com/products/
For this, I write these codes in .htaccess file:
Options +FollowSymLinks
RewriteEngine On
RewriteBase /
RewriteRule ^([^/]*)/$ /index.php?page=$1
But whenever I click on the links in my project, I redirect to localhost
page. Links are in this form:
<a href="products">Products</a>
I think it may be related to href
value.
What's the correct form for href
value? href="product"
,href="/product/"
,etc. ?
There is no correct form for your href attribute, each form acts in different ways:
<a href="products">Products</a>
The above would go relatively from the page you are currently on, so might be domain.com/products
but might be domain.com/shop/products
You are better using a link direct from the root by using a /
at the start, for example:
<a href="/products">Products</a>
would go to domain.com/products
(note the lack of a /
at the end of the url, which will make it not work with your current htaccess rule)
To make both /products
and /products/
go to the correct place, change your htaccess rule to:
RewriteRule ^([^/]*)/?$ /index.php?page=$1
The ?
will make the character preceding it optional, so the /
in this case.