Rewrite image directory to another folder in server using htaccess - apache

I have images in
/home/crawler/scrapers/images/website
for example
/home/crawler/scrapers/images/website/1/img-name.jpg
My Apache root is at
/var/www/html
I have .htaccess in this folder with following content
RewriteEngine On
RewriteRule ^/images/(.*)$ /home/crawler/scrapers/images/$1 [R,L]
My goal is to show images from the images folder as I mentioned above.
http://website.com/images/website/1/img-name.jpg >> /home/crawler/scrapers/images/website/1/img-name.jpg
Currently I am getting
Not Found
The requested URL /home/crawler/scrapers/images/website/1/img-name.jpg was not found on this server.
Apache/2.4.18 (Ubuntu) Server at IP Port 80
PS: I confirm that hataccess is enabled.

You can use Alias directive for this.
Include following code in your vhosts.conf or httpd.conf and restart Apache.
Alias /images /home/crawler/scrapers/images
<Directory /home/crawler/scrapers/images>
Allow from all
</Directory>

Related

Apache/2.4.6 (CentOS) Virtual Host error 404 with directory and files outside root

I'm trying to setup a Virtual Host (let's say "subdomain1.domain.com") on Apache/2.4.6 (CentOS). The web server does not have a hostname, IP address let's say is 55.55.55.55, Apache is listening at port 80. I have a redirect service active and the requests for "subdomain1.domain.com:80" are redirected to http://55.55.55.55/.
DocumentRoot is "/var/www/html" as per httpd.conf.
The website public content is in a directory named "subdomain1" which I'm placing inside the /var/www/html directory. The website code makes use of some JS/CSS which should be in common with the other virtual hosts I would like to create later, so I place the JS/CSS in a directory "libraries" located outside the WebSite Document Root (means, inside "/var/www/html"), and I'm tryng to give them access from the html code by a "../libraries/".
Virtual Host configuration file is a "subdomain1.conf" placed in /etc/httpd/conf.d/" directory, this is the file:
<VirtualHost *:80>
ServerName subdomain1.domain.com
DocumentRoot /var/www/html/subdomain1
</VirtualHost>
Now, with this configuration I can reach the website of Subdomain1 at http://subdomain1.domain.com. The public content of Subdomain1 is reachable, but.. the problem is: all the JS/CSS are not found by Apache and they all return a 404 error. While debugging with the web browser I see the path where Apache is searching the JS/CSS library is "http://55.55.55.55/libraries/" and this returns 404.
Any idea where I'm doing wrong ?
Many thanks !
Mic
Here are some suggestions from me.
Setting FQDN of your server FQDN
$ nano /etc/hosts
55.55.55.55 subdomain1.domain.com subdomain1
Setting your server hostname
$ nano /etc/hostname
subdomain1.domain.com
Enable mod_rewrite on the apache webserver
Create an .htaccess file below and save it into the folder /var/www/html/subdomain1/
< IfModule mod_rewrite.c> RewriteEngine On RewriteBase / RewriteRule ^index\.php$ - [L] RewriteCond %{REQUEST_FILENAME} !-f RewriteCond %{REQUEST_FILENAME} !-d RewriteRule . /index.php [L] < /IfModule>
Thank you,
Afrizal

URL Rewrite in httpd.conf

I have a redirect setup on my Apache server within the httpd.conf file that redirects all traffic to "server.mydomain.com" to https://server.mydomain.com/uri. Unfortunately, some users will enter https before the URL and it does not redirect, instead they get Apache error page. I want to know how can I get users who input https before the URL to be redirect to https://server.mydomain.com/uri. I believe I may have to do a rewrite but I'm not sure or know how to go about doing it. I've research about rewrite and found it should be done in the .htaccess file but when I read Apache best practice they state it should not be done within .htaccess file for security and performance. Instead, it should be done within the config file. Since the redirection is working within the httpd.conf file, I would like to incorporate the rewrite there as well. I presume that is the correct according to Apache website. My issue is how do I go about doing this within that file. I've included the file information below. Any assistance is greatly appreciated. The server is using Tomcat 7 with Apache 2.2.15.
httpd.conf file:
</IfModule>
#
ProxyPass /uri/fbs-ws ws://server.mydomain.com:8081/uri/fbs-ws
ProxyPassReverse /uri/fbs-ws ws://server.mydomain.com:8081/uri/fbs-ws
</IfModule>
<VirtualHost *:80>
ProxyPass /uri http://server.mydomain.com:8080/uri
ProxyPassReverse /uri http://server.mydomain.com:8080/uri
Redirect permanent / https://server.mydomain.com/uri
</VirtualHost>
Did you try creating a vhost for 443 and switching to http there via Rewrite?
<VirtualHost *:443>
RewriteEngine On
RewriteCond %{HTTPS} on
RewriteRule (.*) http://%{HTTP_HOST}%{REQUEST_URI}
</VirtualHost>

