htaccess doesn't load new js/css from another vhost - apache

Ok, so here it is!
I have two virtual hosts, named main.aaa.com and static.aaa.com.
I have a htaccess rule in main.aaa.com that for each path that contains js|css|img|ico will load them from static.aaa.com.
This is the htaccess for main.aaa.com:
Options +Indexes
Options -MultiViews
Options +FollowSymLinks
# Turn on the RewriteEngine
RewriteBase /
RewriteEngine On
#
# Rules
#
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule !\.(css|js|ico|img)$ index.html [PT,L]
RewriteRule ^(.+\.(css|js|img|ico))$ http://static.aaa.com/$1 [L]
What's going on is that everything works perfectly, absolutely perfect, but...
if I create a new file in static/js folder and try to load it in main.aaa.com index file, it just gives a 200 OK, file not changed, and by looking into it it seems that htaccess doesn't even bother to look for the file in static vhost.
I have changed file permissions to 755, all of the files have the same permissions and the same owner, only that the new ones that I create are not loaded.
Any help would be much appreciated! Thank you!
[EDIT]: I tried also to rename a file that is now correctly loaded, because I thought I am missing something about file permissions. Stopped working. Renamed it back to previous name and it loads. So, I am thinking, is there a history of files or something that I can clear? I am at the point to break something, anything!
Using ubuntu 14.04 LTS and apache 2.4 if that helps.

I have found a solution to my problem, I don't really understand why it works, I just hope it does not introduce any other problems when I push my files to my hosting.
So here it is, if anybody is as stupid as me :), I have changed and moved this line:
RewriteRule ^(.+\.(css|js|img|ico))$ http://static.aaa.com/$1 [L]
above this line
RewriteRule !\.(css|js|ico|img)$ index.html [PT,L]
the final result being this one:
RewriteRule ^(.+\.(css|js|img|ico))$ http://static.aaa.com/$1 [R,L]
RewriteRule !\.(css|js|ico|img)$ index.html [PT,L]
Good luck!
EDIT: maybe the answer is that redirects, with R flag, must be written first. Maybe.

Related

Htaccess RewriteRule for maping urls to be served from one file

