homepage Welcome to WebmasterWorld Guest from 54.226.213.228
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Become a Pro Member

Home / Forums Index / Code, Content, and Presentation / Apache Web Server
Forum Library, Charter, Moderators: Ocean10000 & incrediBILL & phranque

Apache Web Server Forum

    
mod rewrite vs files vs virtual subdirectories
ray365




msg:4578239
 4:25 pm on May 27, 2013 (gmt 0)

I have a site with virtual subdirectories (for languages) created by the means of mod_rewrite. They work fine. Now I need to upload some files that should be accessed via certain subdirectories only.

Currently I can access any files via any subdirectories.

site.com/file.txt
site.com/jp/file.txt
site.com/es/file.txt

site.com/another_file.txt
site.com/jp/another_file.txt
site.com/es/another_file.txt

site.com/third_file.txt
site.com/jp/third_file.txt
site.com/es/third_file.txt


I need to access these files via the related subdirectories only, if I try to access them via unrelated directory, it should throw the 404 error.

site.com/file.txt - for english (the only language that does not have a subdirectory)
site.com/jp/anoter_file.txt - for japaneese
site.com/es/third_file.txt - for Spanish.


Please note that there is no pattern in file names and there are many languages. So, it is necessary to create separate rules for each file/subdirectory.

As there are many languages and files, it could be a nightmare to write rules for each language like this:

e.g. I want file another_file.txt to be displayed inside the jp directory only. So, I have to exclude it like this:

RewriteRule ^de/another_file.txt - [R=404]
RewriteRule ^fr/another_file.txt - [R=404]
RewriteRule ^es/another_file.txt - [R=404]


But I think there is a more elegant solution.
I created the following code:

RewriteRule ^(?!jp)../another_file.txt - [R=404]

It throws 404 error if I try to access this file using other subdirectories:

site.com/fr/another_file.txt - 404 error as expected
site.com/es/another_file.txt - 404 error as expected


But it does not work for the root directory:
site.com/another_file.txt - opens the file - not expected.
Please advise how to fix it.

Thanks in advance.

 

Dideved




msg:4578279
 5:32 pm on May 27, 2013 (gmt 0)

It would be helpful if you could show an example of your public www filesystem, and if you could show the current rewrite rules you have in place.

Right now, I can only speculate that if site.com/some_language/file.txt doesn't exist, then a rewrite rule will try to fetch it from English. Though, some other details make the fallback mechanism seem more complicated than that.

RewriteRule ^(?!jp)../another_file.txt - [R=404]


This might be easier with a rewrite condition.

RewriteCond $1 !=jp
RewriteRule ^(?:([^/]+)/)?another_file\.txt$ - [R=404]


The rule pattern will match any language (or no language), followed by some file to exclude, and the condition checks whether the language was "jp" or whatever you want it to be.

ray365




msg:4578292
 6:19 pm on May 27, 2013 (gmt 0)

Dideved, thank you for your message.

It would be helpful if you could show an example of your public www filesystem, and if you could show the current rewrite rules you have in place.


Unfortunately, the site is hosted in a private network so far.

Please let me explain in details.

All the language subdirectories are virtual and created by this rule:

RewriteRule ^(jp|cz|ru|ua|de|fr|it|es|br|cn)\/(.*)$ $2 (this is the only rule in the .htaccess).

This code works fine.

The English version of the site is the only version without a subdirectory. (i.e. it is just site.com)

All the mentioned *txt files are located in the root directory.

These files should be opened via the related language subdirectories only.

This code:

RewriteRule ^(?!jp)../another_file.txt - [R=404]

Works almost exactly as I need.

I cannot open this particular file via other subdirectories (cz/cn/ru/de/fr etc).
I can open it via jp subdirectory as expected.
But I can also access it via english version of a site as well: site.com/another_file.txt - this is not correct.

So, I'm asking how to prohibit accesing the file using the english version of the site (without a subdirectory) as well.

Unfortunately, the code you sent me does not work at all: it just throws 404 error regardless the path.

Dideved




msg:4578311
 7:28 pm on May 27, 2013 (gmt 0)

So it sounds like the English files at the web root are the only real files, correct?

I would do this: First, change the language virtual directory rule so that it will perform the replace *only* if the request does not already translate to a real file.

RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(jp|cz|ru|ua|de|fr|it|es|br|cn)\/(.*)$ $2

Then, you can create a real "jp" directory, and in it put the real "another_file.txt". Since the file won't live at the root with the other English files, it won't be accessible by any other language code, not even English. This solution comes with the added benefit that you don't need to exclude each and every file through htaccess.

ray365




msg:4578324
 8:06 pm on May 27, 2013 (gmt 0)

You're GENIUS!

This is exactly what I need.

Thank you so much for your help.

phranque




msg:4578427
 2:12 am on May 28, 2013 (gmt 0)

welcome to WebmasterWorld, ray365!

g1smd




msg:4578513
 9:28 am on May 28, 2013 (gmt 0)

RewriteRule ^(jp|cz|ru|ua|de|fr|it|es|br|cn)\/(.*)$ $2

Slashes in RegEx pattterns should not be escaped.
This rule needs the [L] flag.
The $2 should be preceded with at least a leading slash.

Global Options:
 top home search open messages active posts  
 

Home / Forums Index / Code, Content, and Presentation / Apache Web Server
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Home ¦ Free Tools ¦ Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About ¦ Library ¦ Newsletter
WebmasterWorld is a Developer Shed Community owned by Jim Boykin.
© Webmaster World 1996-2014 all rights reserved