validating .htaccess before deployment
In order to get better SEO and cleaner URLs, I tend to export certain RewriteRules directly into the .htaccess (eg, RewriteRule ^The_North_Face(.*)$ index.php?a=brands&id=27开发者_C百科&extras=%1 [NC,L]
and so forth for each brand or category). It's a lot more complex than that but today I discovered that the file is only as good as the data it's trying to use. The site owner managed to put empty category names / URLs and some unescaped characters that caused a nasty internal server error, blocking any and all site access (inclusive of the tool to rebuild it).
I realise that the best defence here will probably be good training + failsafe at the CMS level. Regretfully, this is a 3-rd party solution called CubeCart which I can't dip into for the time being, the SEO solution was supposed to be standalone and just using the CubeCart data.
Obviously, I'd have to add some checks to do with brand / category / landing page names. Even so, I'd very much like to parse / validate the newly built .htaccess before replacing the 'live' one in order to avoid possible issues to do with syntax. Are there any syntax validators / ways to test Apache against a new .htaccess?
I can also think of deploying it in a sub-directory, then using curl to GET a few requests as a test, anything else I can do?
You may use something like WordPress does:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
... and then in the index.php
file parse the $_SERVER["REDIRECT_URL"]
against your website's URI logic. This way it would be easier for you to process any database variables like brand or category automatic through the PHP, without editing the .htaccess file on every content change.
I would suggest redirecting all requests to a file rewrite.php
. There, you parse the requested file and match it against an array of rules. You use the result for filling the $_GET
array and then include the correct file.
PHP syntax errors are much easier to find and you will definitely not get a 500 error page.
精彩评论