Apache folders mapped to urls - wordpress

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

Related

How to config Apache to get Symfony4 routes working in subdirectory on a WordPress domain

On my Apache server I have a WordPress installation working on www.domain.com and I want to add a Symfony4 installation on www.domain.com/symfony. Symfony routes are taken to the WordPress 404 page (but still are the correct URL i.e. www.domain.com/symfony/about). However, if there are NO routes defined in my Symfony app, then the base www.domain.com/symfony/ URL will correctly display the Symfony4 getting started page. What is wrong in my Apache conf files?
Web server is Apache 2.4, I have tried using Alias and AliasMatch directives in the /etc/apache2 conf files. Alias displays WordPress 404 for all Symfony routes and AliasMatch appends many /index.php's onto the URL and displays the same 404. I tried to copy what phpmyadmin does with its Alias /phpmyadmin /usr/share/phpmyadmin
# /etc/apache2/sites-available/wordpress.conf
<Directory /var/www/wordpress>
Options Indexes FollowSymLinks
AllowOverride All
Require all granted
</Directory>
<VirtualHost *:80>
ServerName domain.com
ServerAlias www.domain.com
ServerAdmin me#myemail.com
DocumentRoot /var/www/wordpress
</VirtualHost>
# /var/www/wordpress/.htaccess
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
# /etc/apache2/sites-available/symfony.conf
Alias /symfony /var/www/symfony/app/public
<Directory /var/www/symfony/app/public>
Options FollowSymLinks
AllowOverride All
Require all granted
Allow from All
FallbackResource /index.php
</Directory>
# /var/www/symfony/app/config/routes.yaml
about_index:
path: /about
controller: App\Controller\AboutController::index
I expected www.domain.com/symfony/about to go to /var/www/symfony/app/public/index.php and route to App\Controller\AboutController::index, but it looks like WordPress is trying to find the page /symfony/about and 404's.
When I change my Alias /symfony /var/www/symfony/app/public to AliasMatch ^/symfony.* /var/www/symfony/app/public it changes the Symfony URL to www.domain.com/symfony/index.php/index.php/index.php/index.php/index.php/index.php but has the same result (WordPress 404 page). None of my changes have impacted the WordPress part at all - it still functions perfectly.
EDIT 1
Tried a new conf to test if it's my symfony config - it works as expected so I just think it must be something in my Apache confs. This config treats the symfony app as the main site and wordpress is totally disabled:
# /etc/apache2/sites-available/symfony_only.conf
<Directory /var/www/symfony/app/public>
Options FollowSymLinks
AllowOverride All
Require all granted
Allow from All
FallbackResource /index.php
</Directory>
<Directory /var/www/symfony/app/public/bundles>
FallbackResource disabled
</Directory>
<VirtualHost *:80>
ServerName domain.com
ServerAlias www.domain.com
ServerAdmin me#myemail.com
DocumentRoot /var/www/symfony/app/public
</VirtualHost>
Looks like I got a working conf now. Not sure if it's even resulting in any different configuration than the one I had. I also did a lot of php bin/console cache:clear as I was even getting 404 and 500 errors on the little debug bar at the bottom of the symfony welcome page (not displayed on the bar, but the bar actually wouldn't properly load). The below file is my entire conf which I combined into one file. It serves from my Symfony4 app if the url path begins with /symfony, and the routes all work.
# /etc/apache2/sites-available/wordpress_symfony.conf
Alias /symfony /var/www/symfony/app/public
<Directory /var/www/symfony/app/public>
AllowOverride All
Require all granted
Allow from All
FallbackResource /index.php
</Directory>
<Directory /var/www/symfony/app/public/bundles>
FallbackResource disabled
</Directory>
<Directory /var/www/wordpress>
Options Indexes FollowSymLinks
AllowOverride All
Require all granted
</Directory>
<VirtualHost *:80>
ServerName domain.com
ServerAlias www.domain.com
ServerAdmin me#myemail.com
DocumentRoot /var/www/wordpress
ErrorLog ${APACHE_LOG_DIR}/error.log
CustomLog ${APACHE_LOG_DIR}/access.log combined
</VirtualHost>
Also I added the symfony/apache-pack with composer which added this .htaccess in the symfony public directory. The docs said I can get improved performance by moving all these rules into my .conf file and disabling overrides:
# /var/www/symfony/app/public/.htaccess
DirectoryIndex index.php
<IfModule mod_negotiation.c>
Options -MultiViews
</IfModule>
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REQUEST_URI}::$1 ^(/.+)/(.*)::\2$
RewriteRule ^(.*) - [E=BASE:%1]
RewriteCond %{HTTP:Authorization} .
RewriteRule ^ - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]
RewriteCond %{ENV:REDIRECT_STATUS} ^$
RewriteRule ^index\.php(?:/(.*)|$) %{ENV:BASE}/$1 [R=301,L]
RewriteCond %{REQUEST_FILENAME} -f
RewriteRule ^ - [L]
RewriteRule ^ %{ENV:BASE}/index.php [L]
</IfModule>
<IfModule !mod_rewrite.c>
<IfModule mod_alias.c>
RedirectMatch 307 ^/$ /index.php/
</IfModule>
</IfModule>

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

