8

I have a build script (Perl) that modifies the .htaccess file when I deploy my applications. As a double-check, I'd like to be able to perform some sort of syntax checking on the created .htaccess file.

I am familiar with the idea of using apachectl -t however, I am in a shared hosting environment and because of file access restrictions I cannot read certain configuration files specified by the sysadmins. Apachectl simply will not work in this regard.

Ideas or suggestions welcome.

1
  • But even if you could run apachectl -t it is used for virtualhosts only it would not serve to your needs. My best guess would be that you gona need to make your own validator ... perhaps asking in the stackoverflow.com will get you a lead of how to go thru about this. At any rate this is more programming related from what i see.
    – Prix
    Aug 28, 2010 at 17:37

5 Answers 5

2

There is no native .htaccess validator. If a third party tool exists, I am not sure if I would trust it. Since it is a script that modifies the file, you should be able to code the validation within your script.

You can also include testing and verification processes for your deployment, which would help reduce mistakes.

2
  • 2
    This would be part of the testing.....
    – Danny
    Mar 23, 2010 at 3:53
  • Ideally, I'd like to verify .htaccess has correct syntax as part of CI
    – Thayne
    Apr 13, 2020 at 15:54
2

Here two useful, free services:

This one lets you upload an .htaccess file for validation.
This one lets you paste the contents of an .htaccess file into a textbox for validation.

2
  • 2
    Welcome to Server Fault! Whilst this may theoretically answer the question, it would be preferable to include the essential parts of the answer here, and provide the link for reference.
    – Scott Pack
    Aug 26, 2012 at 0:01
  • 1
    Also, a link and run like that does generally tend to get flagged as spam, so next time, it may be wise to consider adding some content and context into your post. Aug 26, 2012 at 3:09
2

The most reliable syntax checker is Apache itself.

If you can handle 500ms of possible downtime (the time it takes to make an http request to localhost, very approximately) then do this:

  1. Make a backup of the current htaccess file
  2. Replace the current htaccess file with the file to be tested
  3. Make a request to the server, for example "curl localhost"
  4. If the curl is not successful, place the backup back
2
  • In general, functionality checks like these should be a standard part of your deployment environment. Good stuff!
    – womble
    Sep 10, 2015 at 3:52
  • +1, as misconfiguration might happen even with the valid syntax you will want such a step in any case and this will handle the syntax as well. Mar 12, 2019 at 5:48
1

Using cool Text Editors like Coda for Mac or TextMate for Mac or maybe Notepad++ for PC, you can enable syntax highlighting, which might not be perfect, but will be helpful. .htaccess is written in Per, so just turn on Syntax Highlighting for Perl!

1
  • 3
    .htaccess is not written in Perl, not by a longshot. Feb 25, 2016 at 5:22
0

You can simply use Notepad++ for syntax checking.

Silas Brill has been gracious enough to create a custom language for htaccess files and share it here. http://notepad-plus.sourceforge.net/commun/userDefinedLang/Htaccess.xml

Just go to View -> User-Defined-Dialogue -> Import

and restart Notepad++. You should be able to see the new language under the Language option.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .