Use .htaccess to transparently manage versions of site depending on extension - apache

After reading and trying a lot of tutorials and howtos... I have not found a way to do this using .htaccess ;-P
I have this folder structure:
somepath/mywebPHP_v314/ (incluing many subfolders with php files)
somepath/mywebJS_v007/ (incluing many subfolders with js files)
somepath/mywebCSS_v876/ (incluing many subfolders with css files)
somepath/mywebJPG_v543/ (incluing many subfolders with jpg files)
somepath/standardhostingfolder/.htaccess (Only file at this folder. No subfolders)
I would like to program .htaccess to get this behaviour:
Depending on the extension of the file, Apache will serve transparently the corresponding file.
E.g. when visiting www.mywebname.com/products/family/somearchive.php Apache will serve transparently somepath/mywebPHP_v314/products/family/somearchive.php (instead of serving the usual somepath/standardhostingfolder/products/family/somearchive.php)
e.g. when visiting www.mywebname.com/products/family/foto3.jpg Apache will serve transparently somepath/mywebJPG_v543/products/family/foto3.jpg
Thanks in advance!
P.S. Just for clarifying: "somepath/standardhostingfolder/" is the server's "web root" folder: If I delete somepath/standardhostingfolder/.htaccess: When visiting www.mywebname.com/products/family/foto3.jpg Apache will try to serve naturally somepath/standardhostingfolder/products/family/foto3.jpg .

This should work for .jpg, you can create 3 other rules accordingly:
RewriteCond %{REQUEST_URI} \.jpg$
RewriteCond %{REQUEST_URI} !^/mywebJPG_v543
RewriteRule (.*) /mywebJPG_v543%{REQUEST_URI} [L]

Related

How to use apache to reroute files to other directory?

I am maintaining a website someone else wrote. He used absolute path in the html to request assets files, and he assumed the project is always installed at the root of the domain.
Now I am testing the website; I installed the project at a subdirectory. And I want to redirect files that start with a certain characters to a subfolder.
For example, he wrote something like this
<img src="/en/wp-content/themes/.....">
So now the server will start searching files at the root like example.com/en/wp-content/....
I installed the project at /foo/en, example.com/foo/en. I want to use .htaccess to reroute urls that start with /en/wp-content/themes to /foo/en/wp-content/themes. What should I do?
This is how I wrote it:
RewriteEngine On
RewriteRule ^en/wp-content/themes/(.*)$ /foo/en/wp-content/themes/$1
But it doesn't work....
I would change the line only a little bit just to be sure that this is not interfering with any other htaccess files use [L] or [END]:
RewriteEngine On
RewriteRule ^/?en/wp-content/themes/(.*)$ /foo/en/wp-content/themes/$1 [L]
[L] means Last https://httpd.apache.org/docs/2.4/rewrite/flags.html#flag_l
[END] https://httpd.apache.org/docs/2.4/rewrite/flags.html#flag_end

apache configuration for react-router with browserHistory and baseName subdirectory

My application is in a subdirectory of the main web site. I have implemented basename and browserHistory and put the following recommended apache rewrite code into .htaccess in the app folder:
RewriteEngine On
RewriteBase /
RewriteRule ^index\.html$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . index.html [L]
This recommended apache rewrite works to return browserHistory URLs to the application index.html. For some reason (see below), it does not find the "style.css" and "brundle.js" that are EMBEDDED in the index page HTML. The only thing that I can see that I have different in the example .htaccess file is that I have a RewriteBase value of "/react_librivox_search" because my application is in that subfolder of the site.
RewriteBase /react_librivox_search
I have tested using various different beginning and ending slashes for paths and files in .htaccess, and the problem is not that.
The problem seems to be that the react application is setting a GET value for the files that includes a part of the PATH variable that is supposedly only a part of the react-router definition:
<Route path="/book/:id" component={BookDataDisplay}/>
Note that the additional path segment "/book/" is being appended to the base URL, and when files are not found in THAT directory (which does not really exist), the server returns "index.html" for the missing files, which accounts for the mime-type error for the librivox_search.css file and the "<" error for the bundle.js file.
The stylesheet http://jstest.dd:8083/react_librivox_search/book/librivox_search.css was not loaded because its MIME type, “text/html”, is not “text/css”. librivox_search.css (embedded in index.html)
SyntaxError: expected expression, got '<' bundle.js (embedded in index.html)
The same unexpected addition of "book" to the URL is at work here as well. Neither of the embedded index.html files is in that subdirectory. But I want to maintain that "book" in the path, since it identifies what KIND of data is being passed to the route . . . which distinquishes it from other kinds of data. I just do not want to have it sent to the SERVER (but perhaps that cannot be avoided), since the actual index.html embedded files are not there.
I suppose rewriting "/react_librivox_search/book" as "/react_librivxo_search" might work, but it seems to be a hacky way to go about it. And I don't want to have to put duplicate bundle.js files in multiple directories (that works, but
what a maintenance nightmare THAT would be, and no doubt bad practice).
Or is it recommended to put a separate .htaccess in a REAL "book" subfolder that returns "librivox_search.css" and "bundle.js" (in the base directory) depending on the file request?

