.htaccess - Wordpress under Magento site with multi language sub directories - wordpress

tl; dr
I have a Magento install on www.example.com
I have a Wordpress install on www.example.com/wordpress
And I need the following urls to also serve that same wordpress content;
www.example.com/eu/wordpress
www.example.com/gb/wordpress
P.S.: I know there are duplicate content issues with this, please ignore that
The question is: What's the best way to do that?
The full story
I have a Magento multi store site using the 2 digit language code subdirectory technique.
I have one Wordpress installation in it's own subdirectory.
app
downloader
errors
eu/ - symlinks for the € Euro store
gb/ - symlinks for the £ UK store
includes
js
lib
media
shell
wordpress/ - The Wordpress install
var
I need the Wordpress blog to be available from all stores so they user stays in the store with their locale/currency.
What I have Tried
Using the answers in these Stacks;
htaccess multi language site with sub directories, and default 301
Endless Redirect Loop by htaccess rules multi language
I've made attempts but unfortunately I am terrible with .htaccess and vhosts problems
Via the vhosts file
<VirtualHost *:80>
ServerName www.example.com/eu/wordpress/
ServerAlias www.example.com/wordpress/
DocumentRoot /var/www/vhosts/www.example.com/public/wordpress
</VirtualHost>
<VirtualHost *:80>
ServerName www.example.com/gb/wordpress/
ServerAlias www.example.com/wordpress/
DocumentRoot /var/www/vhosts/www.example.com/public/wordpress
</VirtualHost>
Via the Wordpress htaccess
RewriteCond %{REQUEST_URI} !^/(eu|gb )/wordpress(/|$) [NC]
RewriteRule ^(.*)$ wordpress/$1 [R=301,L]
Via the Magento .htaccess
RewriteCond %{REQUEST_URI} ^/eu/wordpress/(.*)$ [OR]
RewriteCond %{REQUEST_URI} ^/gb/wordpress/(.*)$ [OR]
RewriteRule ^/wordpress/.*$ - [L]

Firstly, you don't need vhosts for wordpress, you need only one vhost per domain and/or subdomains. Your vhost should look something like this(which I assume that you already have a vhost similar to this for your magento shop):
<VirtualHost *:80>
ServerName example.com
ServerAlias www.example.com
DocumentRoot /var/www/vhosts/www.example.com/public
<Directory "/var/www/vhosts/www.example.com/public">
# allow .htaccess files to override all directives
AllowOverride All
</Directory>
</VirtualHost>
Now, you just need to modify magento's .htaccess(/var/www/vhosts/www.example.com/public/.htaccess), and add the following rules:
<IfModule mod_rewrite.c>
RewriteEngine on
# rewrite rule to redirect
# eu/wordpress -> /wordpress/
# eu/wordpress/ -> /wordpress/
# gb/wordpress -> /wordpress/
# gb/wordpress/ -> /wordpress/
RewriteRule ^(eu|gb)/wordpress/?$ /wordpress/ [R=301,NC,L]
# ... continue here with magento's rewrite rules ...
</IfModule>

We have this in production, hopefully this answer might help someone at some point.
The way we've done this is to add a RUN_CODE environmental variable which is used in a custom Wordpress filter to get the urls all working. I've used the 'eu' example from my question to illustrate it below. Note we had to do this for both Apache and Nginx so I've added the .htaccess and server blocks for both.
APACHE - .htaccess
In the eu country stub subdirectory you add this to your .htaccess file (so eu/.htaccess)
RewriteCond %{REQUEST_URI} ^/eu/wordpress$
RewriteRule ^(.*)$ /eu/wordpress/ [R=301]
RewriteCond %{REQUEST_URI} ^/eu/wordpress(.*)$
RewriteRule ^(.*)$ /wordpress/index.php/$1 [L,E=RUN_CODE:eu]
NGINX - Server Block
location ~* /eu/wordpress(.*) {
if (!-f $request_filename) {
set $code 'eu';
rewrite ^(.*)$ /wordpress/index.php?$1 last;
break;
}
}
In the Wordpress root index.php this was added at the top of the file
$pos = strpos($_SERVER['REQUEST_URI'], '/wordpress');
if ($pos !== 0) {
$_SERVER['REQUEST_URI'] = substr($_SERVER['REQUEST_URI'], $pos);
}
Wordpress theme functions.php
add_filter('post_link', 'link_mcnab');
add_filter('page_link', 'link_mcnab');
add_filter('bloginfo_url', 'link_mcnab');
function link_mcnab($link)
{
if (isset($_SERVER['REDIRECT_RUN_CODE']) && $_SERVER['REDIRECT_RUN_CODE']) {
$homeUrl = home_url();
$domain = substr($homeUrl, 0, strrpos($homeUrl, '/'));
$link = str_replace($domain, $domain . '/' . $_SERVER['REDIRECT_RUN_CODE'], $link);
}
return $link;
}

