1

I got the following code in my .htaccess:

Options +FollowSymlinks
RewriteBase /temp/test/
RewriteEngine on 
RewriteCond %{REQUEST_FILENAME} !-d 
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^about/(.*)/$ $1.php [L]
RewriteRule ^(.*)/download/(.*)/(.*)/(.*)/downloadfile/$ file-download.php?product=$1&version=$2&os=$3&method=$4 [L]
RewriteRule ^(.*)/download/(.*)/(.*)/(.*)/$ download-donate.php?product=$1&version=$2&os=$3&method=$4 [L]
RewriteRule ^(.*)/download/(.*)/$ download.php?product=$1&version=$2 [L]
RewriteRule ^subscribe/(.*)/$ subscribe-$1.php [L]
RewriteRule ^subscribe/(.*)/(.*)/$ subscribe-$1.php?email=$2 [L]
RewriteRule ^(.*)/screenshots/$ screenshots.php?product=$1 [L]
RewriteRule ^(.*)/(.*)/$ products.php?product=$1&page=$2 [L]
RewriteRule ^schedule-manager/$ products.php?product=schedule-manager&page=view [L]
RewriteRule ^visual-command-line/$ products.php?product=visual-command-line&page=view [L]
RewriteRule ^windows-hider/$ products.php?product=windows-hider&page=view [L]
RewriteRule ^(.*)/$ $1.php [L]

everything work perfect. I would like to know how can I modify it so it will be less lines. I am pretty sure I can atleast remove 4-5 lines, but I dont know how. (merge the schedule-manager, visual-command-line and windows-hider, and some more).

I know that the order of the rules is important, this order works - although I have no idea why, I just played with the rules until it worked. If you think that there'll be a bug with the following order please tell me where.

Another thing - I would like to redirect for example www.myweb.com/products.php to www.myweb.com/products/ (I mean that the URL in the address bar will change).

I dont know if the redirect can go along with my rewrite rules.

Thank you.

EDIT: Deleted the RewriteRule of the products (what Orbling mentioned) and changed 1 rule

Ron
  • 113
  • 4

1 Answers1

0

The order is important because you need to ensure that rules that match specifics come before general rules that would also catch the specifics but should not.

So, for instance, the following double parameter lines:

RewriteRule ^newsletter-confirm/(.*)/$ newsletter-confirm.php?email=$1 [L]
RewriteRule ^newsletter-remove/(.*)/$ newsletter-remove.php?email=$1 [L]
RewriteRule ^(.*)/screenshots/$ screenshots.php?product=$1 [L]

Need to come before the generic double parameter:

RewriteRule ^(.*)/(.*)/$ products.php?product=$1&page=$2 [L]

Otherwise they would match as a product (with page) request. As long as you order with that priority in mind, it should work acceptably.

You could refactor the rules to make them more concise, but probably at the cost of readability. The only rule that is redundant is the last one, as the penultimate line catches it.

RewriteRule ^(.*)/$ $1.php [L]
RewriteRule ^products/$ products.php [L]

products/ is a specific case of the generic (.*)/, so would be matches by the generic before processing ever past to that last rule. Fortunately it still works as the keyword is fortunately the same as the filename.

Orbling
  • 298
  • 5
  • 8
  • Thank you about the information. About the products - somehow RewriteRule ^(.*)/$ $1.php [L] didnt work when I went to www.myweb.com/products/ because I got folder with the same name, so the server thought that I want to go to index.php inside products. but, now when u mentioned it - the RewriteRule ^(.*)/$ $1.php [L] comes before the products rule and it did work - no idea why. About the minification - I dont care if the rules will be readable - I just want to have less lines. – Ron Jan 31 '11 at 02:08
  • @Ron: Why do you wish to have less lines? The performance change would be negligible. If you have the `RewriteCond %{REQUEST_FILENAME} !-d` statement, then you need to have no directories that match the URL paths you wish to have, otherwise you will get a conflict. – Orbling Jan 31 '11 at 03:53
  • less lines make it looks more proffesional, and although the performance change would be negligible, every less byte that the server need to load the better for me. – Ron Jan 31 '11 at 11:51
  • @Ron: Well, to each their own, but as a professional, I can say that less is not necessarily more. Having said that, using a front controller and putting all calls in to PHP to handle is usually a good idea. – Orbling Jan 31 '11 at 12:47