skip to Main Content

At the root of the web server are audio files. Files can be in wav or mp3 format. I am trying to redirect to a file with the same name, but with a different extension. For example, if there is no file with the name hello.wav, redirect to hello.mp3. My attempts were on the example of one answer about html and php. Please correct me!

Server version: Apache/2.4.25 (Debian 9.8)

my .htaccess:

Options -Indexes
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*).wav$ $1.mp3 [L]

For example, if there is no file with the name hello.wav, redirect to hello.mp3. in browser i am write http://domain.kz/hello.wav and should get http://domain.kz/hello.mp3

2

Answers


  1. Chosen as BEST ANSWER

    I made a simple mistake in not having completely checked all the settings of the web server. I turned on the modules "rewrite" and "userdir"

    sudo a2enmod rewrite
    sudo a2enmod userdir
    

    and edited AllowOverride for /var/www/

    <Directory /var/www/>
       Options Indexes FollowSymLinks
       AllowOverride **All**
       Require all granted
    </Directory>
    

    and restart apache

    systemctl restart apache2
    

  2. I don’t know if your problem is solved, but I found a lead that might help you.
    https://httpd.apache.org/docs/2.4/en/rewrite/flags.html#flag_s

    # Does the file exist?
    RewriteCond "%{REQUEST_FILENAME}" "!-f"
    RewriteCond "%{REQUEST_FILENAME}" "!-d"
    # Create an if-then-else construct by skipping 3 lines if we meant to go to the "else" stanza.
    RewriteRule ".?" "-" [S=3]
    
    # IF the file exists, then:
        RewriteRule "(.*.gif)" "images.php?$1"
        RewriteRule "(.*.html)" "docs.php?$1"
        # Skip past the "else" stanza.
        RewriteRule ".?" "-" [S=1]
    # ELSE...
        RewriteRule "(.*)" "404.php?file=$1"
    # END
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search