Related

Subdomain always redirect to WWW (home page)

I want, when open subdomain to return existing Wordpress page ex. www.example.com/custom-wordpress-page.
Server OS is CentOS 6.6, while Web Server (httpd) is Apache 2.2.15.
It have installed and configured Wordpress with .htaccess files configured for it's mod_rewrite.
Root folder is /var/www/html.
.htaccess file is in /var/www/html/.htaccess :
# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
# END WordPress
This configuration is working very well with one domain (ex. example.com). Today I wanted to add subdomain (ex. subdomain.example.com).
I tried multiple solutions including VirtualHosts with/without NameVirtualHost in httpd.conf file, also adding RewrriteCond&RewriteRule
under mod_rewrite in .htaccess file.
NameVirtualHost *:80
<VirtualHost *:80>
ServerAdmin email-address-redacted
ServerName www.example.com
ServerAlias example.com
DocumentRoot /var/www/html
</VirtualHost>
<VirtualHost *:80>
ServerAdmin email-address-redacted
ServerName subdomain.example.com
ServerAlias subdomain.example.com
Redirect permanent / http://example.com/custom-wordpress-page
</VirtualHost>
Also in first VirtualHost, I tried *.example.com instead off www.example.com.
In second one, I tried to make Reverse Proxy using ProxyPass and ProxyPassReverse.
Instead of using :*80 I tried using IP address of server and domain name.
Using .htaccess Rewrite rule with/without VirtualHosts gave no results.
subdomain.example.com always opens www.example.com instead of www.example.com/custom-wordpress-page.
Keep in mind - there is a lot of threads like this, but I tried many of them, including many tutorials but they aren't working alongside Wordpress.
Edit : Want to add that there are no wildcards in DNS records. There are NS records, one A record for example.com, one A record for subdomain.example.com and CNAME record for www that is poiting to example.com. There are NO *.
There was problem with hosting provider DNS records, configuration was correct, but I had to call provider to check records in their system and fix what is broken. After they done their job, it works correctly.

Apache 2.4 + FPM + Wordpress Multisite: URL Rewrite not working in admin

