Configuring Blogs on a multi site Magento setup - apache

I have two websites say abc.com and pqr.com running from the same Magento installation. Now I want to create blogs for each of those domains through wordpress for which I have created a folder "blogs" in the root Magento directory and installed two instances of wordpress in folders named blogs/abc and blogs/pqr.
How do I configure my .htaccess or index.php in the root directory so that requests to www.abc.com/blog and www.pqr.com/blog are routed to the folders blogs/abc and blogs/pqr respectively.
Please note that I want my blog URL's to be of the form abc.com/blog and not blog.abc.com
############################################
## you can put here your magento root folder
## path relative to web root
RewriteBase /
############################################
## uncomment next line to enable light API calls processing
# RewriteRule ^api/([a-z][0-9a-z_]+)/?$ api.php?type=$1 [QSA,L]
############################################
## rewrite API2 calls to api.php (by now it is REST only)
RewriteRule ^api/rest api.php?type=rest [QSA,L]
############################################
## workaround for HTTP authorization
## in CGI environment
RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]
############################################
## TRACE and TRACK HTTP methods disabled to prevent XSS attacks
RewriteCond %{REQUEST_METHOD} ^TRAC[EK]
RewriteRule .* - [L,R=405]
############################################
## redirect for mobile user agents
#RewriteCond %{REQUEST_URI} !^/mobiledirectoryhere/.*$
#RewriteCond %{HTTP_USER_AGENT} "android|blackberry|ipad|iphone|ipod|iemobile|opera mobile|palmos|webos|googlebot-mobile" [NC]
#RewriteRule ^(.*)$ /mobiledirectoryhere/ [L,R=302]
############################################
## 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
#Redirect Blogs
#RewriteCond %{HTTP_HOST} ^(?:www\.)?(abc|pqr)\.com$ [NC]
#RewriteRule ^blog(?:/(.*))?$ /blogs/%1/$1 [NC,L]
############################################
## rewrite everything else to index.php
RewriteRule .* index.php [L]
</IfModule>

Add the following to your root .htaccess:
RewriteEngine on
RewriteCond %{HTTP_HOST} ^(?:www\.)?(abc|pqr)\.com$ [NC]
RewriteRule ^blog(?:/(.*))?$ /blogs/%1/$1 [NC,L]
Please rearrange the end part of your .htaccess as
#Redirect Blogs
RewriteCond %{HTTP_HOST} ^(?:www\.)?(abc|pqr)\.com$ [NC]
RewriteRule ^blog(?:/(.*))?$ /blogs/%1/$1 [NC,L]
############################################
## rewrite everything else to index.php
## but never rewrite for existing files, directories and links
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-l
RewriteRule .* index.php [L]
Please note that the RewriteCond and the immediately following RewriteRule come together to form a redirect rule. You were putting the blog related rules right between them breaking that functionality.

If you have access to the vhost configuration (usually /etc/apaches/sites-enabled/abc), just enter
Alias /blog /var/www/blogs/abc
into the vhost for abc.com and analog for the vhost of pqr.com.
You also need to have mod_alias enabled. This is not possible to be done in a .htaccess file, see also this document

Related

.htaccess rules to redirect crawlers to another folder

