I'm trying to develop site on php and mysql, at this moment i'm 50% done with php coding, though I don't know almost anything about php programming :). Now I'm stuck in url structure for my site.
My sites root directory is localhost/mysite and when I going to a article page I have
localhost/mysite/article.php?title=any-article-title
I want to change this slug in to
localhost/mysite/article/any-article-title
I saw an exact similar thread here but I cant implement that answer to my site.
Any help will be highly appreciated
One more thing I would like to add - Is there any global rule to do the same for all files like article.php?title= or product.php?item= or category.php?id=
The possible problem that you may be facing here is the way how you accessing to your site: localhost/mysite/...
This can make perfectly valid rewrite rule not to work (based on other configuration parameters/circumstances). It will be better if you could set up your website to be accessible via dedicated domain name (which you can easily fake via hosts
file) -- so that you can access your local website via http://mysite.dev/index.php
instead of the current http://localhost/mysite.com/index.php
.
Anyway -- these are the rules:
# activate rewrite engine
RewriteEngine On
# treat /mysite/ as a root
RewriteBase /mysite/
# rewrite "localhost/mysite/article/some-title" into "localhost/mysite/article.php?title=some-title"
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule article/(.+)$ article.php?title=$1 [QSA,L]
You can do the rest (product.php?item=
or category.php?id=
) in a similar fashion.
http://www.freewebmasterhelp.com/tutorials/htaccess/2
Will teach you how to configure and what to write for each redirection
EDIT: I suppose you need to do individually for all the files
Something like this should do the trick:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^article/(.*)$ article.php?title=$1 [L]
for multiple handlers try something like this:
RewriteRule ^(.*)/(.*)$ router.php?type=$1&title=$2 [L]
router.php is there to make sure that you will address only allowed files (e.g. caregory.php, product.php and article.php) and prevent users to access any sensitive files using URLs like /config/blablabla.