{"id":31,"date":"2011-11-30T18:45:08","date_gmt":"2011-11-30T18:45:08","guid":{"rendered":"http:\/\/beta.robertprice.co.uk\/robblog\/2011\/11\/adding_a_trailing_slash_to_a_zend_framework_url-shtml\/"},"modified":"2011-11-30T18:45:08","modified_gmt":"2011-11-30T18:45:08","slug":"adding_a_trailing_slash_to_a_zend_framework_url-shtml","status":"publish","type":"post","link":"https:\/\/www.robertprice.co.uk\/robblog\/adding_a_trailing_slash_to_a_zend_framework_url-shtml\/","title":{"rendered":"Adding A Trailing Slash To A Zend Framework URL"},"content":{"rendered":"<p>\n&#8220;We need all our URL&#8217;s to have a trailing slash&#8221;, was a request I had in recently.\n<\/p>\n<p>\nThis sounds easy enough, a quick rewrite rule will do the trick, and that is what I added.\n<\/p>\n<pre class=\"lang:apache decode:true \" >RewriteRule ^(.*[^\/])$ \/$1\/ [R=301,L]\n<\/pre>\n<p>\nI look at the start of the URL, grab every character to the end of the URL, making sure the last one isn&#8217;t a slash already, then rewrite that with slashes around it, issuing a 301 redirect and informing Apache not to process any more rules.\n<\/p>\n<p>\nThis works great for pages already on the server, for example <code>\/guestbook<\/code> becomes <code>\/guestbook\/<\/code>. However, what if we wanted to load <code>\/logo.gif<\/code>, this would rewrite to <code>\/logo.gif\/<\/code> and cause a error.\n<\/p>\n<p>\nOK, so we need not to match every character in the URL, but only if it doesn&#8217;t have a dot in there. The site in question was a Zend Framework application, and this caused it&#8217;s own problems.\n<\/p>\n<p>\nZend Framework applications have their own rewriting rules&#8230;\n<\/p>\n<pre class=\"lang:apache decode:true \" >RewriteCond %{REQUEST_FILENAME} -s [OR]\nRewriteCond %{REQUEST_FILENAME} -l [OR]\nRewriteCond %{REQUEST_FILENAME} -d\nRewriteRule ^.*$ - [NC,L]\nRewriteRule ^.*$ index.php [NC,L]\n<\/pre>\n<p>\nThis checks if the requested resource is actually a real object Apache can serve, if not it passes it over to the Zend Framework to handle internally via the index.php script.\n<\/p>\n<p>\nSo what happens if <code>\/guestbook<\/code> is actually a Zend Framework application?\n<\/p>\n<p>\nWell, the URL would be rewritten to <code>\/index.php\/<\/code>.\n<\/p>\n<p>\nWhy? Well the first rewrite we added a slash, this caused the page to re-requested, this then passed to the Zend Framework rewrites, this saw it needed to rewrite to index.php, this caused the page to be re-requested again internally, which added a slash to the end, and redirected again. Finally this request was \/index.php\/ which doesn&#8217;t exist and failed.\n<\/p>\n<p>\nWow, that was a mouthful, let&#8217;s look at that as a sequence diagram.\n<\/p>\n<p>\n<img loading=\"lazy\" decoding=\"async\" src=\"\/robblog\/images\/rewritesequence1.png\" alt=\"sequence diagram showing apache rewrites and Zend Framework working incorrectly\" width=\"403\" height=\"453\" class=\"blogimage\" \/>\n<\/p>\n<p>\nWhat we need is to only rewrite the requested URL if it doesn&#8217;t have a trailing slash and if it doesn&#8217;t have a dot in the request and if it isn&#8217;t an internal Apache redirect. We also only want to rewrite if the request is a GET, POSTs (DELETE&#8217;s and PUT&#8217;s too) don&#8217;t support rewriting in the browser.\n<\/p>\n<p>\nThis indicates a series a conditions that need to be met, so tells me we should be using Apache&#8217;s <code>RewriteCond<\/code> directive.\n<\/p>\n<p>\nLet&#8217;s check for a GET request first&#8230;\n<\/p>\n<pre class=\"lang:apache decode:true \" >RewriteCond %{REQUEST_METHOD} GET [NC]\n<\/pre>\n<p>\nThe <code>%{REQUEST_METHOD}<\/code> variable in a rewrite rule tells us the HTTP method used to make the request, so we just need to check if that is GET. The <code>[NC]<\/code> makes the check case insensitive so matches GET, get, Get, etc&#8230;\n<\/p>\n<p>\nNext we need to see if the request already has a trailing slash.\n<\/p>\n<pre class=\"lang:apache decode:true \" >RewriteCond %{REQUEST_URI} !\/$\n<\/pre>\n<p>\nThis checks the requested URI and sees if the last character isn&#8217;t a slash.\n<\/p>\n<p>\nNow we need to check if there are any dots in the request.\n<\/p>\n<pre class=\"lang:apache decode:true \" >RewriteCond %{REQUEST_URI} !.\n<\/pre>\n<p>\nThis just checks that there isn&#8217;t a dot in the requested URI. As dot normally matches any character, we need to escape this with a clash first.\n<\/p>\n<p>\nIf all these conditions match, we can issue our RewriteRule.\n<\/p>\n<pre class=\"lang:apache decode:true \" >RewriteRule ^.*$ %{REQUEST_URI}\/ [R=301,L]\n<\/pre>\n<p>\nThis takes the requested URI, adds a slash and issues a 301 status code to force the browser to re-request the page and change the URL shown in the address bar. We add the <code>L<\/code> to tell Apache not to process any more rules.\n<\/p>\n<p>\nLet&#8217;s put this all together, and insert it above the normal Zend Framework rewrites, but after <code>RewriteEngine On<\/code>.\n<\/p>\n<pre class=\"lang:apache decode:true \" >RewriteCond %{REQUEST_METHOD} GET [NC]\nRewriteCond %{REQUEST_URI} !\/$\nRewriteCond %{REQUEST_URI} !.\nRewriteRule ^.*$ %{REQUEST_URI}\/ [R=301,L]\n<\/pre>\n<p>\nLet&#8217;s request <code>\/guestbook<\/code> again, and see what happens.\n<\/p>\n<p>\n<img loading=\"lazy\" decoding=\"async\" src=\"\/robblog\/images\/rewritesequence2.png\" alt=\"sequence diagram showing apache rewrites and Zend Framework\" width=\"488\" height=\"355\" class=\"blogimage\" \/><\/p>\n","protected":false},"excerpt":{"rendered":"<p>&#8220;We need all our URL&#8217;s to have a trailing slash&#8221;, was a request I had in recently. This sounds easy enough, a quick rewrite rule will do the trick, and that is what I added. RewriteRule ^(.*[^\/])$ \/$1\/ [R=301,L] I look at the start of the URL, grab every character to the end of the &hellip; <a href=\"https:\/\/www.robertprice.co.uk\/robblog\/adding_a_trailing_slash_to_a_zend_framework_url-shtml\/\" class=\"more-link\">Continue reading<span class=\"screen-reader-text\"> &#8220;Adding A Trailing Slash To A Zend Framework URL&#8221;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"closed","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"inline_featured_image":false,"footnotes":""},"categories":[2],"tags":[10,75,81],"class_list":["post-31","post","type-post","status-publish","format-standard","hentry","category-dev","tag-apache","tag-web-development","tag-zend-framework"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.7 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>Adding A Trailing Slash To A Zend Framework URL - Robert Price<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/www.robertprice.co.uk\/robblog\/adding_a_trailing_slash_to_a_zend_framework_url-shtml\/\" \/>\n<meta property=\"og:locale\" content=\"en_GB\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Adding A Trailing Slash To A Zend Framework URL - Robert Price\" \/>\n<meta property=\"og:description\" content=\"&#8220;We need all our URL&#8217;s to have a trailing slash&#8221;, was a request I had in recently. This sounds easy enough, a quick rewrite rule will do the trick, and that is what I added. RewriteRule ^(.*[^\/])$ \/$1\/ [R=301,L] I look at the start of the URL, grab every character to the end of the &hellip; Continue reading &quot;Adding A Trailing Slash To A Zend Framework URL&quot;\" \/>\n<meta property=\"og:url\" content=\"https:\/\/www.robertprice.co.uk\/robblog\/adding_a_trailing_slash_to_a_zend_framework_url-shtml\/\" \/>\n<meta property=\"og:site_name\" content=\"Robert Price\" \/>\n<meta property=\"article:published_time\" content=\"2011-11-30T18:45:08+00:00\" \/>\n<meta name=\"author\" content=\"rob\" \/>\n<meta name=\"twitter:card\" content=\"summary_large_image\" \/>\n<meta name=\"twitter:label1\" content=\"Written by\" \/>\n\t<meta name=\"twitter:data1\" content=\"rob\" \/>\n\t<meta name=\"twitter:label2\" content=\"Estimated reading time\" \/>\n\t<meta name=\"twitter:data2\" content=\"3 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\\\/\\\/schema.org\",\"@graph\":[{\"@type\":\"Article\",\"@id\":\"https:\\\/\\\/www.robertprice.co.uk\\\/robblog\\\/adding_a_trailing_slash_to_a_zend_framework_url-shtml\\\/#article\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/www.robertprice.co.uk\\\/robblog\\\/adding_a_trailing_slash_to_a_zend_framework_url-shtml\\\/\"},\"author\":{\"name\":\"rob\",\"@id\":\"https:\\\/\\\/www.robertprice.co.uk\\\/robblog\\\/#\\\/schema\\\/person\\\/fac6d5b076e0e14e1fb13e15b542a6c5\"},\"headline\":\"Adding A Trailing Slash To A Zend Framework URL\",\"datePublished\":\"2011-11-30T18:45:08+00:00\",\"mainEntityOfPage\":{\"@id\":\"https:\\\/\\\/www.robertprice.co.uk\\\/robblog\\\/adding_a_trailing_slash_to_a_zend_framework_url-shtml\\\/\"},\"wordCount\":568,\"keywords\":[\"Apache\",\"Web Development\",\"Zend Framework\"],\"articleSection\":[\"Dev\"],\"inLanguage\":\"en-GB\"},{\"@type\":\"WebPage\",\"@id\":\"https:\\\/\\\/www.robertprice.co.uk\\\/robblog\\\/adding_a_trailing_slash_to_a_zend_framework_url-shtml\\\/\",\"url\":\"https:\\\/\\\/www.robertprice.co.uk\\\/robblog\\\/adding_a_trailing_slash_to_a_zend_framework_url-shtml\\\/\",\"name\":\"Adding A Trailing Slash To A Zend Framework URL - Robert Price\",\"isPartOf\":{\"@id\":\"https:\\\/\\\/www.robertprice.co.uk\\\/robblog\\\/#website\"},\"datePublished\":\"2011-11-30T18:45:08+00:00\",\"author\":{\"@id\":\"https:\\\/\\\/www.robertprice.co.uk\\\/robblog\\\/#\\\/schema\\\/person\\\/fac6d5b076e0e14e1fb13e15b542a6c5\"},\"breadcrumb\":{\"@id\":\"https:\\\/\\\/www.robertprice.co.uk\\\/robblog\\\/adding_a_trailing_slash_to_a_zend_framework_url-shtml\\\/#breadcrumb\"},\"inLanguage\":\"en-GB\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\\\/\\\/www.robertprice.co.uk\\\/robblog\\\/adding_a_trailing_slash_to_a_zend_framework_url-shtml\\\/\"]}]},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\\\/\\\/www.robertprice.co.uk\\\/robblog\\\/adding_a_trailing_slash_to_a_zend_framework_url-shtml\\\/#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\\\/\\\/www.robertprice.co.uk\\\/robblog\\\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Adding A Trailing Slash To A Zend Framework URL\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\\\/\\\/www.robertprice.co.uk\\\/robblog\\\/#website\",\"url\":\"https:\\\/\\\/www.robertprice.co.uk\\\/robblog\\\/\",\"name\":\"Robert Price\",\"description\":\"\",\"potentialAction\":[{\"@type\":\"SearchAction\",\"target\":{\"@type\":\"EntryPoint\",\"urlTemplate\":\"https:\\\/\\\/www.robertprice.co.uk\\\/robblog\\\/?s={search_term_string}\"},\"query-input\":{\"@type\":\"PropertyValueSpecification\",\"valueRequired\":true,\"valueName\":\"search_term_string\"}}],\"inLanguage\":\"en-GB\"},{\"@type\":\"Person\",\"@id\":\"https:\\\/\\\/www.robertprice.co.uk\\\/robblog\\\/#\\\/schema\\\/person\\\/fac6d5b076e0e14e1fb13e15b542a6c5\",\"name\":\"rob\",\"image\":{\"@type\":\"ImageObject\",\"inLanguage\":\"en-GB\",\"@id\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/6f0eb511179100a4e968abc70403e33686e6ab3e992e392bedd2ccac01da666c?s=96&d=mm&r=g\",\"url\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/6f0eb511179100a4e968abc70403e33686e6ab3e992e392bedd2ccac01da666c?s=96&d=mm&r=g\",\"contentUrl\":\"https:\\\/\\\/secure.gravatar.com\\\/avatar\\\/6f0eb511179100a4e968abc70403e33686e6ab3e992e392bedd2ccac01da666c?s=96&d=mm&r=g\",\"caption\":\"rob\"}}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Adding A Trailing Slash To A Zend Framework URL - Robert Price","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/www.robertprice.co.uk\/robblog\/adding_a_trailing_slash_to_a_zend_framework_url-shtml\/","og_locale":"en_GB","og_type":"article","og_title":"Adding A Trailing Slash To A Zend Framework URL - Robert Price","og_description":"&#8220;We need all our URL&#8217;s to have a trailing slash&#8221;, was a request I had in recently. This sounds easy enough, a quick rewrite rule will do the trick, and that is what I added. RewriteRule ^(.*[^\/])$ \/$1\/ [R=301,L] I look at the start of the URL, grab every character to the end of the &hellip; Continue reading \"Adding A Trailing Slash To A Zend Framework URL\"","og_url":"https:\/\/www.robertprice.co.uk\/robblog\/adding_a_trailing_slash_to_a_zend_framework_url-shtml\/","og_site_name":"Robert Price","article_published_time":"2011-11-30T18:45:08+00:00","author":"rob","twitter_card":"summary_large_image","twitter_misc":{"Written by":"rob","Estimated reading time":"3 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"Article","@id":"https:\/\/www.robertprice.co.uk\/robblog\/adding_a_trailing_slash_to_a_zend_framework_url-shtml\/#article","isPartOf":{"@id":"https:\/\/www.robertprice.co.uk\/robblog\/adding_a_trailing_slash_to_a_zend_framework_url-shtml\/"},"author":{"name":"rob","@id":"https:\/\/www.robertprice.co.uk\/robblog\/#\/schema\/person\/fac6d5b076e0e14e1fb13e15b542a6c5"},"headline":"Adding A Trailing Slash To A Zend Framework URL","datePublished":"2011-11-30T18:45:08+00:00","mainEntityOfPage":{"@id":"https:\/\/www.robertprice.co.uk\/robblog\/adding_a_trailing_slash_to_a_zend_framework_url-shtml\/"},"wordCount":568,"keywords":["Apache","Web Development","Zend Framework"],"articleSection":["Dev"],"inLanguage":"en-GB"},{"@type":"WebPage","@id":"https:\/\/www.robertprice.co.uk\/robblog\/adding_a_trailing_slash_to_a_zend_framework_url-shtml\/","url":"https:\/\/www.robertprice.co.uk\/robblog\/adding_a_trailing_slash_to_a_zend_framework_url-shtml\/","name":"Adding A Trailing Slash To A Zend Framework URL - Robert Price","isPartOf":{"@id":"https:\/\/www.robertprice.co.uk\/robblog\/#website"},"datePublished":"2011-11-30T18:45:08+00:00","author":{"@id":"https:\/\/www.robertprice.co.uk\/robblog\/#\/schema\/person\/fac6d5b076e0e14e1fb13e15b542a6c5"},"breadcrumb":{"@id":"https:\/\/www.robertprice.co.uk\/robblog\/adding_a_trailing_slash_to_a_zend_framework_url-shtml\/#breadcrumb"},"inLanguage":"en-GB","potentialAction":[{"@type":"ReadAction","target":["https:\/\/www.robertprice.co.uk\/robblog\/adding_a_trailing_slash_to_a_zend_framework_url-shtml\/"]}]},{"@type":"BreadcrumbList","@id":"https:\/\/www.robertprice.co.uk\/robblog\/adding_a_trailing_slash_to_a_zend_framework_url-shtml\/#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/www.robertprice.co.uk\/robblog\/"},{"@type":"ListItem","position":2,"name":"Adding A Trailing Slash To A Zend Framework URL"}]},{"@type":"WebSite","@id":"https:\/\/www.robertprice.co.uk\/robblog\/#website","url":"https:\/\/www.robertprice.co.uk\/robblog\/","name":"Robert Price","description":"","potentialAction":[{"@type":"SearchAction","target":{"@type":"EntryPoint","urlTemplate":"https:\/\/www.robertprice.co.uk\/robblog\/?s={search_term_string}"},"query-input":{"@type":"PropertyValueSpecification","valueRequired":true,"valueName":"search_term_string"}}],"inLanguage":"en-GB"},{"@type":"Person","@id":"https:\/\/www.robertprice.co.uk\/robblog\/#\/schema\/person\/fac6d5b076e0e14e1fb13e15b542a6c5","name":"rob","image":{"@type":"ImageObject","inLanguage":"en-GB","@id":"https:\/\/secure.gravatar.com\/avatar\/6f0eb511179100a4e968abc70403e33686e6ab3e992e392bedd2ccac01da666c?s=96&d=mm&r=g","url":"https:\/\/secure.gravatar.com\/avatar\/6f0eb511179100a4e968abc70403e33686e6ab3e992e392bedd2ccac01da666c?s=96&d=mm&r=g","contentUrl":"https:\/\/secure.gravatar.com\/avatar\/6f0eb511179100a4e968abc70403e33686e6ab3e992e392bedd2ccac01da666c?s=96&d=mm&r=g","caption":"rob"}}]}},"_links":{"self":[{"href":"https:\/\/www.robertprice.co.uk\/robblog\/wp-json\/wp\/v2\/posts\/31","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.robertprice.co.uk\/robblog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.robertprice.co.uk\/robblog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.robertprice.co.uk\/robblog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.robertprice.co.uk\/robblog\/wp-json\/wp\/v2\/comments?post=31"}],"version-history":[{"count":0,"href":"https:\/\/www.robertprice.co.uk\/robblog\/wp-json\/wp\/v2\/posts\/31\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.robertprice.co.uk\/robblog\/wp-json\/wp\/v2\/media?parent=31"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.robertprice.co.uk\/robblog\/wp-json\/wp\/v2\/categories?post=31"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.robertprice.co.uk\/robblog\/wp-json\/wp\/v2\/tags?post=31"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}