url – parameters as part of the path [ 1002 views ]
Goal: simplify the url
A normal url request with parameters looks like this:
www.server.com/mysite/index.php?param1=value1
In many cases we need the following form:
www.server.com/mysite/value1
how to:
1. we have to accept the invalid request as a normal one.
Let’s create a .htaccess file in the root with the following content:
<IfModule mod_rewrite.c> RewriteEngine On RewriteBase /mysite/ RewriteRule ^index\.php$ - [L] RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_FILENAME} !-d RewriteRule . /mysite/index.php [L] </IfModule>
2. receive the parameter in your php code
$url = explode('/',getenv('REQUEST_URI')); $code = array_pop ($url);
or
1. we can transfer the invalid request to a normal one.
www.server.com/value1 -> www.server.com/?q=value1
Let’s create a .htaccess file in the root with the following content:
<IfModule mod_rewrite.c> RewriteEngine on RewriteBase / RewriteRule ^/?([-A-Za-z0-9]+)/?$ ?q=$1 [R=301,L] <-- with redirect or RewriteRule ^/?([-A-Za-z0-9]+)/?$ ?q=$1 [L] <-- no redirect </IfModule>
where R=301
is doing a quick jump with 301 Moved Permanently to the converted request string.
2. receive the parameter in your php code
$code = $_GET["q"];