.htacces files doesnt work despite of AllowOverride All - apache

I dont know why but my simple .htaccess file doesnt work on my server.
/var/www/html/.htaccess
RewriteEngine On
#RewriteCond %{HTTP_HOST} ^/$ [R=301,L]
#RewriteRule ^(.*)$ htt://www.onet.pl/$1 [L]
RewriteRule ^/lol$ /dev/public
I tried to make a test with either redirection of all traffic to another server or simple redirection myaddress/lol to myadress/dev/public where i have index.html file.
Modul REWRITE is already enabled
#a2enmod rewrite
Module rewrite already enabled
And this is my /etc/apache2/sites-enabled/000-default.conf
<VirtualHost *:80>
ServerAdmin webmaster#localhost
DocumentRoot /var/www/html
<Directory /var/www/html>
AllowOverride All
</Directory>
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>
When i write rubbish in /var/www/html/.htaccess and open servers main address then it throws error message and in apache logs is see some error about wrong command in .htaccess file.
But neither of redirections placed in .htaccess works. Second redirection does not redirects from adress/lol to address/dev/public - browser just throws that there is no /dev site.
I have root access, server is Debian 8 with apache 2.4.10. What can i do?

The simple issue us that you are trying to use an absolute path with a rewrite rule inside a dynamic configuration file. It is clearly documented that in this case you need a relative path due to the nature of how those stupid dynamic configuration files work (.htaccess).
Take a look at the version using a relative path:
RewriteEngine On
RewriteRule ^lol$ /dev/public
A more intelligent way to handle this discrepancy is to use a pattern that will work in both case, in dynamic configuration files and in the real host configurations:
RewriteEngine On
RewriteRule ^/?lol$ /dev/public
But the best idea would be not to use such dynamic configuration files at all but the http servers host configuration instead. Those files are notoriously error prone, hard to debug and they really slow down the server. They are only provided as a last option for situations where you do not have control over the host configuration (read: really cheap hosting service providers) or if you have an application that relies on writing its own rewrite rules (which is an obvious security nightmare).
Here is the official documentation (which you definitely want to read), take a look at the section labelled "Per-directory Rewrites":
http://httpd.apache.org/docs/current/mod/mod_rewrite.html

Related

Apache subdomain vhost + subfolders of several Magento 2 installs

Basically I want to have several stores under one subdomain as sub-folders. Like this:
mysubdomain.website.com/magento2v1 - Magento 2 store #1
mysubdomain.website.com/magento2v2 - Magento 2 store #2
mysubdomain.website.com/magento2v3 - Magento 2 store #3
Steps that I did:
This subdomain will be run in a VPS server so first I needed to redirect the subdomain (mysubdomain.website.com, the one above) to another IP (the actual VPS server) using an A record.
Second I created a Vhost with the mysubdomain.website.com on that VPS server
This is the vhost on the VPS server, Apache2:
<VirtualHost *:80>
RewriteEngine On
ServerName mysubdomain.website.com
ServerAlias www.mysubdomain.website.com
ServerAdmin webmaster#localhost
DocumentRoot /var/www/mysubdomain
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>
And then in /var/www/mysubdomain I have:
/var/www/mysubdomain/magentov1
/var/www/mysubdomain/magentov2
/var/www/mysubdomain/magentov3
Now the problem is, Magento 2 needs .htaccess redirects where it redirects /pub to the actual sub-folder. I can't seem to get these .htaccess to work. The htaccess is read but it seems like the folders go a little bit crazy.
/var/www/mysubdomain/magentov1/.htaccess
/var/www/mysubdomain/magentov2/.htaccess
/var/www/mysubdomain/magentov3/.htaccess
Here's an example of this .htaccess:
RewriteEngine on
RewriteCond %{REQUEST_URI} !^/pub/
RewriteCond %{REQUEST_URI} !^/setup/
RewriteCond %{REQUEST_URI} !^/update/
RewriteCond %{REQUEST_URI} !^/dev/
RewriteRule .* /pub/$0 [L]
DirectoryIndex index.php
Can anybody help me get this .htaccess to work? I think it's related to the folder structure, the folder base, but I don't know how to do this and didn't found any other example.
Thank you very much!
The issue most likely is that you rewrite to the absolute path /pub/ from within the configuration files in the subfolders. How should that rewritten request be processed? After that rewriting step it appears to the next round of rewriting like a request to https://sub.example.com/pub/........ None of your existing configuration files will get applied to that. And it most likely will lead to a http status 404. Which you should be able to see in your http server's error log file and also in your browser's console network tab.
Instead you should rewrite to the relative path pub/instead. Which would result in the next rewriting step getting applied to the request to https://sub.example.com/magentov2/pub/........ Which would again get (correctly) served from within that subfolder.
In general you should try to keep global rewriting rules in a common place instead of doubling them in various parallel configuration files. Even better than a common distributed configuration file (".htaccess") in the http hosts DOCUMENT_ROOT folder would be to implement such rule in the actual central configuration file. That is faster, more robust, more secure and easier to debug.