I have a single page app (react, react-router) and I am trying to redirect search and share bots/crawlers to a different folder with static snapshots of the app. I have a .htaccess setup which is mostly working but missing a few things.
It is currently correctly redirecting from domain.com/something to domain.com/snap/something/
Edit: It is now correctly redirecting domain.com to domain.com/snap/
It does not handle domain.com/non-existing, it should redirect to domain.com/snap/404.html but currently redirects to domain.com/snap/non-existing
The .htaccess I have is
<IfModule mod_rewrite.c>
Options +FollowSymlinks
RewriteEngine On
RewriteCond %{HTTP_HOST} ^www\.(.+)$ [NC]
RewriteRule ^(.*)$ https://%1/$1 [L,R=301]
# For crawlers show snapshots
RewriteCond %{HTTP_USER_AGENT} googlebot|bingbot|yandex|baiduspider|facebookexternalhit|twitterbot|rogerbot|linkedinbot|embedly|quora\ link\ preview|showyoubot|outbrain|pinterest|slackbot|vkShare|W3C_Validator [NC,OR]
RewriteCond %{QUERY_STRING} _escaped_fragment_
RewriteCond %{REQUEST_URI} !^/(snap) [NC]
# Proxy the request
RewriteRule ^(?!.*?(\.js|\.css|\.xml|\.less|\.png|\.jpg|\.jpeg|\.gif|\.pdf|\.doc|\.txt|\.ico|\.rss|\.zip|\.mp3|\.rar|\.exe|\.wmv|\.doc|\.avi|\.ppt|\.mpg|\.mpeg|\.tif|\.wav|\.mov|\.psd|\.ai|\.xls|\.mp4|\.m4a|\.swf|\.dat|\.dmg|\.iso|\.flv|\.m4v|\.torrent|\.ttf|\.woff))(.*) /snap/$2/ [R=301,L]
# Don't rewrite files or directories
RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^ - [L]
# If the requested pattern is file and file doesn't exist, send 404
RewriteCond %{REQUEST_URI} ^(\/[a-z_\-\s0-9\.]+)+\.[a-zA-Z]{2,4}$
RewriteRule ^ - [L,R=404]
# otherwise use history router
RewriteRule ^ /index.html [L]
</IfModule>
Edit: I moved the crawler part of .htaccess up and it is now correctly handling domain.com -> domain.com/snap/ scenario, but I can't figure out how to make it handle the non-existing routes.
To redirect your 404 uris to the correct destination, you can use the following rule . Put this right bellow your www to non-www redirect rule block and make sure to clear your browser cache before testing :
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{HTTP_USER_AGENT} googlebot|bingbot|yandex|baiduspider|facebookexternalhit|twitterbot|rogerbot|linkedinbot|embedly|quora\ link\ preview|showyoubot|outbrain|pinterest|slackbot|vkShare|W3C_Validator [NC,OR]
RewriteCond %{QUERY_STRING} _escaped_fragment_
RewriteCond %{REQUEST_URI} !^/(snap) [NC]
# Proxy the request
RewriteRule ^(?!.*?(\.js|\.css|\.xml|\.less|\.png|\.jpg|\.jpeg|\.gif|\.pdf|\.doc|\.txt|\.ico|\.rss|\.zip|\.mp3|\.rar|\.exe|\.wmv|\.doc|\.avi|\.ppt|\.mpg|\.mpeg|\.tif|\.wav|\.mov|\.psd|\.ai|\.xls|\.mp4|\.m4a|\.swf|\.dat|\.dmg|\.iso|\.flv|\.m4v|\.torrent|\.ttf|\.woff))(.*) /snap/404.html [R=301,L]

Why is this .htaccess file routing static files to our front controller?

We have been using the following .htaccess file for years. We are using Apache 2.4.7. And for some reason, static files seem to be hitting our front controller index.php!
For example: https://example.com/apple-touch-icon.png is being handled by our front controller as we can see the response header includes X-Powered-By:PHP/5.5.9-1ubuntu4.3.
Can anyone spot the issue?
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
### Force SSL
RewriteCond %{HTTP:X-Forwarded-Proto} !https
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
### Canonicalize codeigniter URLs
# If your default controller is something other than
# "welcome" you should probably change this
RewriteRule ^(welcome(/index)?|index(\.php)?)/?$ / [L,R=301]
RewriteRule ^(.*)/index/?$ $1 [L,R=301]
# Removes trailing slashes (prevents SEO duplicate content issues)
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.+)/$ $1 [L,R=301]
# Enforce NO www
RewriteCond %{HTTP_HOST} ^www [NC]
RewriteRule ^(.*)$ https://example.com/$1 [L,R=301]
# Removes access to the system folder by users.
# Additionally this will allow you to create a System.php controller,
# previously this would not have been possible.
# 'system' can be replaced if you have renamed your system folder.
RewriteCond %{REQUEST_URI} ^system.*
RewriteRule ^(.*)$ /index.php/$1 [L]
# Checks to see if the user is attempting to access a valid file,
# such as an image or css document, if this isn't true it sends the
# request to index.php
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L]
</IfModule>
<IfModule !mod_rewrite.c>
# Without mod_rewrite, route 404's to the front controller
ErrorDocument 404 /index.php
</IfModule>
It appears that your ErrorDocument directive is causing this (loading index.php when some resource i.e. image, js or css file is not found.
You can comment out this directive to avoid this behavior:
ErrorDocument 404 /index.php

Laravel on Plesk-Server - Only accessible by calling /public

When setting up a fresh Laravel installation, we have to run it using domain.com/public instead of domain.com.
I googled after this problem and I saw that this could be because of mod_rewrite not being enabled. However, we are running Plesk on our server and their documentation at http://download1.parallels.com/Plesk/PP12/12.0/Doc/en-US/online/plesk-administrator-guide/index.htm?fileName=74206.htm says:
The mod_rewrite module is enabled by default in Apache that comes with Plesk for Linux. To make use of the functionality, you will need to create an .htaccess file containing the desired rewrite rules - refer to the Apache documentation for more information.
Laravel is working fine, if I move the contents of public in the main folder, and the rest in a /laravel folder (simply have to adopt the main index.php file).
However I would like to keep the original file structure.
Any tips?
Just use .htaccess to redirect from / to /public. Maybe similar to this
RewriteCond %{REQUEST_URI} !^public
RewriteRule ^(.*)$ public/$1 [L]
You need two .htaccess files. One inside the Laravel Project and the other one inside your domain.com
Laravel Project (/var/www/vhosts/domain.com/public/.htaccess)
<IfModule mod_rewrite.c>
<IfModule mod_negotiation.c>
Options -MultiViews -Indexes
</IfModule>
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ public/index.php?$1 [L,QSA]
# Handle Authorization Header
RewriteCond %{HTTP:Authorization} .
RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]
# Redirect Trailing Slashes If Not A Folder...
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} (.+)/$
RewriteRule ^ %1 [L,R=301]
# Send Requests To Front Controller...
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [L]
</IfModule>
main domain (/var/www/vhosts/domain.com/.htaccess)
RewriteEngine on
RewriteCond %{REQUEST_URI} !^public
RewriteRule ^(.*)$ public/$1 [L]

