Question

Building on this question: .htaccess redirect if folder name in url I'd like to know how to apply the .htaccess rules regarded there, in the .htaccess of the webroot of the CakePHP app. without generating server errors or missing the CakePHP rewrite functionality.

My CakePHP version is 1.2.10 and the .htaccess of the webroot is as follows:

#app/webroot/.htaccess
<IfModule mod_rewrite.c>
    RewriteEngine On
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteRule ^(.*)$ index.php?url=$1 [QSA,L]
</IfModule>
AddType audio/mpeg mp3

I need this because the application has an audio folder that has sub-folders with mp3s files that the application play them using . I want when some one try to access a mp3 file directly or from external website to be redirected to another page, but keeping the able to play the file from my website page. So suppose the following:

http://mycake.com/audio/folderX/fileY.mp3 I want any access to http://mycake.com/audio/* to be redirected to http://mycake.com/mistakeSound.mp3

Was it helpful?

Solution 2

I found the solution which is depend on placing .htaccess file in the regarded audio folder. The syntax of that .htaccess should be as follows:

RewriteEngine on
RewriteCond %{HTTP_REFERER} !^http://(www\.)?mycake\.com [NC] 
RewriteCond %{HTTP_REFERER} !^http://(www\.)?mycake\.com.*$ [NC] 
RewriteRule \.(gif|jpg|mp3)$ - [R=401]
ErrorDocument 401 /e401.html

When someone try to access any mp3 file via, for example, http://mycake.com/audio/folderX/fileY.mp3. A 401 error will be generated.

However, I noticed that FDM is able to access and download this file. The redirect works only for web browsers.

OTHER TIPS

In a htaccess file the order of the rules is important, if the rules and conditions apply to the same requests. If the request URLs are processed by different rules, you can place the rules in any order.

In your case, you have real files in the other question and non-existing files and directories here in this question

RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f

This means, you can insert the other rules before or after the CakePHP rules.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top