How can I get .htaccess to be seen when I make my site live

I have a .htaccess file which (among other things) creates pretty urls. Here's a sample of part of the file:
RewriteEngine On
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteCond %{SCRIPT_FILENAME} !-f
...
# Pretty urls
RewriteRule ^home(\d+)*$ ./index.html
RewriteRule ^contact(\d+)*$ ./contact.html
RewriteRule ^enquire(\d+)*$ ./enquire.html
RewriteRule ^terms(\d+)*$ ./terms.html
RewriteRule ^about(\d+)*$ ./about.html
RewriteRule ^owners(\d+)*$ ./owners.html
I am currently moving the site to new hosting on digitalocean. When I test the site out by browsing the IP e.g.
111.222.333.444/html_docs/contact
the .htaccess file is working perfectly. Rewrite rules are on and the site properly rewrites the url and serves (in this case, contact.html) to the browser.
When I change the nameservers and browse the site by domain after propogation e.g.
mydomain.com/contact
the .htacces file fails to do the redirect, at least, it seems like it's not being seen at all. This is weird to me since it works when I'm browsing by ip. I purposely put some rubbish into the .htaccess to test if it was being picked up. Again, if I browse via the ip I get a 500 which I would expect, but when browsing by domain name I do not get an error but the rewrites don't work.
I'm banging my head at this stage and can't figure it out. Any help appreciated,
many thanks,
Wittner
Ok, this was less of a lack of knowledge about how .htaccess works and more to do with not configuring my vhosts settings properly.
<VirtualHost *:80>
ServerName mydomain.com
ServerAlias www.mydomain.com
ServerAdmin me#mydomain.com
DocumentRoot "/var/www/mydomain.com/html_docs/"
<Directory "/var/www/mydomain.com/html_docs/"> <-- Problem was here
Options Indexes FollowSymLinks
AllowOverride All
Order allow,deny
Allow from All
</Directory>
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
Turns out that in the tag I had left out the path to my domain root. So when I directly browsed the site via the IP the system was able to figure it out, but no while browsing. All my .htaccess stuff is now working. Thanks to all who looked and anyone who replied. Live and learn I guess :-)
cheers,
Wittner

VirtualHost configuration (rewrite) not working when using SSL

