user2040633
user2040633

Reputation: 1

RewriteRule do THIS if image file, or else do THAT

I'm having a hard time having this to work.. I have installed YOURLS wich is a PHP script to shorten urls. In order to work, it needs to have this:

            RewriteEngine On
            RewriteBase /
            RewriteCond %{REQUEST_FILENAME} !-f
            RewriteCond %{REQUEST_FILENAME} !-d
            RewriteRule ^(.*)$ /yourls-loader.php [L]

No problem here. But I also want to use a directory for image hosting that has nothing to do with the PHP script. It would check if the requested url ends with .jpg|.jpeg|.gif|.png and RewriteRule would redirect to /imgshare/$1 I've tried the code below but I get a server error when going to mysite.com/img.jpg but not for the url redirection "mysite.com/y4Jd":

    RewriteCond %{REQUEST_URI} !(\.jpg|\.jpeg|\.gif|\.png)$ [NC]
    RewriteRule ^(.*)$ /yourls-loader.php [L]
    RewriteCond %{REQUEST_URI} (\.jpg|\.jpeg|\.gif|\.png)$ [NC]
    RewriteRule ^(.*\.(jpeg|jpg|png|gif))$ /imgshare/$1 [L]

Upvotes: 0

Views: 4417

Answers (1)

David Ravetti
David Ravetti

Reputation: 2040

This is not the issue, but as a note, your second RewriteCond already matches files with image endings, so there's no need to repeat that match in the RewriteRule. Alternately, there's no need for the RewriteCond since it's redundant to the RewriteRule.

The real issue, however, may be that you have an extra slash in the final rule. $1 will contain the leading slash matched from the original URL so your rule is currently adding two slashes between imgshare and the file name. I would implement the rule like this:

RewriteCond %{REQUEST_URI} \.(jpg|jpeg|gif|png)$ [NC]
RewriteRule ^(.*)$ /imgshare$1 [L]

Upvotes: 0

Related Questions