{"id":790,"date":"2015-05-01T09:23:20","date_gmt":"2015-05-01T09:23:20","guid":{"rendered":"http:\/\/blog.silverterra.net\/?p=790"},"modified":"2015-05-02T12:03:45","modified_gmt":"2015-05-02T12:03:45","slug":"url-parameters-as-part-of-the-path","status":"publish","type":"post","link":"https:\/\/blog.silverterra.net\/?p=790","title":{"rendered":"url &#8211; parameters as part of the path"},"content":{"rendered":"<blockquote><p>Goal: simplify the url<\/p><\/blockquote>\n<p>A normal url request with parameters looks like this:<\/p>\n<pre data-enlighter-language=\"html\" class=\"EnlighterJSRAW\">\r\nwww.server.com\/mysite\/index.php?param1=value1\r\n<\/pre>\n<p>In many cases we need the following form:<\/p>\n<pre data-enlighter-language=\"html\" class=\"EnlighterJSRAW\">\r\nwww.server.com\/mysite\/value1\r\n<\/pre>\n<p><strong>how to:<\/strong><\/p>\n<p>1. we have to accept the <span class=\"red\">invalid<\/span> request as a normal one.<\/p>\n<p>Let&#8217;s <strong>create a .htaccess file<\/strong> in the root with the following content:<\/p>\n<pre data-enlighter-language=\"raw\" class=\"EnlighterJSRAW\">\r\n&lt;IfModule mod_rewrite.c&gt;\r\nRewriteEngine On\r\nRewriteBase \/mysite\/\r\nRewriteRule ^index\\.php$ - [L]\r\nRewriteCond %{REQUEST_FILENAME} !-f\r\nRewriteCond %{REQUEST_FILENAME} !-d\r\nRewriteRule . \/mysite\/index.php [L]\r\n&lt;\/IfModule&gt;\r\n<\/pre>\n<p>2. receive the parameter in your php code<\/p>\n<pre data-enlighter-language=\"php\" class=\"EnlighterJSRAW\">\r\n$url = explode('\/',getenv('REQUEST_URI'));\r\n$code = array_pop ($url);\r\n<\/pre>\n<p><b>or<\/b><\/p>\n<p>1. we can transfer the <span class=\"red\">invalid<\/span> request to a normal one.<\/p>\n<pre data-enlighter-language=\"html\" class=\"EnlighterJSRAW\">\r\nwww.server.com\/value1 -&gt; www.server.com\/?q=value1\r\n<\/pre>\n<p>Let&#8217;s <strong>create a .htaccess file<\/strong> in the root with the following content:<\/p>\n<pre data-enlighter-language=\"raw\" class=\"EnlighterJSRAW\">\r\n&lt;IfModule mod_rewrite.c&gt;\r\nRewriteEngine on\r\nRewriteBase \/\r\nRewriteRule ^\/?([-A-Za-z0-9]+)\/?$ ?q=$1 [R=301,L] &lt;-- with redirect\r\nor\r\nRewriteRule ^\/?([-A-Za-z0-9]+)\/?$ ?q=$1 [L]       &lt;-- no redirect\r\n&lt;\/IfModule&gt;\r\n<\/pre>\n<p>where <code data-enlighter-language=\"raw\" class=\"EnlighterJSRAW\">R=301<\/code> is doing a quick jump with <i>301 Moved Permanently<\/i> to the converted request string.<\/p>\n<p>2. receive the parameter in your php code<\/p>\n<pre data-enlighter-language=\"php\" class=\"EnlighterJSRAW\">\r\n$code = $_GET[&quot;q&quot;];\r\n<\/pre>\n","protected":false},"excerpt":{"rendered":"<p>Goal: simplify the url A normal url request with parameters looks like this: In many cases we need the following form: how to: 1. we have to accept the invalid request as a normal one. Let&#8217;s create a .htaccess file in the root with the following content: 2. receive the parameter in your php code [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[10,11],"tags":[],"class_list":["post-790","post","type-post","status-publish","format-standard","hentry","category-php","category-tricky"],"_links":{"self":[{"href":"https:\/\/blog.silverterra.net\/index.php?rest_route=\/wp\/v2\/posts\/790","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/blog.silverterra.net\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/blog.silverterra.net\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/blog.silverterra.net\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/blog.silverterra.net\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=790"}],"version-history":[{"count":3,"href":"https:\/\/blog.silverterra.net\/index.php?rest_route=\/wp\/v2\/posts\/790\/revisions"}],"predecessor-version":[{"id":793,"href":"https:\/\/blog.silverterra.net\/index.php?rest_route=\/wp\/v2\/posts\/790\/revisions\/793"}],"wp:attachment":[{"href":"https:\/\/blog.silverterra.net\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=790"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/blog.silverterra.net\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=790"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/blog.silverterra.net\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=790"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}