{"id":944,"date":"2004-12-27T13:08:47","date_gmt":"2004-12-27T18:08:47","guid":{"rendered":"\/?p=944"},"modified":"2016-07-28T10:06:48","modified_gmt":"2016-07-28T14:06:48","slug":"anchors-and-cruft-free-urls","status":"publish","type":"post","link":"https:\/\/mjtsai.com\/blog\/2004\/12\/27\/anchors-and-cruft-free-urls\/","title":{"rendered":"Anchors and Cruft-Free URLs"},"content":{"rendered":"<p>\r\nI&rsquo;m trying to clean up some URLs using <a href=\"http:\/\/httpd.apache.org\/docs-2.0\/mod\/mod_rewrite.html\">mod_rewrite<\/a>. I have <tt>.shtml<\/tt> files on the server, but I want browsers to be able to access them without the file extension. And if the browser <em>does<\/em> send a URL with <tt>.shtml<\/tt>, it should be redirected to the URL without the file extension. I have this much working, and here are the contents of the <tt>.htaccess<\/tt> file:\r\n<\/p>\r\n\r\n<pre>\r\nRewriteEngine On\r\nRewriteBase \/\r\n\r\n# remove .shtml; use THE_REQUEST to prevent infinite loops\r\nRewriteCond %{THE_REQUEST} ^GET\\ (.*)\\.shtml\\ HTTP\r\nRewriteRule (.*)\\.shtml$ $1 [R=301]\r\n\r\n# remove index\r\nRewriteRule (.*)\/index$ $1\/ [R=301]\r\n\r\n# remove slash if not directory\r\nRewriteCond %{REQUEST_FILENAME} !-d\r\nRewriteCond %{REQUEST_URI} \/$\r\nRewriteRule (.*)\/ $1 [R=301]\r\n\r\n# add .shtml to access file, but don't redirect\r\nRewriteCond %{REQUEST_FILENAME}.shtml -f\r\nRewriteCond %{REQUEST_URI} !\/$\r\nRewriteRule (.*) $1\\.shtml [L]\r\n<\/pre>\r\n\r\n<p>\r\nThe problem is that this doesn&rsquo;t work when the URL has an anchor. If I type:\r\n<\/p>\r\n\r\n<pre>\r\nhttp:\/\/www.example.com\/foo#bar\r\n<\/pre>\r\n\r\n<p>into the browser, it works fine. But:<\/p>\r\n\r\n<pre>\r\nhttp:\/\/www.example.com\/foo.shtml#bar\r\n<\/pre>\r\n\r\n<p>\r\ngets redirected to:\r\n<\/p>\r\n\r\n<pre>\r\nhttp:\/\/www.example.com\/foo\r\n<\/pre>\r\n\r\n<p>\r\ninstead of:\r\n<\/p>\r\n\r\n<pre>\r\nhttp:\/\/www.example.com\/foo#bar\r\n<\/pre>\r\n\r\n<p>\r\nAs far as I know, <tt>#bar<\/tt> stays in the browser; it isn&rsquo;t sent to\r\nthe server. So I don&rsquo;t think there&rsquo;s any way to preserve it using the\r\nrewrite rules. Is there any way to get the browser to tack on the <tt>#bar<\/tt> after it gets the 301 from the server?\r\n<\/p>","protected":false},"excerpt":{"rendered":"<p>I&rsquo;m trying to clean up some URLs using mod_rewrite. I have .shtml files on the server, but I want browsers to be able to access them without the file extension. And if the browser does send a URL with .shtml, it should be redirected to the URL without the file extension. I have this much [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"apple_news_api_created_at":"","apple_news_api_id":"","apple_news_api_modified_at":"","apple_news_api_revision":"","apple_news_api_share_url":"","apple_news_coverimage":0,"apple_news_coverimage_caption":"","apple_news_is_hidden":false,"apple_news_is_paid":false,"apple_news_is_preview":false,"apple_news_is_sponsored":false,"apple_news_maturity_rating":"","apple_news_metadata":"\"\"","apple_news_pullquote":"","apple_news_pullquote_position":"","apple_news_slug":"","apple_news_sections":"\"\"","apple_news_suppress_video_url":false,"apple_news_use_image_component":false,"footnotes":""},"categories":[2],"tags":[],"class_list":["post-944","post","type-post","status-publish","format-standard","hentry","category-technology"],"apple_news_notices":[],"_links":{"self":[{"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/posts\/944","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/comments?post=944"}],"version-history":[{"count":1,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/posts\/944\/revisions"}],"predecessor-version":[{"id":15437,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/posts\/944\/revisions\/15437"}],"wp:attachment":[{"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/media?parent=944"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/categories?post=944"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/mjtsai.com\/blog\/wp-json\/wp\/v2\/tags?post=944"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}