change Apache document root in XAMPP

I have just downloaded a website which contains static html pages from a hosting to my localhost (XAMPP) and saved it to a subdirectory in XAMPP htdocs directory.
When I view the local website in browser, all css, js files and images were missing. I checked the HTML source code and saw that it's all using relative path for css/js or href link. For example:
Text Link
It works on the hosting, but on my localhost, it points to http://localhost/contact instead of http://localhost/bf/contact (my subdirectory name is bf). This structure is applied to all css, js files too and they could not be loaded.
I have tried to add .htaccess file to my subdirectory:
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_URI} !bf/
RewriteRule (.*) /bf/$1 [L]
But it still not works. I don't want to manually change all src, href, and link in every HTML pages to fix this. There would be a way to make Apache recognize the root is /bf instead of / , applied to all files in this directory only. Please help !
Thank you very much !
You can get this work by setting apache virtual host at locally as describe below:
For example in ubuntu /etc/apache2/httpd.conf,
If you are using XAMPP on window that is the case, then the httpd.conf file should be C:\xampp\apache\conf\httpd.conf.
Please follow below steps to configure your local sites at local server:
------------
Step 1
------------
i.e
<VirtualHost *:80>
ServerName test.demo.tst ( Any name you want to set )
DocumentRoot "C:/xampp/htdocs/bf"
<Directory "C:/xampp/htdocs/bf">
AllowOverride All
Order allow,deny
Allow from all
</Directory>
#RackEnv development
ErrorLog C:/xampp/htdocs/bf/logs/test.demo.tst_error
CustomLog C:/xampp/htdocs/bf/logs/test.demo.tst_access common
</VirtualHost>
------------
Step 2
------------
Than you need to restart apache service
------------
Step 3
------------
Than you need to do setting in your hosts files
for example in hosts file place below entry
127.0.0.1 test.demo.tst
------------
Step 4
------------
Than you can access this url test.demo.tst local server.
I hope this informations would be helpful to you.
Please try this one..
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_URI} !bf/
RewriteRule ^(.*)$ /bf/$1 [QSA,L]
Hope this is helpful to you. And your apache rewrite mode must be on in php.ini

URL redirection in Apache rewrite

I installed a Aava shopping cart jadasite on a shared Java webhost and now my home page is like http://www.mydomain.com/jada/web/fe/localhost/English/home.
To make my home page appear at http://www.mydomain.com I need to make changes in the Apache's mod_rewrite rule. I don't know much about the Apache server.
Can you please tell me what should be the code for Apache's rewrite rule and where do I need to put it.
yes you could redirect with an htaccess file or you could upload an index.php file to your root and write this line of code
<?php
header('Location: http://www.mydomain.com/jada/web/fe/localhost/English/home');
?>
if you are sure you want this - put into .htaccess
RewriteRule ^(.*)$ /jada/web/fe/localhost/English/home/$1 [QSA,L]
However i feel there is server misconfiguration at your end. If you have direct access to apache configs you may want to set right vhosts. Here is example from my localhost:
<VirtualHost 127.0.0.2:80>
DocumentRoot X:/xampp/htdocs/mydomain.com
ServerName mydomain.com
ServerAlias www.mydomain.com
</VirtualHost>
127.0.0.2 should be your external IP address that apache is listening to. X:/xampp/htdocs/mydomain.com can be any folder you want (folders outside htdocs dir may need additional directives in order to work).

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.