Changing .htaccess file in order to allow subdomains - wordpress

I'm hosting a Wordpress page in my domain and I would like to create a subdomain in order to host also a wiki page.
Example:
www.foowordpress.com -> Points to a Wordpress blog
wiki.foowordpress.com -> It should point to a Media Wiki page
It seems that the .htaccess generated by Wordpress redirects all subdomains to the Wordpress index page.
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
Could someone suggest how to change this .htaccess file in order not to redirect the wiki subdomain?
I've already configured my DNS manager and my VirtualHost for creating this subdomain.

As it turns out, I had not configured properly the VirtualHost file, there was nothing wrong with the .htaccess file.
Here is my VirtualHost file for the subdomain in case anyone finds it useful:
<VirtualHost *:80>
# Admin email, Server Name (domain name), and any aliases
ServerAdmin webmaster#foo.com
ServerName www.wiki.foo.com
ServerAlias wiki.foo.com
# Index file and Document Root (where the public files are located)
DirectoryIndex index.html index.php
DocumentRoot /var/www/wiki.foo.com/
# Log file locations LogLevel warn ErrorLog
/var/log/apache2/wiki.foo.com/error.log CustomLog
/var/log/apache2/wiki.foo.com/access.log combined
</VirtualHost>

Related

Cannot use custom permalinks in wordpress when proxying request to a sub folder

We have a setup where a main site lives on one (aws) server, say www.example.com. We then run a wordpress blog on another aws server in the /blog subdirectory www.example.com/blog
On the main server, we proxy the /blog request to the other server with apache like:
<VirtualHost *:443>
...
ProxyPass /blog/ https://xx.xxx.x.xxx/siteA/
ProxyPassReverse /blog/ https://xx.xxx.x.xxx/siteA/
ProxyPass /blog https://xx.xxx.x.xxx/siteA/
ProxyPassReverse /blog https://xx.xxx.x.xxx/siteA/
...
</VirtualHost>
This works and the blog displays.... but only if the permalinks are set to 'plain'. If we set them to anything else then we just get a 404 response.
To further complicate matters, the blog server holds a number of different blogs in the following structure
/var/www/blog/siteA
/var/www/blog/siteB
/var/www/blog/siteC
The virtual host setup on the blog server is:
<VirtualHost *:443>
...
DocumentRoot /var/www/blog
<Directory "/var/www/blog/siteA/">
Options Indexes FollowSymLinks
AllowOverride all
Order allow,deny
allow from all
Require all granted
</Directory>
...
</VirtualHost>
the .htaccess file on the blog is:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /blog/
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /blog/index.php [L]
</IfModule>
I've tried various combinations for the rewrite base and the virtual host setup but I can't seem to get anywhere - those custom permalinks never work.
In case anyone else stumbles on the same issue. I tried changing the persmissions but even 777 didn't work.
in the end the solution was to add 'ProxyPreserveHost On' to virtual host on the main server, and to include the 'blog' sub folder on the proxy:
<VirtualHost *:443>
ProxyPreserveHost On
ProxyPass /blog/ https://xx.xxx.x.xxx/siteA/blog/
ProxyPassReverse /blog/ https://xx.xxx.x.xxx/siteA/blog/
ProxyPass /blog https://xx.xxx.x.xxx/siteA/blog/
ProxyPassReverse /blog https://xx.xxx.x.xxx/siteA/blog/

How to add WordPress' htaccess configurations into VirtualHost conf