Allow Adwords Query ?gclid=* in .htaccess

I have a problem with my adwords session that are not tracked because my htaccess is rewriting query strings to the original url.
https://www.example.com/toto?gclid=Test-1234 is redirect to https://www.example.com/toto
Below, a portion of my htaccess :
############################################
## redirection 301 https
RewriteEngine On
RewriteCond %{HTTPS} off
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI}
############################################
## make HTTPS env vars available for CGI mode
SSLOptions StdEnvVars
</IfModule>
<IfModule mod_rewrite.c>
redirect 301 /home http://www.example.com
############################################
## enable rewrites
Options +FollowSymLinks
RewriteEngine on
############################################
## you can put here your magento root folder
## path relative to web root
#RewriteBase /magento/
############################################
## uncomment next line to enable light API calls processing
# RewriteRule ^api/([a-z][0-9a-z_]+)/?$ api.php?type=$1 [QSA,L]
############################################
## rewrite API2 calls to api.php (by now it is REST only)
RewriteRule ^api/rest api.php?type=rest [QSA,L]
############################################
## workaround for HTTP authorization
## in CGI environment
RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]
############################################
## TRACE and TRACK HTTP methods disabled to prevent XSS attacks
RewriteCond %{REQUEST_METHOD} ^TRAC[EK]
RewriteRule .* - [L,R=405]
############################################
## redirect for mobile user agents
#RewriteCond %{REQUEST_URI} !^/mobiledirectoryhere/.*$
#RewriteCond %{HTTP_USER_AGENT} "android|blackberry|ipad|iphone|ipod|iemobile|opera mobile|palmos|webos|googlebot-mobile" [NC]
#RewriteRule ^(.*)$ /mobiledirectoryhere/ [L,R=302]
############################################
## 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
RewriteRule .* index.php [L]
</IfModule>
Could you help me please ? Many thanks.
Julien

Force https in htaccess

I got a certificate for my site and now I need to move it to https. This is my htaccess:
# Turn on URL rewriting
RewriteEngine On
# Installation directory
RewriteBase /
# Protect hidden files from being viewed
<Files .*>
Order Deny,Allow
Deny From All
</Files>
# Protect application and system files from being viewed
RewriteRule ^(?:application|modules|system)\b.* index.php/$0 [L]
# Allow any files or directories that exist to be displayed directly
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
# Rewrite all other URLs to index.php/URL
RewriteRule .* index.php/$0 [PT]
How do I do it? Thanks
There's a few ways to do it; here's one:
## port requirement (bail if not 443)
RewriteCond %{SERVER_PORT} !^443$
## exceptions
RewriteCond %{REQUEST_URI} ^/somepath$ [OR]
RewriteCond %{REQUEST_URI} ^/anotherpath$
## force traffic to https equivalent
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
I've added 2 optional RewriteCond entries that allow you to specify exceptions based on the first component of the URI.