I always have trouble when moving a site that I've developed in localhost into my hosting server with the .htaccess file. Specifically with relative paths to the files. It's not hard to fix, but it's quite bothering.
For example:
Whereas ErrorDocument 404 /foo/404.php works in the
hosting, for it to work in localhost I have to specify ErrorDocument
404 /projectroot/foo/404.php.
When using mod_rewrite, RewriteRule ^example/$ example.php [L,NC]
works in localhost, but for it to work in the hosting I must use
RewriteRule ^example/$ /example.php [L,NC] (note the slash before the file name).
It could be because I'm running localhost on Windows and my hosting is on Linux, but I don't think that's the problem.
So my question is, how can I ensure the paths to the files are correct when working locally or on the remote server? What is the "working directory" for the .htaccess file?
This may look slight more complicated then it seems, but I would work out your issue in the following way.
Create a dev domain on the C:\Windows\System32\drivers\etc\hosts file pointing to the IP 127.0.0.1 which is the localhost IP.
The domain name doesn't really matter just something easy for you to use for that given project for example test.dev so the hosts file would look like this:
# localhost name resolution is handled within DNS itself.
# 127.0.0.1 localhost
# ::1 localhost
127.0.0.1 localhost
127.0.0.1 test.dev
127.0.0.1 www.test.dev
Now define where the folder for that project will be, I will use c:\projects\test for this example.
Now create on your web server a new virtualhost for the domain you have just created, here is a sample of virtualhost that I use:
<VirtualHost *:80>
ServerAdmin root#localhost
DocumentRoot "C:/projects/test"
ServerName test.dev
ServerAlias www.test.dev
ErrorLog "C:/projects/logs/test.dev_error_log"
CustomLog "C:/projects/logs/test.dev_access_log" common
RewriteLog "C:/projects/logs/test.dev_rewrite_log"
RewriteLogLevel 9
ScriptAlias /cgi-bin/ "C:/projects/test/cgi-bin/"
<Directory "C:/projects/test">
Options -Indexes FollowSymLinks +ExecCGI
AllowOverride All
Order allow,deny
Allow from all
</Directory>
<Directory "C:/projects/test/cgi-bin">
AllowOverride None
Options None
Order allow,deny
Allow from all
</Directory>
</VirtualHost>
Restart your server to activate the changes, you do not need to restart the computer for the hosts file, it should work immediately once you save it.
Now you can use http://www.test.dev or http://test.dev and you can use the very same .htaccess on both, dev and live sites.
Some other tips:
Always use and define RewriteBase / as it can make a difference specially when you want to use rules without the / at the start, like you have mentioned for RewriteRule ^example/$ example.php [L,NC]
Depending on the rules you want to use, you may need to specify -MultiViews so I often just leave it on by default, like this:
Options +FollowSymLinks -MultiViews
As you can see on my virtualhost setup, I define the access, error and rewrite logs into the logs folder by default and with the domain name in question so when I need to see what is going on I have an easy access with the clear logs for that domain only and not my whole bunch of projects running on the same virualhost.
I guess that is about it, you don't really need that huge virtualhost that's just how I use it and its not like I need to copy and paste it all the time as I have my own simple script to create new domains and do all the work for me.
Related
I have installed a nextcloud on my raspberrypi. I want to use it in my local network for file transfer and everywhere for contacts and the calendar.
My nextcloud is located at /var/www/html/nextcloud. I also created a file /etc/apache2/sites-available/nextcloud.conf to enable the virtual host. Basically:
<VirtualHost *:80>
DocumentRoot /var/www/html/nextcloud/
ServerName nextcloud.example.com
<Directory /var/www/html/nextcloud/>
Require all granted
AllowOverride All
Options FollowSymLinks MultiViews
<IfModule mod_dav.c>
Dav off
</IfModule>
</Directory>
SetEnv HOME /var/www/html/nextcloud
SetEnv HTTP_HOME /var/www/html/nextcloud
</VirtualHost>
The nextcloud is reachable for the following links:
192.168.2.100/nextcloud
nextcloud.example.com
example.com/nextcloud
My question: Is there a way to forbid the access via example.com/nextcloud?
I tried to add deny, allow entries into /var/www/html/nextcloud/.htaccess. But either I block too much, or it does not work. Help is appreciated.
The fact that example.com/nextcloud is accessible at all is because you're pointing the subdomain at a subdirectory off the main domain's document root (as defined in the main domain's VirtualHost). If the subdomain is an entirely separate entity then it would be preferable that it points to an area outside of the main domain's document root.
I tried to add deny, allow entries into /var/www/html/nextcloud/.htaccess
If you want to block this in .htaccess (as opposed to the main domain's vHost config) then you need to check the requested Host header. And in doing this I would have thought it would be preferable to block anything other than the canonical hostname, ie. anything other that nextcloud.example.com (so it also blocks requests via the IP address). Rather than checking for the hostnames you want to block. You can do this using mod_rewrite, for example:
# /var/www/html/nextcloud/.htaccess
RewriteEngine On
RewriteCond %{HTTP_HOST} !^nextcloud\.example\.com$
RewriteRule ^ - [F]
The above responds with a "403 Forbidden" for any request where the requested Host header is not (as denoted by the ! prefix) nextcloud.example.com.
You could also block this subdirectory in the main domain's vHost container (not the subdomain). So it only applies to requests to example.com. For example:
<Directory /var/www/html/nextcloud>
Require all denied
</Directory>
Note that Order, Allow and Deny are Apache 2.2 directives and formerly deprecated on Apache 2.4. You should be using Require (mod_authz_core) on Apache 2.4.
I have a strange problem in accessing to virtual host in WAMP.
I have defined my virtual host (it calls mvc.dev) by defining in server configuration page in wampserver. Steps are exact that I have explained in the following:
1- first, going to server configuration page and select Add a virtual host
2- As index.php in my project is in C:\wamp64\www\mvcdev\public path, so I fill out related fields for my virtual host and then start to creation of virtual host.
3- It is created correctly and wamp server wants to restart DNS manually. I restarted it from wamp server menu like this:
4- After restarting, I check localhost page. It shows that mvc.dev is created correctly without any problem
5- And the problem occurs here, when I click on mvc.dev or type it in address bar of browser to access my virtual host, it causes to download a file instead of loading index.php file. I'm so frustrated about this issue and I don't know how can I solve this. Why does it cause to download hex file instead of executing URL?
This file is a hex file and every time that I put mvc.dev in url, it will start to download this file
For your information, I have also tested it by defining virtual host manually. I put related codes in httpd-vhosts.conf and then define mvc.dev in host and then restart DNS, but the result is the same with thing that I have explained before and I can not access to my virtual host at all. You can see it in the following:
configuration in host file:
# localhost name resolution is handled within DNS itself.
# 127.0.0.1 localhost
# ::1 localhost
127.0.0.1 localhost
127.0.0.1 mvc.dev
::1 mvc.dev
configuration of httpd-vhosts.conf:
#
# Virtual Hosts
#
<VirtualHost *:80>
ServerName localhost
DocumentRoot c:/wamp64/www
<Directory "c:/wamp64/www/">
Options +Indexes +FollowSymLinks +MultiViews
AllowOverride All
Require all granted
</Directory>
</VirtualHost>
<VirtualHost *:80>
ServerName mvc.dev
DocumentRoot c:/wamp64/www/mvcdev/public
<Directory "c:/wamp64/www/mvcdev/public/">
Options Indexes FollowSymLinks MultiViews
AllowOverride All
Require local
</Directory>
</VirtualHost>
How can I solve this problem?
Note: I also checked this one by one but I couldn't solve my problem:
project-links-do-not-work-on-wamp-server
The .htaccess file is
# Remove the question mark from the request but maintain the query string
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?$1 [L,QSA]
The code in index.php is
echo 'Request URL is: '.$_SERVER['QUERY_STRING'];
//require '../core/Router.php';
require "../vendor/autoload.php";
$router = new Core\Router();
$router->add('/','HomeController#index');
I had the same issue and I found out that Chrome requires any *.dev to be access via HTTPS and any chromium base browser will negate localhost access to it. Check this link
Change the website extension to something else. That worked for me. For example instead of example.com use example.tst.
I use Ratchet websocket on my server. It is working well without SSL but i need to make it work with SSL.
I've read this stackoverflow post. Unfortunately the support of my PAAS don't use httpd.conf. They advised me to add the ProxyPass straight in the .htaccess.
Regarding adding the following line in httpd.conf file then here I
would like to inform that we are not using httpd on the server as
server is Debian based and we are using Apache web server. I believe
you can use the same line in htaccess file or it would be better if
you can consult with the developer regarding this.
# ProxyPass for Ratchet with SSL
ProxyPass /wss2/ ws://127.198.132.141:8000/
# Preventing the app from being indexed
Header set X-Robots-Tag "noindex, nofollow"
# Use the front controller as index file. It serves as a fallback solution when
# every other rewrite/redirect fails (e.g. in an aliased environment without
# mod_rewrite). Additionally, this reduces the matching process for the
# start page (path "/") because otherwise Apache will apply the rewriting rules
# to each configured DirectoryIndex file (e.g. index.php, index.html, index.pl).
DirectoryIndex app.php
# By default, Apache does not evaluate symbolic links if you did not enable this
# feature in your server configuration. Uncomment the following line if you
# install assets as symlinks or if you experience problems related to symlinks
# when compiling LESS/Sass/CoffeScript assets.
# Options FollowSymlinks
# Disabling MultiViews prevents unwanted negotiation, e.g. "/app" should not resolve
# to the front controller "/app.php" but be rewritten to "/app.php/app".
<IfModule mod_negotiation.c>
Options -MultiViews
</IfModule>
<IfModule mod_rewrite.c>
RewriteEngine On
[...]
Unfortunately Adding ProxyPass /wss2/ ws://127.198.132.141:8000/ is crashing the server as if the .htaccess was incorrect.
Do you have any solutions or hints?
UPDATE :
From what i understand we can't use ProxyPass in .htaccess it should be only used in the server configuration or virtual host configuration.
I tried to explain it to the support but they do not seem to understand.
So apparently it is forbidden to use ProxyPass in .htaccess.
"ProxyPass and ProxyPassReverse are available only in the server
config and virtual host contexts."
Therefore if you can't add this line in the server config, could it be
added in the virtual host contexts?
Their answer :
As I have again reviewed all the settings on the server level which
includes the Apache modules and Firewall rules to make the Ratchet
websockets able to run on the server also the rules which we have
added in Firewall indicates that all the traffic from outside is
allowed on the port 8000 and I believe which should be sufficient to
allow outside connections for websocket.
As of now, it seems like you are trying to make the connection using
the different port (in case of https). As we have reviewed the server
settings and configurations and all seems to be good.
It would be highly appreciated if you can involves the developer in
this process so he can guide you better as he know the code level
things much better.
Right now attempting to connect with wss will throw :
WebSocket connection to 'wss://127.198.132.141/wss2/' failed:
WebSocket opening handshake was canceled
While using http with ws is working well.
In your virtual host add :
ProxyPass /wss2/ ws://yourdomain.xxx:8888/
(try with port 8888)
do not forget to restart apache service
virtual host example:
<IfModule mod_ssl.c>
<VirtualHost *:443>
ServerAdmin webmaster#localhost
DocumentRoot /var/www/html
<Directory /var/www/html/>
Options Indexes FollowSymLinks
AllowOverride All
Require all granted
</Directory>
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
<IfModule mod_dir.c>
DirectoryIndex index.php index.pl index.cgi index.html index.xhtml index.htm
</IfModule>
SSLCertificateFile /etc/letsencrypt/live/yourdomain.xxx/fullchain.pem
SSLCertificateKeyFile /etc/letsencrypt/live/yourdomain.xxx/privkey.pem
Include /etc/letsencrypt/options-ssl-apache.conf
ServerName yourdomain.xxx
ProxyPass /wss2/ ws://yourdomain.xxx:8888/
</VirtualHost>
</IfModule>
Here you can find a full working example
https://github.com/ratchetphp/Ratchet/issues/100
I have Apache 2.0.64 running in Windows 7. I visited lots of sites regarding installation and configuration of Apache. My config file is ok (I think). I can stop and restart the server without problems, but when try to access the page (localhost/market) then I get the 'Oops! Google Chrome could not connect to localhost' message.
I have written my RewriteRule within httpd.conf and I donot have .htaccess file in htdocs folder
Some values of my httpd.config may be helpful to you
ServerName localhost
Listen 80
DocumentRoot "C:/Apache/Apache2/htdocs"
<Directory "C:/Apache/Apache2/htdocs">
RewriteEngine On
RewriteRule ^(market|stock|mutual)$ stackoverflow/$1 [L]
Options Indexes FollowSymLinks
AllowOverride None
Order allow,deny
Allow from all
</Directory>
AccessFileName .htaccess
Please help me
I don't know what to do anymore...
If you are not able to access your page with localhost, try to access it with 127.0.0.1 IP address because this IP address points to localhost. Make the .htaccess file writable if it is not. Hope it helps.
My web host points my "main" domain name to the root www folder. The web files for that site are located in the "www/app/webroot" folder. I currently have the site up and running using the following in the htaccess file:
RewriteBase /
RewriteEngine on
RewriteRule ^$ app/webroot/ [L]
RewriteRule (.*) app/webroot/$1 [L]
I'm trying to start a dev site for the same site. I made a folder named "dev" in the www folder. So, the web files for this folder are in: "www/dev/app/webroot" I have a sub-domain pointing to the dev folder. When I use the same htaccess as above in the dev folder, it doesn't work because (I believe) it is inheriting the settings from the root www folder. When the page loads, it just comes up blank. How do I set up my htaccess files to allow for both sites?
Thanks in advance for any help! I'm obviously a novice at this stuff.
So we'll try to clean the things :-)
Avoid using .htaccess. All the settings in a .htaccess in a directory /foo/bar can be set in apache configuration as a Directory setting (.haccess is usefull if you provide limited access on apache conf, if you own the server don't use it).
<Directory /foo/bar>(...)</Directory>
Then you can access your sites with named based virtualhosts. Verify you have this option:
NameVirtualHost *:80
When you have it nice things can start.
This will be your virtualhost for your 1st app:
<VirtualHost *:80>
ServerName app
ServerAlias www.app.somwhere.com
ServerAlias app.somwhere.com
DocumentRoot /www/app/webroot
<Directory />
Options FollowSymLinks
AllowOverride None
</Directory>
<Directory /www/app/webroot>
Options Indexes FollowSymLinks
# this prevent.htaccess reading, remove if you want .htaccess
AllowOverride None
# allow web access
Order allow,deny
allow from all
</Directory>
</VirtualHost>
Most apache settings can be define here. Only for your 1st app. Apache will serve this configuration for all requests done for the site name 'app', or 'www.app.somwhere.com', or 'app.somwhere.com'. You can define a lot of alias(ServerAlias)., and only one name (ServerName).
Then if you go in your browser and type http://app/ your browser won't find the server, so set it in your /etc/hosts. This is what every people wanting to access your app should have in the hosts file until you get a real DNS (assuming your 1st app is app.somwhere.com and the second foo.somwhere.com and 92.128.52.226is your external IP):
127.0.0.1 app.somwhere.com app foo foo.somewhere.com
92.128.52.226 app.somwhere.com app foo foo.somewhere.com
And now let's add another virtualhost for your second app:
<VirtualHost *:80>
ServerName foo
ServerAlias www.foo.somwhere.com
ServerAlias foo.somwhere.com
DocumentRoot /www/foo/webroot
<Directory />
Options FollowSymLinks
AllowOverride None
</Directory>
<Directory /www/foo/webroot>
Options Indexes FollowSymLinks
# this prevent.htaccess reading, remove if you want .htaccess
AllowOverride None
# allow web access
Order allow,deny
allow from all
</Directory>
</VirtualHost>
And etc.
Don't forget to restart your apache. No rewrite rule. nice virtualhosts is the 1st step of a nice configuration, you will be able to define rules, directory or location specific things per name used. Even php configuration can be set per virtualhost with php_value instead of a global shared one on php.ini.
type
apache2 -S
to get the list of your virtualhosts, you'll see that the first one is the 'default' one, if apache does'nt understand the name of the requested site it will serve this default one (so you could ad a specific virtualhost on top to handle theses cases).
Try adding dev/ to the paths in lines 3 and 4 to your dev .htaccess.
Maybe you should remove the "RewriteBase /" line in the .htaccess in your dev folder?