Forum Moderators: phranque

Message Too Old, No Replies

RewriteCond

RewriteCond

         

agriz

10:27 am on Mar 20, 2010 (gmt 0)

10+ Year Member



Hi,

I have a site for books.
So i am using virtual directory name in websites.

ex: http://www.example.com/books/index.php
books directory is not available.

Now i need to check whether a file exist on server
http://www.example.com/books/isbn/i.php

To check the above link is valid,

RewriteCond isbn/%{REQUEST_FILENAME} -f
RewriteRule ^books/isbn/([^@]*).php$ isbn/$1.php [L]

But this line is not working.
Can you tell me what is the mistake i have done?

[edited by: jdMorgan at 10:32 pm (utc) on Mar 20, 2010]
[edit reason] example.com [/edit]

agriz

6:49 pm on Mar 20, 2010 (gmt 0)

10+ Year Member



:-)

jdMorgan

10:41 pm on Mar 20, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



The problem is that %{REQUEST_FILENAME} is the full Apache server filepath, so you are likely checking something like "isbn/var/users/<your-site-name>/http/www/books/isbn/1234.php to see if it exists... which of course it cannot, because the Apache server's document root is not in a folder called "/isbn".

This may not be exactly right, but I don't see a consistent example above of a URL and it's associated filepath, so this may only be close to correct:

RewriteCond %{DOCUMENT_ROOT}/isbn/$1.php -f
RewriteRule ^books/isbn/([^.]+)\.php$ isbn/$1.php [L]

This essentially checks the same path that you are about to rewrite to.

Jim

agriz

11:09 pm on Mar 20, 2010 (gmt 0)

10+ Year Member



Hi,

Thanks for your help.
It works!

I thought REQUEST_FILENAME just contains the the file's name. It is really good explanation about %{REQUEST_FILENAME}

I have one more doubt. It is just to know.
If i have folders like isbn1,isbn2,isbn3 etc, can i write loop or something in the condition and in rewriterule?

RewriteCond %{DOCUMENT_ROOT}/isbn/$1.php -f
RewriteRule ^books/isbn/([^.]+)\.php$ isbn/$1.php [L]

Thanks for your help again.

agriz

12:18 am on Mar 21, 2010 (gmt 0)

10+ Year Member



Hi,

I tried this. But again no use.
RewriteCond %{DOCUMENT_ROOT}/isbn([^.]+)/$1.php -f
RewriteRule ^books/isbn/([^.]+)\.php$ isbn%1/$1.php [L]

jdMorgan

3:08 am on Mar 21, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



The value on the "left side" of a RewriteCond may not contain a pattern, only strings and/or variables.

There is little support for programmatic looping in .htaccess. It is not a sequentially-executed program or script, but rather a list of directives to be parsed by each of the Apache modules in turn.

How far does your "isbn1,isbn2,isbn3" sequence of directories go? If you mean that you have 20 or fewer "isbn" directories, then you could use a list:

RewriteCond %{DOCUMENT_ROOT}/$1/$2.php -f
RewriteRule ^books/(isbn1|isbn2|isbn3)/([^.]+)\.php$ /$1/$2.php [L]

If you mean that these isbn directories are sequentially-numbered from 1 to 23 (for example), then you could match "isbn" as a literal, and match the "numbers" as numeric characters:

RewriteCond %{DOCUMENT_ROOT}/isbn$1/$2.php -f
RewriteRule ^books/isbn([1-9]|1[0-9]|2[0-3])/([^.]+)\.php$ /isbn$1/$2.php [L]

However, if "isbn" is not a literal you have more than a few dozen such directories, then you need to consider changing your method of mapping URLs to scripts to products. That might be a good idea anyway, since it appears that you have a script for each "product" in each "isbn" directory, and that is hardly a sustainable approach for maintenance and growth of your site. A single-script-with-database (or CMS) approach would be probably be a lot easier to maintain and to grow. Instead of adding a script for each product, you'd add a database entry. And instead of checking for 'file exists' you'd just rewrite all 'product' requests to the script, and the script would check to see if a database entry existed for that 'product number'. If not, the script itself could return a 404 response, or it could return an offer to get that product if the viewer wanted to request it. I'm not a database expert, but this would seem to be a very good application for PHP+MySQL.

Jim

agriz

5:52 am on Mar 21, 2010 (gmt 0)

10+ Year Member



Hi,

I tried this
RewriteCond %{DOCUMENT_ROOT}/isbn$1/$2.php -f
RewriteRule ^books/isbn/([^.]+)\.php$ isbn$1/$2.php [L]

Because the url will be always website/books/isbn/filename

But it should search in multiple isbn folders.
isbn1,isbn2

Is there anything wrong in the above coding. It is not working for some reason.

I like your idea using PHP Mysql. I am going to convert it to mysql. That is the best way to do i think.

Until i move everything to mysql, I want to use the above htaccess code. Can you advice me on this?

Thanks for you help again.

g1smd

5:02 pm on Mar 21, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



In your latest code $2 will be undefined, always blank.

jdMorgan

5:23 pm on Mar 21, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I posted two versions of syntactically-correct code above.

If you have questions about this code, please ask before changing it.

If the URL always contains "/isbn/" but you need to check multiple file directories, then the code will be different -- and far less efficient. But you haven't said that yet.

Jim