How to return a 410 error with htaccess for all *.html queries?

Some time ago, I had a problem with my server. Using a security hole at Joomla, someone created thousands of files (ending in *.html) in my website.
I've deleted all these files, but Google keep querying this files. I've already added a hundred (or more) of filenames at Google Webmaster Tools to be removed, but there are still lots of files.
What I want, is to add a rule to the .htaccess file to return a 410 error code when any file ending with .html is requested, EXCEPT if the filename is google123456789abcdefg.html (a file from google). The problem is that the html files can be in any (non-existing) folder of the webserver...
Can you help me with this problem? Because I haven't a clue about how the .htaccess file works...
You can use the following rule :
RewriteEngine on
#--exclude "/google12345.html--#
RewriteCond %{REQUEST_URI} !^/google12345abc\.html$ [NC]
#--redirect all .html requests to 410 Gone--#
RewriteRule \.html$ - [R=410,L]

mod_rewrite inserting full path to file

I need to create a rewrite to take traffic going to mp3/mp4 files in a specific subdirectory and then route them to a PHP file that tracks download stats etc before routing them to the actual file location since iTunes requires your podcast RSS contain actual media file extensions (.mp3, .mp4, etc)
I have created rewrites before with no problem but now I am running into an odd issue on this company's server.
My .htaccess located at www.company.com/companytools/podcasts
RewriteEngine on
RewriteRule ^/(.*).mp3$ /test.php?file=$1 [r=301,L]
Right now it is partially working it does act upon the mp3 file but ends up including the full path to test.php after the domain, so I end up with a 404 page looking for this URL:
www.company.com/www/internal/docs/companytools/podcasts/test.php?file=test
basically I need the path, but only the /companytools/podcasts part.
Any help is appreciated.
You may not need R=301 here to hide actual PHP handler.
Try this rule with RewriteBase:
RewriteEngine on
RewriteBase /companytools/podcasts/
RewriteRule ^(.+?)\.mp3$ test.php?file=$1 [L,QSA]

Apache redirect 404 for only the files that are not found?

What I'm looking to do is basically take all requests to a directory, and if the file exists, send it. If not, send it from the parent directory (assume it exists there). The files are large and there can be a lot, and the subdirectories will change frequently, so filesystem links isn't a great way to manage. Is there some Apache config way of doing this? e.g.
/path/file0
/path/file1
/path/sub1/fileA
/path/sub1/fileB
/path/sub1/fileC
/path/sub2/fileA
/path/sub2/fileB
/path/sub2/fileC
So, if a request comes in for /path/sub1/fileB they get /path/sub1/fileB (normal-case). If a request comes in for /path/sub1/file0 they get /path/file0 (special-case).
Or maybe there's a way in PHP, if I could have Apache redirect all requests in one folder to a specific php file that checks if the file is present and if not checks 'up' a folder?
Thanks.
You could use mod_rewrite to do that:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^path/[^/]+/([^/]+)$ path/$1 [L]
This rule will rewrite a request of /path/foo/bar to /path/bar only if /path/foo/bar is not a regular file.
Yes, PHP can redirect to a parent directory.