1

Please suggest me what to put into .htaccess file.

I have PNG, GIF, JPG images on server http://domain.tld/images/anyimage.anyextension

Want to make URLs more friendly like a http://domain.tld/anyimage.anyextension


This I have now. First two strings change links as described. But last string doesn't change it back for server.

RewriteCond %{REQUEST_URI} ^/images/(.+)(\.gif|\.jpg|\.png)$ [NC]
RewriteRule ^image/(.+)$ /$1 [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^/(.+)$ /images/$1 [L]

If I add

RewriteCond %{REQUEST_URI} ^/([^/.]+\.(png|gif|jpg))$ [NC]
RewriteCond %{DOCUMENT_ROOT}/image/%1 -f
RewriteRule ^([^/.]+\.(png|gif|jpg))$ /image/$1 [L,NC]

Right after previous query string rule then images don't open. If before there's no problem. What it could be? Do you have an idea how to fix it? The last string RewriteRule ^/?(.+)$ /?$1 [L] cause this conflict

RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /\?([^\ ]+) [NC]
RewriteRule ^$ /%1? [R=301,L]
RewriteCond %{QUERY_STRING} ^$
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^/?(.+)$ /?$1 [L]
tshepang
  • 12,111
  • 21
  • 91
  • 136
andrew
  • 426
  • 8
  • 18

1 Answers1

1

Not sure how that makes it more friendly, aside from it being shorter. You can try adding this to the htaccess file in your document root:

RewriteEngine On
RewriteCond %{REQUEST_URI} ^/([^/.]+\.(png|gif|jpg))$ [NC]
RewriteCond %{DOCUMENT_ROOT}/images/%1 -f
RewriteRule ^([^/.]+\.(png|gif|jpg))$ /images/$1 [L,NC]

Then you can change all of your links from http://domain.tld/images/anyimage.anyextension to http://domain.tld/anyimage.anyextension

The first condition checks to make sure the request is for anyimage.anyextension, as long as the extension is a case-insensitive png, gif, or jpg.

The second condition checks to make sure the requested image actually exists in the /images/ directory.


RewriteEngine On
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /images/(.+)(\.gif|\.jpg|\.png) [NC]
RewriteRule ^image/(.+)$ /$1 [R=301,L]

This redirects the browser to the non-/image/ URL. There's 2 completely different things going on here. One deals with the browser and the other deals with content on the server. See the top part of this answer that explains how they are different.

The rules that you have won't work. First:

RewriteRule ^/(.+)$ /images/$1 [L]

will never match. URI's used to match against the regex of a RewriteRule in htaccess files have the leading slash removed, so no URI is going to start with a /. You need to get rid of it.

Secondly, once you do, you'll get a 500 internal server error because your rules will cause an internal infinite loop. You need to match against %{THE_REQUEST} to ensure that a browser is actually requesting a URL with the /images/ path in it, not what has been internally rewritten.

Community
  • 1
  • 1
Jon Lin
  • 142,182
  • 29
  • 220
  • 220
  • Don't know why but it doesn't change them from http://domain.tld/images/anyimage.anyextension to http://domain.tld/anyimage.anyextension after applying this rule. I worked out by myself code that change links this way but can't work out code to change it back to server. Please see end of my post edited. – andrew Aug 15 '12 at 04:37
  • So my method will not work at all? Jon I checked again your method, seems it's working. You are the God! Did right what I wanted. Simply I didn't understand first time the principle of your method. Thanks a lot again! Do you remember I asked you about conflict between removing .html extensions and query string rules? I described that as a separate question as said. Please could you help there also: [link](http://stackoverflow.com/questions/11963856/removing-html-extension-rules-conflict) – andrew Aug 15 '12 at 06:01
  • 1
    @user1586029 I took a quick look at that but I have no idea why SSI isn't working. I'll post something on that question if I see something wrong. In the mean time, if these answers are correct, you should [accept them as answers](http://meta.stackexchange.com/a/5235/171219). It's the way this site works, people are less likely to help someone who has a low accept % and accepting answers marks the question as resolved so when others run into the same problem they know that there was an answer. – Jon Lin Aug 15 '12 at 06:08
  • Yeah, I searched how I could accept answers. I'm new here. I've just already accept previous answer and will accept soon this. – andrew Aug 15 '12 at 06:14
  • Jon please could you see my first post again? There's one little problem I edited it to show you what's the problem. Maybe you know a fix for that. – andrew Aug 15 '12 at 06:19
  • 1
    @user1586029 2 regular expressions are matching the same thing. `^/?(.+)$` matches images as well, so it needs to be **after** these image rules, there's nothing you can do about it unless you make a clear distiction between what letters/numbers/characters make up an image name vs a page name, and they must be mutually exclusive. Or just put the image rules before the query string rules and problem solved. – Jon Lin Aug 15 '12 at 06:29
  • Thank you! You're so good in explanation. I didn't forget tick the great answer by the way) – andrew Aug 15 '12 at 06:38