I am using the following directives to configure my VirtualHost in Plesk:
[vhost.conf]
ServerName www.mydomain.com
DocumentRoot /var/www/vhosts/mydomain.com/httpdocs
RewriteEngine on
RewriteCond %{SCRIPT_FILENAME} !^.+\.(css)
RewriteCond %{REQUEST_URI} !^.+js
RewriteRule ^(.+)$ /index.php/$1/
<Directory /var/www/vhosts/mydomain.com/httpdocs>
Options Indexes FollowSymLinks MultiViews
AllowOverride All
Order allow,deny
allow from all
php_admin_flag safe_mode Off
</Directory>
This worked fine in every case, until i added an SSL certificate and accessed the server through https. Now the rewrites were no longer working, when calling the site through SSL.
So i figured that the configuration loaded for the other protocol (https) must differ from the one that is loaded in the case of http.
I then found out that Plesk uses two separate config files for both protocols. I copied vhost.conf to vhost_ssl.conf. Now the site loaded fine over https and the rewrites were working fine.
But now the rewrites were no longer working when accessing the site through http.
Seems like the cat is biting its tail, i am running in circles and out of options.
Unfortunately i lack the experience in configuring apache. I do assume that somehow my two sets of rules are causing a problem, but after all they are included into two different <VirtualHost> directives.
Perhaps someone knows what is going on here and how to fix it?
I can't tell you how to fix it but I can tell you how to start.
Look carefully at the access and error logs. Read about this here: https://httpd.apache.org/docs/2.2/logs.html
There are a bunch of tools to help you with this debugging described there and in linked pages.

mod_rewrite: redirect requests from localhost to remote server

I have a following scenario:
Remote server with some webapp running at http://remote/webapp
Local machine inside corporate network
Corporate proxy between them
Apache with mod_rewrite running on my local machine
I would like to have the mod_proxy rewrite every request like http://localhost/webapp?someparams into http://remote/webapp?someparams.
Currently I have the following httpd.conf:
DocumentRoot "C:/Apache2.2/htdocs"
<Directory />
RewriteEngine On
RewriteRule ^(.+) http://remote/$1
Options FollowSymLinks
AllowOverride All
Order deny,allow
Deny from all
</Directory>
Which results in mod_rewrite transforming http://localhost/webapp?someparams into http://remote/C:/Apache2.2/htdocs/webapp?someparams
How to configure mod_rewrite to handle it correctly?
Since it looks like you have access to vhost/server config, you should ditch mod_rewrite and just use mod_proxy:
ProxyPass /webapp http://remote/webapp
ProxyPassReverse /webapp http://remote/webapp
and get rid of the 2 mod_rewrite lines (which is redirecting, not proxying):
RewriteEngine On
RewriteRule ^(.+) http://remote/$1
Note that if you have cookies, you may need to reverse map their domains.using ProxyPassReverseCookieDomain.
Also:
The fact that windows absolute path appears in the URL is due to misconfiguration of the mod_rewrite and this is what I'm trying to avoid
This is not a misconfiguration with mod_rewrite. When you put rewrite rules inside a <Directory>, the filepath is used in the match instead of the URI-path. According to the mod_rewrite documentation
What is matched?
In VirtualHost context, The Pattern will initially be matched against the part of the URL after the hostname and port, and before the query string (e.g. "/app1/index.html").
In Directory and htaccess context, the Pattern will initially be matched against the filesystem path, after removing the prefix that lead the server to the current RewriteRule (e.g. "app1/index.html" or "index.html" depending on where the directives are defined).
Thank you Jon for inspiration, finally mod_proxy + mod_rewrite worked:
# Global context
RewriteEngine On
RewriteRule ^(.+) http://remote/$1 [P]
ProxyPassReverse / http://remote/
I know that this is a simplified and coarse solution, but works for my purpose.

Running magento on other port than default 80