I just moved a Wordpress multi-site from a Apache 2.4 Prefork + mod_php to a new server with Apache 2.4 Event + php-fpm.
The site is working well on the frontend and it is a lot faster then before due to the CGI, but... the Wordpress administration panel is working just for the main site (and network administration).
The admin area for the second site is no more working, but the frontend is working great.
Examples
http://www.example.com/en/wp-admin/ => works
http://www.example.com/en/wp-admin/post-new.php => goes on error 404
I tried to debug the rewrites, but the unique log I have (also using Debug Level 8) is
[proxy_fcgi:error] [pid 13700:tid 140381047965440] [client X.X.X.X:54354] AH01071: Got error 'Primary script unknown\n', referer: http://www.example.com/en/wp-admin/
Follwing my configurations.
Any help appreciated. Thank you.
Virtual Host
<VirtualHost *:80>
ServerName www.example.com
DocumentRoot "/srv/www/example.com/public_html"
<IfModule mpm_event_module>
ProxyPassMatch ^/(.*\.php(/.*)?)$ fcgi://127.0.0.1:9000/srv/www/example.com/public_html/$1
</IfModule>
<Directory "/srv/www/example.com/public_html">
AllowOverride all
Require all granted
</Directory>
ErrorLog /srv/www/example.com/logs/error_log
TransferLog /srv/www/example.com/logs/access_log
</VirtualHost>
.htaccess
<Files "xmlrpc.php">
Order Allow,Deny
Deny from all
</Files>
# BEGIN WordPress
<IfModule mod_rewrite.c>
Options +FollowSymLinks
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
# uploaded files
RewriteRule ^([_0-9a-zA-Z-]+/)?files/(.+) wp-includes/ms-files.php?file=$2 [L]
# add a trailing slash to /wp-admin
RewriteRule ^([_0-9a-zA-Z-]+/)?wp-admin$ $1wp-admin/ [R=301,L]
RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^ - [L]
RewriteRule ^([_0-9a-zA-Z-]+/)?(wp-(admin|content|includes).*) $2 [L]
RewriteRule ^([_0-9a-zA-Z-]+]/)?(.*\.php)$ $2 [L]
RewriteRule . index.php [L]
</IfModule>
# END WordPress
Not sure if you found the solution. Just like to share our solution to this issue.
We've just added the lines below to the apache config. This will do the forwarding to FPM for all items in the regex.
ProxyPassMatch ^/([_0-9a-zA-Z-]+/)?(wp-(admin|activate|blog-header|comments-post|config|cron|links-opml|load|login|mail|settings|signup|trackback)\.php(/.*)?)$ fcgi://127.0.0.1:9000/var/www/html/$2
I add a different solution because #parpar's one, although it is working for me, it continues to throw proxy_fcgi errors:
[proxy_fcgi:error] Got error 'Primary script unknown\n'
The solution consists in removing the ProxyPassMatch way to call PHP FPM and including a handler to manage PHP calls into the Directory environment:
<Directory "/path/to/host/root">
# rest of configuration...
<FilesMatch \.php$>
SetHandler "proxy:unix:/path/to/socket.sock|fcgi://localhost"
</FilesMatch>
</Directory>
As I moved a WordPress multisite from a local XAMPP installation to a Ubuntu 18.04 server with Apache 2.4.29 and php-fpm I had the exact same issues and thus found this post.
For everyone also struggling with this, I found the following ressources helpful, however they did not work for me out of the box:
Same issue, different soloution approaches, which however did not work for me out of the box: https://serverfault.com/questions/450628/apache-2-4-php-fpm-proxypassmatch
Apache Doc on php-fpm with helpful and explained examples and even examples for Wordpress (however nothing multisite specific):
https://cwiki.apache.org/confluence/display/httpd/PHP-FPM
https://cwiki.apache.org/confluence/display/HTTPD/PHPFPMWordpress
Instead of using ProxyPassMatch I ended up using the following Apache configuration in the virtual host for the migrated page:
...
<FilesMatch "\.php$">
<If "-f %{REQUEST_FILENAME}">
SetHandler "proxy:unix:/run/php/php7.4-fpm.sock|fcgi://localhost/"
</If>
</FilesMatch>
...
With this configuration the Wordpress Mulisite .htaccess is recognized by apache and thus the Rewrite rules for multisite routes are applied. Due to the if statement only existing php files are passed to php-fpm.
So far both main and /en/ routes resolve correctly for frontend and admin pages.

Links not working in Wordpress 4.1

I have Wordpress 4.1 application on DigitalOcean.
After installation I found that any other link besides root/home link don't work.
I looked into similair questions and tried:
1) Change permalinks to default settings.
Now links partialy work. I mean they are ugly :http://104.131.95.146/?post_type=product instead of http://104.131.95.146/products
As suggested I did some troubleshooting from this link :
Pretty permalinks are available under:
Apache web server with the mod_rewrite module
But my server is Apache and I checked if mod_rewrite is aviable with:
apache2ctl -M | grep rewrite
got this message :
AH00558: apache2: Could not reliably determine the server's fully qualified domain name, using 127.0.1.1. Set the 'ServerName' directive globally to suppress this message
rewrite_module (shared)
Tried to restart apache and my droplet but still the same problem.
My htacces file inside var/www/html/.htaccess
# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
# END WordPress
Any help would be great.
Thanks in advance.
Once again fixed by myself.
I skipped important part of Wordpress installation.
What I did:
By default, this is 000-default.conf, but your file might be different if you created another configuration file:
sudo nano /etc/apache2/sites-available/000-default.conf
Inside of this file, we want to set up a few things. We should set the ServerName and create a directory section where we allow overrides. This should look something like this:
<VirtualHost *:80>
ServerAdmin webmaster#localhost
DocumentRoot /var/www/html
ServerName server_domain_name_or_IP
<Directory /var/www/html/>
AllowOverride All
</Directory>
. . .
When you are finished, save and close the file.
Next, we need to enable the rewrite module, which allows you to modify URLs. You can do this by typing:
sudo a2enmod rewrite
After you have made these changes, restart Apache:
sudo service apache2 restart

Bitnami Wordpress httpd-vhosts.conf url rewrite issue

