2

I have recently learned how to use my .htaccess file and a script to rewrite .URL shortcuts into functional links in a directory listing:

That solution is working well ... but my other .htaccess calls to modify the .URL items aren't working (unlike all the other file types):

AddIcon             http://SoSaysSunny.com/icos/url.ico     .URL
AddDescription      "<I>(web link)</I>"                     .URL
AddAlt              "URL"                                   *.URL

Did the rewriting process change the file extension?

Is there some other way to refer to these files?


Once I get that problem fixed, I have another: I begin the filenames of links to web stores with a "$". How can I specify the files with this naming scheme?

Here's what doesn't work (even with other file types):

AddIcon             http://SoSaysSunny.com/icos/cart.ico    $*.URL
AddDescription      "<I>(web store link)</I>"               $*.URL

I know it seems like a lot of work to use directory listing but it's crucial to my website organization (to avoid constantly having to update HTML index files).

I really appreciate your help.

  • Those directives are part of mod_autoindex, are you sure you have that module enabled in your Apache config? – Stephen Ostermiller Aug 2 '17 at 9:57
  • The documentation gives an example that uses a relative URL. I'd try AddIcon /icos/url.ico .URL – Stephen Ostermiller Aug 2 '17 at 10:01
  • 1
    @StephenOstermiller I don't think it really matters whether you use an absolute or relative URL here. Although an absolute URL would seem unnecessary if these icons are all stored locally. AFAIK, the stated URL is simply used as-is in the generated HTML. ie. As the value of the src attribute on the img element. – MrWhite Aug 2 '17 at 22:28
  • The format works for all the other file types: AddIcon http://SoSaysSunny.com/icos/pdf.ico .pdf AddDescription "<I>(PDF)</I>" .pdf AddAlt "PDF" *.pdf – SoSaysSunny Aug 3 '17 at 1:02
2

but my other .htaccess calls to modify the .URL items aren't working (unlike all the other file types)

Ah, yes, the "problem" is actually with the mod_rewrite code that sends requests for .URL files to your PHP script (that parses the file to get the URL to redirect to). This just needs a minor change: the NS flag is required on the RewriteRule directive.

The problem stems from the fact that when Apache constructs a directory listing it issues an internal subrequest for each file that is listed. The mod_rewrite code is rewriting this internal subrequest, which it doesn't need to, and preventing the AddIcon directive from matching (since the .url request is rewritten to a .php script). (It only needs to rewrite direct requests from the user.) Based on my earlier answer to your question, you probably have something like the following in your .htaccess file:

# Rewrite requests for ".url" files to our PHP script
RewriteCond %{REQUEST_FILENAME} -f
RewriteRule .\.url$ process-url-shortcut.php [NC,E=URL_FILE:%{REQUEST_FILENAME},L]

You just need to add the NS (nosubreq) flag to the RewriteRule in order to prevent the directive being processed for subrequests (ie. so it's only processed for non-subrequests). For example:

RewriteRule .\.url$ process-url-shortcut.php [NS,NC,E=URL_FILE:%{REQUEST_FILENAME},L]

I begin the filenames of links to web stores with a "$". How can I specify the files with this naming scheme?

Ok, this a bit of a weird one. However, this seems to work if you change the wildcard pattern from $*.URL to *$*.URL, ie. append a * to the pattern (as if there were characters before the $ - but there isn't). So, for example:

AddIcon             http://SoSaysSunny.com/icos/cart.ico    *$*.URL
AddDescription      "<I>(web store link)</I>"               $*.URL

The AddDescription directive still seems to work OK with the $*.URL pattern!

http://SoSaysSunny.com/icos/cart.ico

Another potential problem is that you appear to be using ICO format files for your icons (mime-type: image/x-icon). This is primarily a Microsoft Windows file format, so they aren't going to display (correctly) on all OS/devices. eg. These won't display at all on Android/iOS (iPhone/iPad) if that is a concern. It would be preferable to use PNG (or GIF) files instead.

...to avoid constantly having to update HTML index files

I agree, no-one want's to manually update an index file every time you upload a file. However, the alternative is to have a (PHP) script that generates your directory listing instead of relying on Apache. Whilst this might be more work in the beginning (unless you have a premade script) - this would potentially give you far greater control and a more portable solution.


UPDATE:

... But the *$*.URL modifications still aren't working.

You need to change the order of the directives so that the most specific directives/patterns are first. In other words, you currently have:

AddIcon           http://example.com/icos/url.png         .URL
AddDescription      "<I>(web link)</I>"                     .URL
AddIcon             http://example.com/icos/cart.png        *$*.URL
AddDescription      "<I>(web store link)</I>"               $*.URL
AddAlt          "URL"                           *.URL

The directives need to be reversed, so that the most specific (ie. $*.URL) are first:

AddIcon             http://example.com/icos/cart.png        *$*.URL
AddDescription      "<I>(web store link)</I>"               $*.URL
AddIcon             http://example.com/icos/url.png         .URL
AddDescription      "<I>(web link)</I>"                     .URL
AddAlt          "URL"                           *.URL
  • I added the NS flag and that fixed the .URL calls. I've converted the ICOs to PNGs (thanks for the info). Thanks! ... But the *$*.URL modifications still aren't working. The current .htaccess file is copied to: sosayssunny.com/htaccess.txt – SoSaysSunny Aug 3 '17 at 1:41
  • You need to make sure the most specific directives are first - I've updated my answer. – MrWhite Aug 3 '17 at 10:29

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.