I have configured Apache virtual hosting on port 8080 to point to my magento website.
Listen 8080
<VirtualHost 6x.2x.6x.1x:8080>
ServerAdmin webmaster#localhost
ServerName domainname.com
ServerAlias *.domainname.com
DocumentRoot /var/www/sites/domain/
<Directory />
Options FollowSymLinks
AllowOverride all
Order allow,deny
allow from all
</Directory>
ErrorLog /var/log/apache2/error.log
# Possible values include: debug, info, notice, warn, error, crit,
# alert, emerg.
LogLevel warn
CustomLog /var/log/apache2/access.log combined
ServerSignature On
<LocationMatch ".*\svn.*">
Order allow,deny
Deny from all
</LocationMatch>
</VirtualHost>
When i go to the website www.domain.com:8080 the js, css, img and other things are not loaded because the port is not attached to the links
Here is a rewrite rule in magento .htaccess that does not seem to work:
<IfModule mod_rewrite.c>
############################################
## enable rewrites
Options +FollowSymLinks
RewriteEngine on
############################################
## you can put here your magento root folder
## path relative to web root
#RewriteBase /magento/
############################################
## workaround for HTTP authorization
## in CGI environment
RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]
############################################
## always send 404 on missing files in these folders
RewriteCond %{REQUEST_URI} !^/(media|skin|js)/
############################################
## never rewrite for existing files, directories and links
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-l
############################################
## rewrite everything else to index.php
RewriteCond %{SERVER_PORT} !^8080$
RewriteRule .* http://6x.2x.6x.1x:8080/index.php [L]
</IfModule>
Is Rewrite-mod the right solution to run magento site on different port? If yes, where is my mistake in the current Rewrite rule?
thank you,
Margots
I manage a magento site where a live shop is running on one server on port 80 and a dev site is running on another server on port 3000. When copying the site from live to dev all I need to do is change two rows in the core_config_data table, having:
path="web/unsecure/base_url"
path="web/secure/base_url"
You need to add your port number at the end of the url and that is all. In my case those two rows look like this:
(config_id, scope, scope_id, path,
value)
(default, 0, web/unsecure/base_url,
,http://www.dev-server.com:3000/)
(default, 0, web/secure/base_url,
,http://www.dev-server.com:3000/)
Note that I don't have a certificate on my dev server so I am not using https as the secure base_url. If you wish to use https this setting should be changed to https and you should omitt the custom port at the end.
Modification of the standard .htaccess file is not needed, and should probably be avoided.
EDIT
If you don't have access to the DB you can try creating a php file which will modify the database using magento:
<?php
error_reporting(E_ALL);
ini_set('display_errors', '1');
require_once ( "app/Mage.php" );
Mage::app('default');
$db = Mage::getSingleton('core/resource')->getConnection('core_write');
$db->query("UPDATE `core_config_data` SET `value` = 'http://dev-server.com:3000/' WHERE `path` = 'web/secure/base_url';");
$db->query("UPDATE `core_config_data` SET `value` = 'http://dev-server.com:3000/' WHERE `path` = 'web/unsecure/base_url';");
?>
You need to put this file in the root magento folder or change the path to Mage.php if you put it somewhere else.
I have not tried it (becaus I don't want to kill my Shop), but I think you can just change the Port in the Magento Admin.
In System / Configuration / Web / Base URL. Just try to add the Port to the URL there. Does that work?
You cannnot rewrite a URL that Magento writes in it's HTML Output using mod_rewrite. The Request to the wrong URL (without the :8080 Port) won't even reach your server.
UPDATE:
Look at the Source of the HTML output (i.E. go to your Shop with your Webbrowser and press CTRL-U or whatever). Do tags like the following have the correct URL, including the port?
<script type="text/javascript" src="http://yourstore.com:8080/js/prototype/prototype.js"></script>
All the methods shown seem to use a very complex process to do this.
It is very simple, change the URL to include the port in Admin > System > Configuration > Web > Urls
There you can define base URL, skin and media URLs.
You are missing this:
NameVirtualHost 6x.2x.6x.1x:8080
Perhaps you just didn't copy here since your site is working?
If Magento is loading pictures from the wrong site, there's nothing you can do in your Magento's .htaccess file: picture request will never get there. They will go to whatever other web server you have running on port 80.
You can only have this problem if Magento is building absolute links that include protocol and host (a terribly pointless bandwidth waste IMHO). My advice is that you look at the application settings and see if there's a place to specify the site base URL (since the app doesn't seem to be able to find it by itself).
(I tried to access Magento's demo site but it requires registering.)
I had the same problem because i was trying to use nginx with apache togather and i changed the apache port to a different than port 80 so i can use nginx as proxy, i changed the configuration file on apache httpd.conf but seemed that my site was not working, but I cleared the dns catch of my mac, and also used another browser to open my site and it worked.