Remove web/app_dev.php/ from url

I have my application in symfony 2 done.
And now I want to remove the web/app_dev.php/ from the url.
I read about this and after doing this:
php app/console cache:clear --env=prod --no-debug
And add .htaccess:
DirectoryIndex app.php
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REQUEST_URI}::$1 ^(/.+)/(.*)::\2$
RewriteRule ^(.*) - [E=BASE:%1]
RewriteCond %{ENV:REDIRECT_STATUS} ^$
RewriteRule ^app\.php(/(.*)|$) %{ENV:BASE}/$2 [R=301,L]
RewriteCond %{REQUEST_FILENAME} -f
RewriteRule .? - [L]
RewriteRule .? %{ENV:BASE}/app.php [L]
</IfModule>
<IfModule !mod_rewrite.c>
<IfModule mod_alias.c>
RedirectMatch 302 ^/$ /app.php/
</IfModule>
</IfModule>
After this I can use the url: localhost/test/web/
But it's not exactly my goal. I want to remove the web also.
And after this there's another problem. When I'm using localhost/test/web/ to access the page there's some stylesheet missing, but in app_dev.php everything looks good.
My question is, How can I remove the /web/ from the url?
And how can I have the stylesheet missing?
To remove the web you simply have to modify your apache configuration to make web as root directory.
DocumentRoot /yourpath/www/web
<Directory /youpath/www/web/>
Exemple of Apache Virtual Host Complete configuration :
<VirtualHost *:80>
ServerName www.yourdomain.com
DocumentRoot /yourpath/www/web
<Directory /yourpath/www/web>
Options FollowSymLinks
AllowOverride None
Require all granted
Allow from All
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ /app.php [QSA,L]
</Directory>
LogLevel warn
ErrorLog ${APACHE_LOG_DIR}/your_site.error.log
CustomLog ${APACHE_LOG_DIR}/your_site.log combined
</VirtualHost>
In production you should remove app_dev.php and set as root directory symfony web folder
Note: I don't add <IfModule mod_rewrite.c></IfModule> because if you don't have it you want apache to inform you that you are missing this module.
Apache/mod_rewrite configuration
To remove the /web part you can set RewriteBase additionally
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /web/
...
... or set your DocumentRoot directly to the web folder in your apache VirtualHost configuration.
stylesheet issue
The missing stylesheet in the prod environment can be resolved by invoking the assets:install command.
In the dev symfony will serve the files directly from the bundle's Resources/public folders because the default config_dev.yml contains assetic.use_controller: true.
The default production configuration has assetic.use_controller: false for performance reasons. Assets will not be recompiled and served through symfony on every request.
Now the assets in a bundle's Resource folder are not accessible until they are being moved/symlinked to the web folder where your webserver can find them which the assets:install and assetic:dump commands do.

Changing .htaccess file in order to allow subdomains

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>

Mixed Content errors on Wordpress site with Apache VirtualHost

I got Mixed Content errors on wordpress site, after I set up automatic redirection from http to https on apache.
Mixed Content: The page at 'https://example.example.com/' was loaded over HTTPS, but requested an insecure stylesheet http://example.example.com/wp-content/uploads/elementor/css/post-2612.css?ver=1481615259'. This request has been blocked; the content must be served over HTTPS
I want to know if the problem with my htaccess / virtualhost or maybe with the code, css, etc..
My htaccess code looks like this:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</IfModule>
My VirtualHost looks like this:
<VirtualHost *:80>
RewriteEngine On
RewriteCond %{HTTPS} off
RewriteRule ^/(.*) https://%{HTTP_HOST}/$1 [NC,R=301,L]
ServerName example.example.com
ServerAdmin hostmaster#example.com
DocumentRoot /mnt/data/html1
ErrorLog /mnt/data/html1/example.example.com_error.log
CustomLog /mnt/data/html1/example.example.com_access_log combined env=!dontlog
</VirtualHost>
<VirtualHost _default_:443>
SSLEngine on
RewriteEngine On
RewriteCond %{HTTPS} on
SSLCertificateFile /etc/httpd/sites-available/crt.crt
SSLCertificateKeyFile /etc/httpd/sites-available/pem.pem
ServerName example.example.com
DocumentRoot /mnt/data/html1
ErrorLog /mnt/data/html1/example.example.com_error.log
CustomLog /mnt/data/html1/example.example.com_access_log combined env=!dontlog
</VirtualHost>
<Directory "/mnt/data/html1">
AuthType Basic
AuthName "Restricted Content"
AuthUserFile /mnt/data/html1/.htpasswd
Require valid-user
Options Indexes FollowSymLinks
AllowOverride All
Order allow,deny
Allow from all
</Directory>
Tracking down and solving all mixed content issues can be annoying. Here's an excellent guide from google to methodically go through it.
https://developers.google.com/web/fundamentals/security/prevent-mixed-content/fixing-mixed-content
personally i have been using cloudflare's auto rewrite system at DNS level, which is completely turnkey and an excellent trick for the lazy.
Although chrome should identify the difference between content loaded via 3rd party (let's say you have JS vendor lib which is pulling in assets from somewhere, I understand this sometimes contributes to the warning, reviewing your network tab in browser will identify which outbound links are triggering the problem.

Resources