I put WordPress' configuration into my VirtualHost directive, it looks like this:
<VirtualHost *:80>
ServerName www.example.com
ServerAlias example.com
DocumentRoot /var/www/html
<Directory />
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
# add a trailing slash to /wp-admin
RewriteRule ^wp-admin$ wp-admin/ [R=301,L]
RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^ - [L]
RewriteRule ^(wp-(content|admin|includes).*) $1 [L]
RewriteRule ^(.*\.php)$ $1 [L]
RewriteRule . index.php [L]
</Directory>
</VirtualHost>
When I put WordPress' configuration inside VirtualHost's root directory like above, the permalink and sub domain won't work.
But when I put WordPress' configuration inside the /etc/httpd/conf/httpd.conf root directory which is /var/www/html (as you can see it is the same root as Virtual host), then subdomain and permalinks are fine, I don't get it?
The reason I don't want to put WordPress' configuration in httpd.conf is because I want to make another site but it's not multisite, so I have to enter another WordPress' configuration on it.
I IncludeOptional the virtual host conf file at the very bottom of httpd.conf
I use CentOS 7
Apache 2.4
Wordpress 4.5.3
Mod rewrite included
After few hours it seems another rewrite rule that came above the IncludeOptional path/to/virtualhost/*.conf in httpd.conf had interfere Wordpress' configuration.
To troubleshoot most common permalinks or subdomains problem in wordpress is always check your rewrite rules and make sure the Wordpress rule (that one for the .htaccess) is always on top of the others.

Wordpress permalinks fails with virtualhost

I have a wordpress blog in a local server: /var/www/html/blog
(In the root dir '/var/www/html' there are other pages i shouldn't move, so I cant't put the wordpress blog in the root)
I bought a domain name and I've assigned it to this blog with a virtualhost. Then I activated the permalinks in the wordpress blog.
The problem is that when I go to mydomain/index.php or mydomain/, it shows a "Apache 2 Test Page" and in the apache error log it writes this message:
Options FollowSymLinks or SymLinksIfOwnerMatch is off which implies that RewriteRule directive is forbidden: /var/www/html/blog/
If I go to mydomain/wp-login.php, it's right. If I go to mydomain/section/blabla/, it's right. But the index page fails.
If I comment the .htaccess file I have put in the wordpress blog, the index page works, but pages like mydomain/section/blabla/ doesn't.
This is my virtualhost config code:
<VirtualHost *:80>
ServerAdmin ...Mymail...
DocumentRoot /var/www/html/blog
ServerName ...mydomain...
</Virtualhost>
And this is my .htaccess in the wordpress directory:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
Solved.
Finally I moved the blog files to the root apache directory. Then I removed the virtualhost and I created a redirection rule: mydomain/ -> mydomain/index.php
And it works.
Try putting
Options +FollowSymLinks
at the top of your .htaccess file

Apache folders mapped to urls

I have folder structure like this:
-- /var/www/domain
|
|-- wordpress
|-- framework
The desired functionality is when you go to domain.com its "document root" is in wordpress folder.
When you open domain.com/embed "document root" would be in framework folder.
I thikn the biggest problem is that I still need to support canonical URLS inside both of those folders.
Some example urls for wordpress are:
domain.com/contact
domain.com/solutions/finance
domain.com/blog/something-happened-yesterday (this might be subdomain blog.domain.com - not decided yet)
Embed will be always in format:
domain.com/embed/Category/Item?some=1,params=0
Wordpress and framework code is 100% independent of each other.
I think if I would simply move framework folder into wordpress folder and named it "embed" it would probably save a lot of trouble and work pretty well. But I feel like its not good solutions, and would prefer to keep them separate as they are now.
Some more snippets:
I tried many apache configurations, this is one of them:
<VirtualHost *:80>
ServerName u.ipushpull.com
ServerAdmin admin#ipushpull.com
DocumentRoot /var/www/ipushpull.com/u/wordpress
DirectoryIndex index.php index.html
Alias /embed/ /var/www/ipushpull.com/u/framework/code/wwwroot/
CustomLog ${APACHE_LOG_DIR}/ipushpull_web_u_access.log combined
ErrorLog ${APACHE_LOG_DIR}/ipushpull_web_u_error.log
LogLevel warn
</VirtualHost>
The .htaccess in framework folder:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ index.php [QSA,L]
</IfModule>
Thank you
Ok I found solution.
After looking into logs I noticed errors that were looking for files where they shouldn't be. And after more time googling finally I found something useful.
In your htaccess you MUST setup RewriteBase to whatever first parameter of your Alias is.
My final setting:
Apache Virtual Host
<VirtualHost *:80>
ServerName u.ipushpull.com
ServerAdmin admin#ipushpull.com
Alias /embed /var/www/ipushpull.com/u/framework/code/wwwroot
<Directory "/var/www/ipushpull.com/u/framework/code/wwwroot">
Options Indexes FollowSymLinks MultiViews
AllowOverride all
Order allow,deny
Allow from all
</Directory>
DocumentRoot /var/www/ipushpull.com/u/wordpress
DirectoryIndex index.php index.html
CustomLog ${APACHE_LOG_DIR}/ipushpull_web_u_access.log combined
ErrorLog ${APACHE_LOG_DIR}/ipushpull_web_u_error.log
LogLevel warn
</VirtualHost>
.htaccess (in framework/code/wwwroot)
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /embed
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [L]
</IfModule>
Works like a charm

HTTP 404 with Post name permalink

Okay so I've just create the first page on this site. It works when I use the default permalink settings.
If I change the permalink settings to use Post name, then I get an HTTP 404.
I'm not sure what's gone wrong or if I've broken anything. Can anyone help me fix?
The site is hosted on apache.
Are you using XAMPP or MAMP? There are a couple of common hiccups with those environments, taken from the WordPress Codex: Fixing Permalink Problems
Users of XAMPP (Windows): Some versions of XAMPP do not enable
mod_rewrite by default (though it is compiled in Apache). To enable it
— and thus enable WordPress to write the .htaccess file needed to
create pretty permalinks — you must open apache/conf/httpd.conf and
uncomment the line LoadModule rewrite_module modules/mod_rewrite.so
(i.e., delete the hash/pound sign at the front of the line).
Users of WAMP (Windows): Some versions of WAMP (all versions?) do not
enable mod_rewrite or permit following SymLinks by default. To enable
the required functionality navigate to the apache/conf/httpd.conf
file, open with a text editor and uncomment the line LoadModule
rewrite_module modules/mod_rewrite.so (i.e., delete the hash/pound
sign at the front of the line). Then further down in the same file
there is a section that starts with the line "Options FollowSymlinks".
Change the second line in that section from "AllowOverride none" to
AllowOverride all. Save edited httpd.conf and restart all WAMP
modules. Your permalinks should now work.
You might also see Permalinks without mod_rewrite if your sandbox doesn't have mod_rewrite available.
Apache
If you are using Apache there are usually two other culprits to broken permalinks: .htaccess isn't being generated (because of permissions settings) or Apache's AllowOverride directive isn't enabled.
First, if you SSH into your server, do you see a generated .htaccess file in the root? If not, WordPress might not have permissions to write that file. It's also possible the file does exist, but that WordPress cannot edit it. In either case, you can chmod that file (and create it if it doesn't exist) to 666.
Next, ensure your Apache config has the following settings:
<Directory />
Options FollowSymLinks
AllowOverride All
</Directory>
Finally, read through the Fixing Permalink Problems section of the WordPress Codex. There are several other tips and suggestions on why permalinks might not work.
In my case, firstly I had to update the .htaccess file inside my website root folder:
# 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
WordPress does this automatically if it has write permission. Otherwise it'll complain it can't write to it, and give the above code sample so you can manually update the .htaccess.
After that, I edited the apache2.conf file. In Linux, it resides in /etc/apache2/apache2.conf, there will be a section like this:
<Directory /var/www/>
Options Indexes FollowSymLinks
AllowOverride None
Require all granted
</Directory>
Change AllowOverride None to AllowOverride FileInfo.
Finally, execute the following commands:
sudo a2enmod rewrite
service apache2 restart
All these steps were necessary in order to work.
found this post on another site helped many people already
I finally managed to solve the problem! The solution: I was using a custom permalink structure http://kyl.fi/%category%/%postname%/. I removed the trailing slash (i.e. the last /) and voila. However, I'm quite sure I used a permalink structure with the trailing slash before without any problems, so I'm still confused and would be interested the hear more about this issue if somebody has an explanation.
All the standard permalinks have a trailing / in there.
in centos 8 and apache 2.4
look in /etc/httpd/conf.d
in your site .conf file add AllowOverride All, example like this
<Directory /path/to/site>
#add the following setting to allow .htaccess in your web dir to work
AllowOverride All
</Directory>
MY 80 LISTEN PORT
#Listen 80
<VirtualHost *:80>
DocumentRoot "/var/www/mysite.com"
ServerName www.mysite.com
# Other directives here
<Directory "/var/www/mysite.com">
Options FollowSymLinks
AllowOverride All
</Directory>
RewriteEngine on
RewriteCond %{SERVER_NAME} =www.mysite.com [OR]
RewriteCond %{SERVER_NAME} =mysite.com
RewriteRule ^ https://%{SERVER_NAME}%{REQUEST_URI} [END,NE,R=permanent]
</VirtualHost>
MY SSL VIRTUAL HOST:
<IfModule mod_ssl.c>
<VirtualHost *:443>
DocumentRoot "/var/www/mysite.com"
ServerName www.mysite.com
# Other directives here
<Directory "/var/www/mysite.com">
Options FollowSymLinks
AllowOverride All
</Directory>
ServerAlias mysite.com
Include /etc/letsencrypt/options-ssl-apache.conf
SSLCertificateFile /etc/mysite.com/fullchain.pem
SSLCertificateKeyFile /live/mysite.com/privkey.pem
</VirtualHost>
</IfModule>
Restart apache service.
Check your .htaccess (in my site the .htaccess is located in /var/www/mysite.com
My .htaccess
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^helloWorld/?$ /index.php [NC,L]
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
look helloWorld rewrite rule.
If you invoke url www.mysite.com/helloWorld and the browser show your homepage, the configuration is working and the permalink path to site works.
In my case, I am using the NGINX web browser with my WordPress installation. The fix is to add the following code snippet to the NGINX Directives:
location / {
try_files $uri $uri/ /index.php?$args;
}
# Add trailing slash to */wp-admin requests.
rewrite /wp-admin$ $scheme://$host$uri/ permanent;
location ~* \.(jpg|jpeg|png|gif|css|js|ico)$ {
expires max;
log_not_found off;
}
If you are using the excellent (open source) ISPconfig.org CPanel substitute, then go to your Sites page, under the Options tab, enter the above code snippet for NGINX Directives. ISPconfig has a feature to add common code snippets for quick access under the Options tab.
After making the above fix, I was able to use any of WordPress' Permalinks options.
Tested Working Solution:
in your apache2 config file for example:
/etc/apache2/sites-enabled/000-default.conf or mysite.conf etc ..
Make sure you have set param and not empty: ServerName www.example.com or 123.212.333.111
Also make sure You have set directory rules as below (Your rewrite rules may not have taken effect in the .htaccess hence you put it here and try finding out Why .htaccess does not work .htaccess not working apache):
<Directory /var/www/html>
Options Indexes FollowSymLinks MultiViews
AllowOverride All
Order allow,deny
allow from all
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</Directory>
Must be 2 point check:
1. Add code to .htaccess file:
# 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
And Permission of .htaccess file, must be: 644
My solution was add this code in /etc/apache2/sites_available/000-default.conf
<Directory "/var/www/mysite.com">
Options FollowSymLinks
AllowOverride All
</Directory>
I'm on Apache 2.4.46 (Ubuntu)
Thank you

Resources