I now have a complete fix for not only trailing slash(es), but also characters following the slash, and a redirect to the proper/permissible path.
RewriteEngine on
RewriteCond %{REQUEST_URI} ^(.*\.shtml?)/.*
$RewriteRule ^.*$ http://%{HTTP_HOST}%1 [R=301,L]
You also deny these altogether by changing [R=301,L] to [F,L]. This will generate a 403 FORBIDDEN response.
Showing posts with label coding. Show all posts
Showing posts with label coding. Show all posts
Saturday, March 1, 2008
Friday, February 29, 2008
.htaccess fix for trailing slash(es) on server-parsed HTML files
I just came across something slimilar. Here's a link:
http://www.webmasterworld.com/apache/3412891.htm
http://www.webmasterworld.com/apache/3412891.htm
Friday, February 15, 2008
.htaccess fix for trailing slash(es) on server-parsed HTML files
Ok, this is a strange post, but since Blogger is immediately indexed in Google search, I thought I'd put this out there for any other geek-zoids out there like myself. I spent a great part of yesterday googling a solution to this problem. It is an obscure condition to say the least, but perhaps there is one other person among the world's six billion people who needs this same solution.
The problem: Trailing slashes on a server-parsed html file breaks every relatively linked resource. Images, style sheets, etc. won't display. Apache 2.0 has an httpd mechanism to ignore the trailing slash, but if it is not configured for this, or if you're using Apache 1.3, the trailing slash(es) will present a problem. So I came up with this snippit to put in your .htaccess file. If you don't know what a .htaccess file is, google it. Put this code in the root directory as follows:
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} (.*)/$
RewriteRule ^(.*)$ http://%{http_host/} [R=301,L]
Requested URLs with trailing slashes will instead serve up the default page. Of course, this only removes the trailing slash (or multiple slashes), which is the most common issue encountered thanks to the myriad search engines and spammers who abuse your cgi/pl forms. This fix does not prevent a trailing slash with extra characters following (ex: www.yourhost.tld/index.html/a). If you need this, add the appropriate RegEx to to fix your particular situation. Your snippit would then look like this:
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} (.*)/$ [OR]
RewriteCond %{REQUEST_URI} (.*)/PutYourRegExHere$
RewriteRule ^(.*)$ http://%{http_host/} [R=301,L]
Another--although undependable--way to handle this is to add a conditional SSI (server-side include) meta refresh statement. This is problematic because it will be ignored by robots, crawlers, and the like, not to mention browsers that either ignore or allow the user to disable meta refresh. I tried to include it anyway, but then Blogger wouldn't allow it because it tried to interpret it as html code. But it is made to be irrelevant by the solution above. And if you can use SSI in the first place, you can almost certainly use .htaccess.
If you need some guidance, try these links
Comprehensive guide to .htaccess
Lissa Explains it All -- .htaccess Tutorial
Apache Core Features
Regular Expression Tutorial - Learn How to Use and Get The Most out of Regular Expressions
The problem: Trailing slashes on a server-parsed html file breaks every relatively linked resource. Images, style sheets, etc. won't display. Apache 2.0 has an httpd mechanism to ignore the trailing slash, but if it is not configured for this, or if you're using Apache 1.3, the trailing slash(es) will present a problem. So I came up with this snippit to put in your .htaccess file. If you don't know what a .htaccess file is, google it. Put this code in the root directory as follows:
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} (.*)/$
RewriteRule ^(.*)$ http://%{http_host/} [R=301,L]
Requested URLs with trailing slashes will instead serve up the default page. Of course, this only removes the trailing slash (or multiple slashes), which is the most common issue encountered thanks to the myriad search engines and spammers who abuse your cgi/pl forms. This fix does not prevent a trailing slash with extra characters following (ex: www.yourhost.tld/index.html/a). If you need this, add the appropriate RegEx to to fix your particular situation. Your snippit would then look like this:
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} (.*)/$ [OR]
RewriteCond %{REQUEST_URI} (.*)/PutYourRegExHere$
RewriteRule ^(.*)$ http://%{http_host/} [R=301,L]
Another--although undependable--way to handle this is to add a conditional SSI (server-side include) meta refresh statement. This is problematic because it will be ignored by robots, crawlers, and the like, not to mention browsers that either ignore or allow the user to disable meta refresh. I tried to include it anyway, but then Blogger wouldn't allow it because it tried to interpret it as html code. But it is made to be irrelevant by the solution above. And if you can use SSI in the first place, you can almost certainly use .htaccess.
If you need some guidance, try these links
Comprehensive guide to .htaccess
Lissa Explains it All -- .htaccess Tutorial
Apache Core Features
Regular Expression Tutorial - Learn How to Use and Get The Most out of Regular Expressions
Subscribe to:
Posts (Atom)