I had htaccess which worked for many years with a command like this:
RewriteRule ^products/(.*).php product.php?page=$1&%{QUERY_STRING}
Mapping all items under products folder to be served with product.php file
Today, suddenly all URLs started giving 404. After many hours of digging, I found that the command now works only if there is an actual file (even empty file - it doesn't matter) under the products folder. For example, products/p1.php would work only if p1.php resides under the products folder.
I also run a test and added:
RewriteRule ^tests/(.*).php tests/index.php
and an index.php file under tests folder with hello world. It will only work for files that actually in tests folder. tests/testing.php will show index.php content only if there is a file testing.php in tests folder.
Does anybody have an idea what could have changed at the server configuration to cause this or if there is a way to fix my command to work without an actual file in the location of the URL?
Edited 1st of November 2018:
I found this in the httpd.conf:
<IfModule proxy_fcgi_module>
<FilesMatch \.(phtml|php[0-9]*)$>
SetHandler proxy:unix:/opt/cpanel/ea-php70/root/usr/var/run/php-fpm/.sock|fcgi://mydomain.com
</FilesMatch>
RewriteCond %{REQUEST_FILENAME} \.php$
RewriteCond %{DOCUMENT_ROOT}/%{REQUEST_URI} !-f
RewriteRule (.*) - [H=text/html]
</IfModule>
Could that be the reason?
Another Update:
So this line
RewriteRule ^tests/(.*) tests/index.php
Will work for existing files and also for non-existing directory.
so tests/dir1/ will redirect fine. But test/file.php will only redirect if file.php actually exist.
One more update (sorry I'm debugging it and finding our more stuff):
The redirect will fail only for PHP files! all other files will work correctly.
Final Solution:
These three lines in httpd.conf need to be commented.
RewriteCond %{REQUEST_FILENAME} \.php$
RewriteCond %{DOCUMENT_ROOT}/%{REQUEST_URI} !-f
RewriteRule (.*) - [H=text/html]
I suspect they are auto-generated by some Cpanel updated and will try to report this to them.
Thank you
yshaool I have the exact same problem with you. My search for a solution lead me also to the httpd.conf.
I commented out the 3 Rewrite lines and restarted Apache. Now it works OK. I'm afraid that this is auto generated file and it will overwritten some time.
At first you need to make life more clearer and stop further rules execution by [L] option:
RewriteRule ^products/(.*).php product.php?page=$1&%{QUERY_STRING} [L]
If doesn't help - there will be rules before yours quoted one executed with a priority.
Something with
RewriteCond %{REQUEST_FILENAME} !-f
condition.
Check your .htaccess if you have those rules.
If you control /etc/apache2/ or /etc/httpd/ folder - check root webserver configs for those.
Otherwise you need to contact your hosting provider.

Setting up .htaccess with apache and laravel

This seems to have been asked hundreds of times, and I read it and tried different things. Nothing seems to work, so after reading about 5 google pages about this I gave up and I want to see if anyone can give me an answer.
I want to remove the /public from my URL
I'm using an apache 2.4 server on windows. I just installed laravel.
I made sure the rewrite module is on in apache (php's get_apache_modules() shows it) and now I'm trying to write the .htaccess - and I can't understand how it works.
I don't want to change anything in my apache configurations (I'm using this machine to develop multiple apps)
my current .htaccess files are :
in the app route directory
Options -MultiViews
RewriteEngine On
RewriteRule ^ public/index.php [L]
and in the public folder
Options -MultiViews
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ /texteditor/public/index.php [L]
Right now localhost/texteditor/ - and anything after it that isn't public/ gives me 404
Also can anyone recommend an easy .htaccess tutorial for dummies?
I have done this thing by doing following and working fine.
Please do this if it is helpful to you.
Move public/index.php to www/texteditor/index.php.
also Move public/.htaccess to www/texteditor/.htaccess.
then you can do it.
Please replace your app root directory's .htaccess with public/.htaccess.

My htaccess RewriteRule works on localhost/mysite but not at mysite.com on a 1&1 shared host

Solved:
unfortunately, the solution is not a satisfying one. This morning, when trying #Wige's Suggestion, I found, to my suprise, that the Expected values WERE infact sent to the page as a GET query. Apparently, 1&1 (who I know have been making changes to their environment this last couple weeks), did something behind the scenes which magically fixed my problem, and now all of my previously unworking code is working as originally expected.
New info: The Apache version of the production server is 1.3.34 vs 2.2.21 on my localhost.
I'm having trouble figuring out why my RewriteRule is not working properly in production.
RewriteRule ^page/pretty-url/(.*)$ page.php?query=$1 [L]
In my local testing environment (localhost/mysite/page/pretty-url/{...}) it works fine, but on mysite.com/page/pretty-url/{...} it doesn't work properly. It loads page.php as expected but apparently the ?query=$1 piece is ignored ($_GET is empty)
I imagine that the problem is somehow related to the server configuration. I'm on a 1&1 shared hosting account with no httpd.conf access.
What that RewriteRule does (or should do):
I want urls like
*example.com/page/pretty-url/{{info_for_dynamic_content}}
to be rewritten to
*/page.php?query={{info_for_dynamic_content}}
So I can access info_for_dynamic_content
within php as $_GET['query']
The full .htaccess file for reference:
AddHandler x-mapp-php6 .php
DirectoryIndex index.php
ErrorDocument 404 /index.php
Options +FollowSymLinks
# per #Jacques Chester's suggestion
Options -MultiViews
RewriteEngine on
RewriteBase /
# the rule in question
RewriteRule ^page/pretty-url/(.*)$ page.php?query=$1 [L]
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ $1.php
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php !-f
RewriteRule (.*) /index.php [L]
Most likely, your host is storing the variables somewhere else. I would add a call to phpinfo(); into your script and go through the environment variables there to see if you can find the values that should have been in get.
Seems to be connected to 1&1's hosting environment.
See this question, in particular, this answer.
Basically it appears that 1&1 enable "MultiViews". By adding
Options -MultiViews
You disable that setting for your website and according to various reports, this resolves the issue.
I struggled with RewriteRule issues on a 1&1 / 1and1 / IONOS shared server for WEEKS and eventually I found the perfect setup for a 1 and 1 shared server, start your .htaccess file like this
Options -MultiViews
Options +FollowSymlinks
RewriteEngine On
RewriteBase /
I hope this helps someone as 1&1 are useless when it comes to htaccess support

.htaccess mod_rewrite subdirectory to URL parameter

I hope this was not asked over and over again before, but I didn't get further to an answer using google, w3schools and so on. So here is my question:
I'm writing a script that creates kind of an index of projects that I have on my homepage and makes a nice list with images and teaser text based on an info file. I mostly have my projects on github and the readme is in markdown so I thought I could dynamically generate the HTML from the markdown of the latest blob from github on demand using PHP so it gets updated automatically.
My directory structure looks like this:
projects
project1
.remoteindex
.info
project2
.remoteindex
.info
index.php
.htaccess
So when just domain.tld/projects/ is requested I get the info from .info and make a nice index of all projects. But if domain.tld/projects/project1/ is request it, I want to internally redirect it to domain.tld/projects/?dir=project1 to do my markdown parsing and so on. But domain.tld/projects/project1/image.png should not be redirected.
This is what I tried:
.htaccess
RewriteEngine on
RewriteRule ^([^/]+)/?$ index.php?dir=$1 [R,L]
I made it redirect instead of rewrite so that I can see what the error is because I just got an 404. The URL that I get redirected to is domain.tld/home/www/web310/html/projects/index.php?dir=project1 so obviously there is something going wrong with the internal structure of the web server an paths an whatever.
I hope you can understand my problem an I would be very pleased if someone could help me, because I'm totally lost on .htaccess anyway.
Edit:
See my answer below for the used .htaccess.
The strange thing is that if I have an index.html in on of the subdirectories, my local web server (Apache with XAMPP for Mac OS X 1.7.3) does not rewrite and the index.html gets displayed, without one it works correctly.But on my real web server that serves my homepage it rewrites both with and without index.html (which is what I want). Any hints on that?
Thanks for all the help so far! You guys are just awesome!
I figured out that a symbiosis of both of your solutions works well for me:
RewriteEngine on
RewriteBase /projects
RewriteCond %{SCRIPT_FILENAME} !-f
RewriteCond %{SCRIPT_FILENAME} -d
RewriteRule ^([^/]+)/?$ index.php?dir=$1 [QSA,L]
Of course only without [R], this was my fault. (See my question edit for another question please).
You need to add a RewriteBase /projects to the htaccess.
That way the redirect will work properly.
Edit:
RewriteEngine on
RewriteRule ^index.php - [L]
RewriteRule ^([^/]+)/?$ index.php?dir=$1 [R,L]
Following what you ask, this is important not to do a redirect, but let the rewriterule modify internally the URL i.e. the RewriteRule should not end with [R,L] but rather [L] and maybe the "query string append" directive to keep what's after the ? so this should probably be [QSA,L].
Now here's how I'd do to avoid rewriting static files: if it's not a file then (and only then) test it:
# if it's not a file...
RewriteCond %{SCRIPT_FILENAME} !-f
# ... and it's a dir
RewriteCond %{SCRIPT_FILENAME} -d
# ... then rewrite it internally and stop further processing:
RewriteRule projects/([^/]+)(/?)$ index.php?dir=$1 [QSA,L]
And now two hints:
Please try to use the RewriteLog directive: it helps you to track down such problems:
# Trace:
# (!) file gets big quickly, remove in prod environments:
RewriteLog "/web/logs/mywebsite.rewrite.log"
RewriteLogLevel 9
RewriteEngine On
My favorite tool to check for regexp:
http://www.quanetic.com/Regex (don't forget to choose ereg(POSIX) instead of preg(PCRE)!)

Simple Apache .htaccess RewriteRule

I basically want each request to
http://localhost/~lucamatteis/datadict/AccessionMainName
to actually call
http://localhost/~lucamatteis/datadict/index.php/AccessionMainName
Here's my current .htaccess physically located under ~lucamatteis/datadict/
RewriteEngine on
RewriteBase /~lucamatteis/datadict/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L]
But unfortunately this doesn't work. I've tried everything. At this point I think the issue is that It's using the tilde, or I'm not sure.
In the phpinfo() under the loaded modules section mod_rewrite is loaded. I'm not sure where else to look as I might need to load an extra thing for mod_rewrite to work.
Any ideas?
First of all make sure that you're able to override Apache configuration trough .htaccess file ( AllowOwerride config directive).
Then try to remove RewriteBase directive totally. In this case the current directory will be used as base.
Change you ReWriteBase to your physical directory:
/home/lucamatteis/public_html/datadict (or something similar)
http://httpd.apache.org/docs/1.3/mod/mod_rewrite.html#RewriteBase
I think the problem is that you want to do an internal redirection to a file that does not exists:
/home/lucamatteis/public_html/datadict/index.php/AccessionMainName
This will only work if index.php is a directory and AccessionMainName is a file.
I don't think such thing can be done. Most of the times, using query string parameters is all you need:
index.php?AccessionMainName
... and you read it from $_GET['AccessionMainName'].