I am having some issues with URL rewrite from sub domain to sub directory of a wordpress site. I didnt set the WP stuff up just inherited the problem. The box is a Bitnami WordPress.
I am attempting to do some redirects with url masking in the vhosts config. The redirects are fine just not getting the URL to mask and ending up with ugly sub dir in the URL.
This is what I have so far in /opt/bitnami/apps/wordpress/conf/httpd-vhosts.conf
RewriteEngine On
RewriteCond %{HTTP_HOST} ^www\.domain\.com$
RewriteRule ^(.*)$ http://domain.com/Ugly_sub_dir [P]
RewriteCond %{HTTP_HOST} ^app\.domain\.com$
RewriteRule ^(.*)$ http://domain.com/Another_ugly_sub_dir [P]
And in both cases I am redirecting fine but ending up with URL as:
domain.com/Ugly_sub_dir
domain.com/Another_ugly_sub_dir
But would like to see the plain old..
www.domain.com
app.domain.com
Any help much appreciated
OK I was approaching this in entirely the wrong way. Solved by setting up a proper vhost (not sure why I didnt do this in the first place - wasnt thinking, its been a while) ..
<VirtualHost *:80>
ServerName www.domain.com
ServerAlias www.domain.com
DocumentRoot "/opt/bitnami/apps/wordpress/htdocs/Ugly_sub_dir"
Include "/opt/bitnami/apps/wordpress/conf/httpd-app.conf"
</VirtualHost>

How to remove /drupal from URL in Drupal 7 when installation is in the associated sub-directory

In Drupal 6 I was able to successful install Drupal in a subdirectory called drupal and then reference the site without having to use example.com/drupal.
In Drupal 6 to get this to work I did the following:
- Created an .htaccess file in the root directory where /drupal was created. The file contents was:
Options -Indexes
RewriteEngine On
RewriteRule ^$ drupal/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ drupal/$1
Updated the drupal/sites/default/settings.php file to have the $base_url defined as:
$base_url = 'http://example.com';
When I try and do the same thing for Drupal 7, only the front page can be displayed, all the pages fail quite horribly (or only display the front page). I have also tried uncommenting the RewriteBase lines in /drupal/.htaccess. First I tried RewriteBase /drupal and then tried RewriteBase /. But both attempts failed. I never needed to do this with D6, but I thought I would rule out this possible fix.
I am currently testing the new Drupal 7 install using xampp (version 1.7.4) with the example.com site under htdocs (i.e. xampp/htdocs/example.com/drupal). The Drupal 6 site is within the same xampp installation, but of course with a different directory path (e.g. xampp/htdocs/d6example.com/drupal). Note that I also have the Drupal 6 installation running on a production server with only the $base_url variable value changed.
So, how can you install Drupal 7 in a subdirectory and then run it from that directory without having the directory name in the URL?
Note I am installing Drupal 7 in a subdirectory as it allows for easier upgrading between new releases of the Drupal 7 core.
Try with this :
RewriteEngine On
RewriteBase /example.com
RewriteRule ^$ drupal/ [L]
# rewrite rules for drupal files
RewriteCond %{DOCUMENT_ROOT}/example.com/drupal/$1 -f
RewriteRule ^(.*)$ drupal/$1 [L,QSA]
# rewrite rules for drupal paths
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ drupal/index.php?q=$1 [L,QSA]
Put this .htaccess file in example.com directory.
You don't have to modify drupal7 .htaccess
On Apache server, add this to the root .htaccess file.
RewriteEngine on
RewriteRule (.*) drupal/$1 [L]
Update the drupal settings.php file (in /drupal/site/default/ directory) so that the $base_url line reads:
$base_url = 'http://www.example.com';
I answered a very similar question on this here:
Two Drupal installation on the same server
My answer to your question is the same, I recommend eschewing the rewrite method in favor of the virtual host method as described below (which is just an excerpt of what I answered in the link above):
...
To do this correctly you must first enter the following line (or un-comment the line if >it already exists):
NameVirtualHost *:80
Next you must create the two virtual host entries. One will be similar to the following:
<VirtualHost *:80>
ServerName your.url.fortheroot
ServerAlias alternate.url.fortheroot
DocumentRoot "/path/to/webroot"
</VirtualHost>
The next entry would be similar to the following
<VirtualHost *:80>
ServerName your.url.forthesubfoldertest
ServerAlias alternate.url.forthesubfolder
DocumentRoot "/path/to/webroot/test"
</VirtualHost>
...
In your case, however, you would only require one virtual host entry & not two.
Additionally, it should be noted that, should you desire to serve a site from a location NOT in your webroot then you would also need a
<Directory></Directory>
entry to tell Apache what access to give to visitors (NOTE: in Linux the Apache user should be made owner of the files [or permissions should be set in a method that still allows the apache user rights to serve the files if you want to avoid giving it ownership])

Resources