0

I'm creating a new .htaccess file for my domain, and I want to make sure that all parts of this code are valid, that the right flags are being used, that it would not create any conflicts, and whether the order of these command blocks is ok (is it important at all?)

Also, would you recommend any changes/additions?

# block direct access to folders

Options -Indexes

RewriteEngine on

# make non-www -> www

RewriteCond %{HTTP_HOST} ^(?!www\.)(.+) [NC]
RewriteRule ^(.*) http://www.%1/$1 [R=301,NE,L]

# block about 200 bad agents, and avoid an http 500 error with ENV at first

RewriteCond %{ENV:REDIRECT_STATUS} ^$
RewriteCond %{HTTP_USER_AGENT} ^(badbot1|badbot2|....up-to-badbot200) [NC,OR]
RewriteCond %{HTTP_USER_AGENT} ^$
RewriteRule ^.* - [F,L]

# several types of url rewriting to nice looking urls (about 30)

RewriteRule ^vegetables/healthy-carrot/?$ carrot.php [L]
RewriteRule ^vegetables/delicious-tomato/?$ tomato.php [L]
RewriteRule ^vegetables/great-avocado/?$ avocado.php [L]
RewriteRule ^fruits/nice-apple/?$ apple.php [L]
RewriteRule ^fruits/best-strawberry/?$ strawberry.php [L]
RewriteRule ^sitemap/?$ sitemap.php [L]
RewriteRule ^contact-us/?$ contact.php [L]
RewriteRule ^privacy-policy/?$ privacy.php [L]
RewriteRule ^general/action/?$ /general/process.php [L]

# use cache for images to make site load faster

<IfModule mod_expires.c>
    ExpiresActive On
    ExpiresByType image/gif "access plus 1 month"
    ExpiresByType image/png "access plus 1 month"
    ExpiresByType image/jpeg "access plus 1 month"
    ExpiresByType image/jpg "access plus 1 month"
</IfModule>

Thanks!

rockyraw
  • 1,125
  • 2
  • 15
  • 36
  • Hope the user agents on your rule are not called `badbot1` or it would render useless. You could simplify the several types of URL rewriting with a more dynamic way to reduce the amount of lines and work, aside from that the only thing that would seem to cause a conflict would be your rewrite lines depending on what else you have there and in what order they are. Why are you using `no escape` on the `www` redirect? – Prix Apr 05 '14 at 14:21
  • (a) I think I took it [from one of the answers here](http://stackoverflow.com/questions/234723/generic-htaccess-redirect-www-to-non-www), the author said: "I used NE flag to prevent apache from escaping the query string. Without this flag, apache will change the requested URL `http://www.example.com/?foo%20bar` to `http://www.example.com/?foo%2250bar` , I'm not sure if that's correct or crucial though (b) agents not really called `badbot`, it's just for demonstration (c) how can I simplify URL rewriting? I can't group these rules into only one group so I don't know. (d) nothing else is there – rockyraw Apr 05 '14 at 14:34
  • (c)Without knowing how the folders really are and a roughly real sample of some of the mixed URL you have it would be hard to tell you how, also the question is will file1.php actually lead the user to the right message by the title or whatever is your code properly handling it or you have a single php file for each URL? (a)as for the NE flag that's interesting I never had that happen before so. – Prix Apr 05 '14 at 14:38
  • @Prix I edited my code so the grouping would be clearer, each `file.php` is a unique file, and going to `domain.com/file.php` would be equal to to going to its rewritten url. – rockyraw Apr 05 '14 at 14:52
  • Given the last word is always the name of the php file there is a more dynamic way to do it or at least so it seems from your examples only the last 4 would be unique like that. – Prix Apr 05 '14 at 15:08
  • @Prix well no, it's not necessarily the same; another question though - should I also add the NC flag to these redirects? – rockyraw Apr 05 '14 at 21:09
  • Yes the NC flag is recommended in your case. If the last name was the php file name for instance you could do this: `RewriteRule ^[^/]+/[^-]+-([^/]+)/?$ $1.php [L]` which would dynamically work for all your current 5 rules and in this case no NC would be needed and it would merely take the last name and use it as the php name. That's why I asked for real sample of what you have which would help seeing what can be applicable for your case. – Prix Apr 05 '14 at 21:33
  • I see, though this is just considering that the url are in `word-word` form, and not `word-word-word` form or another, right? why would NC is recommended? can't this cause a duplicate content SEO problem? – rockyraw Apr 05 '14 at 21:41
  • Not really you could to a search from right to left like this `RewriteRule [-/]([^/]+)/?$ $1.php [L]` – Prix Apr 05 '14 at 21:48

0 Answers0