I need to have 1 variable or sometimes 2 variables in my url.
http://example.com/home
or http://example.com/home/hr
I created the following .htaccess
file in public_html
folder:
RewriteEngine On RewriteRule ^([a-zA-Z0-9]+)$ index.php?page=$1
RewriteRule ^([a-zA-Z0-9]+)/$ index.php?page=$1 ReWriteRule
^([^/]+)/(\d+)/? index.php?page=$1&q=$2
It's not working! I do have index.php
in public_html
and hr.php
in public_html/view
now hr.php
is included in index.php
:
include("view/hr.php")
so now index.php
has page variable and /view/hr.php
has the 2nd variable.
How can I make it work?
Don't put your logic in the .htaccess
, rewrite everything except your static files to one php file, then route your requests inside that. E.g.:
# only rewrite static file URLs when not found, otherwise let them through
RewriteCond $0 !^(?:js|css|img|video|audio)$ [OR]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond ^[^/]+ index.php
Now, in index.php
, you have the URL in $_SERVER['REQUEST_URI']
and you can include any file you want.