Move .htaccess wordpress directives to apache config file - wordpress

I'm trying to avoid .htaccess files in WordPress. I tried to move all directives to an apache config file, enclosing them into tags, but it doesn't work.
I'm using Apache/2.4.37
My current config at apache is:
<VirtualHost *:443>
ServerAdmin webmaster#mydomain.com
DocumentRoot /var/www/html/myweb
ServerName myweb.mydomain.com
ErrorLog logs/myweb.com-error_log
CustomLog logs/myweb.com-access_log common
SSLCertificateFile /etc/httpd/certificate/myweb.crt
SSLCertificateKeyFile /etc/httpd/certificate/myweb.key
<Directory "/var/www/html/myweb">
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]
</Directory>
</VirtualHost>
Is there any way to avoid .htaccess rewrites?

You need to consider that there are some slight differences in the situation when you move the code.
RewriteRule ^index\.php$ - [L] does not make sense in the global configuration, since RewriteRules work on absolute path there as opposed to distributed configuration files where they work on relative paths ... So it should be RewriteRule ^/index\.php$ - [L] instead, note the leading slash.
RewriteBase / does not make any sense in the global configuration, remove that one.
Move the code block outside the <Directory ...>...</Directory> block. The rules should get applied globally.
You could replace the L flag with the END flag. You may want to take a look into the documentation about that.
And you need to take care that wordpress is happy even if it can not mess around with configuration files.

Related

Wordpress/htaccess - allow access to subdirectory

I have a subdirectory (app) I want to access that is in the root folder of my wordpress site. I have looked here:
https://wordpress.stackexchange.com/questions/20152/cannot-access-non-wordpress-subdirectories-as-wordpress-overrides-them-with-a-40
I have tried the solutions and nothing worked.
I also tried adding a separate .htaccess file to the app subdirectory that looks like this:
DirectoryIndex index.php index.html index.htm
Options +Indexes
But it didn't seem to help:
The main .htaccess I am trying with now looks like this:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_URI} ^/app/(.*)$ [OR]
RewriteRule ^.*$ - [L]
</IfModule>
# 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
I get a 404 error.
What am I doing wrong? If I am viewing the site on an iPhone, is clearing the safari history on the phone enough to refresh the data so it recognizes the new .htaccess? Thanks.
UPDATE
I added:
<Directory "/home/eamondev/public_html/subconscious/">
Options All
AllowOverride All
Require all granted
</Directory>
to a .conf file that gets Includeed in httpd.conf, restarted apache, but didn't help.
I also tried:
<Directory "/home/eamondev/public_html/subconscious/">
AllowOverride All
</Directory>
and it didn't work, I'm not sure if I only need AllowOverride All - either way this doesn't seem to help.
UPDATE
In a .conf file that is Included in httpd.conf, I tried:
<VirtualHost 162.241.180.99:80>
ServerName eamondev.com
DocumentRoot /home/eamondev/public_html
<Directory "/home/eamondev/public_html/">
AllowOverride All
</Directory>
</VirtualHost>
but it didn't help.
Is there any reason I shouldn't just make another subdomain on my server and host the files out of there so reaching them doesn't conflict with my wordpress site?
Use below code i think it will work.
<IfModule mod_rewrite.c>
RewriteEngine On
# Map http://www.example.com to /app.
RewriteRule ^$ /app/ [L]
# Map http://www.example.com/x to /app/x unless there is a x in the web root.
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !^/app/
RewriteRule ^(.*)$ /app/$1
# Add trailing slash to directories within app
# This does not expose the internal URL.
RewriteCond %{SCRIPT_FILENAME} -d
RewriteRule ^app/(.*[^/])$ http://www.example.com/$1/ [R=301]
</IfModule>
Turns out, my domain hadn't propagated yet, so it wasn't possible to see/test any of the changes I was making. Before I realized that it was still propagating, I also thought to just create another subdomain and host the files I needed to from there.

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

Using htaccess how to serve a subfolder as a WordPress page?

What I'm trying to do is server a subfolder: /subsites/brighton/ when the url domain.com/pop-up-in-brighton/availability is visited (that url is/can be a WP page. Doesn't matter to me). The site is build on wordpress so that domain is itself the result of an apache rewrite.
I've looked at RewriteRule examples and tried to adapt them but failed miserably. Can anyone help?
Whether it needs to be a rewriterule or maybe an alias? It's shared hosting so only really have access to the htaccess file.
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^/pop-up-in-brighton/availability /subsites/brighton
RewriteRule . /index.php [L]
</IfModule>
Thanks!
i think what you need to check is the virtual server directive, you can create a new website with it's own folder and IP.
<VirtualHost 10.20.30.1:80>
ServerAdmin webmaster#host.example.com
DocumentRoot /www/docs/host.example.com
ServerName host.example.com
ErrorLog logs/host.example.com-error_log
TransferLog logs/host.example.com-access_log
</VirtualHost>
Virtual server directive
also check this article about virtual hosts and .htaccess
.htacess and VH
You need to install wordpress in your sub directory /subsites/brighton and you can follow these instructions for using a sub directory.
http://codex.wordpress.org/Giving_WordPress_Its_Own_Directory
Then you can point to that directory from /pop-up-in-brighton/availability using mod_rewrite in the .htaccess of you root directory.
RewriteEngine On
RewriteCond %{HTTP_HOST} ^(www\.)?yourdomain\.com$
RewriteCond %{REQUEST_URI} ^/pop-up-in-brighton/availability [NC]
RewriteRule ^(.*)/?$ /subsites/brigton [L]
See how that works. Replace yourdomain with your real domain.

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.

How come I can't move the contents of my .htaccess file into my virtual host?

I have an .htaccess file for my Wordpress website with this content.
# 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
I moved this content into the Apache virtual host for my site (below) and deleted the htaccess file.
<VirtualHost *:80>
ServerName 68.183.153.223
DocumentRoot /home/david/wordpressWebSite
# 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
</VirtualHost>
However, when I attempt to restart Apache, Apache outputs an error.
RewriteBase: only valid in per-directory config files
Action 'restart' failed.
I thought that htaccess file contents could be moved to virtual hosts. What is awry?
According to the docs, RewriteBase "Sets the base URL for per-directory rewrites".
Since you're not setting up per-directory rewrites, the directive is meaningless outside of an .htaccess file for a particular directory.
RewriteBase does not work in virtualhost files.
The question you probably want to ask is:
How do I include relative paths in VHost RewriteRules?
Particularly, in vhosts, *_FILENAME returns a slash followed by the REQUEST_URI, instead of being relative or mapped to the filesystem!
Eg, %{SCRIPT_FILENAME}
In .htaccess: "assets/style.css"
In a vhost file: "/assets/style.css"
You need: "/var/www/assets/style.css"
There are several solutions:
Solution #1: %{DOCUMENT_ROOT}
RewriteRule ^/foo/bar$ %{DOCUMENT_ROOT}/foo/baz
RewriteCond %{DOCUMENT_ROOT}/%{SCRIPT_FILENAME} !-f
RewriteRule (.*) /script.cgi
Solution #2: <Directory> tags
RewriteEngine On
<Directory "/var/www/">
...
RewriteCond %{SCRIPT_FILENAME} !-f
RewriteRule (.*) /script.cgi
</Directory>
Within the directory tag, *_FILENAME is treated the same as in a htaccess file.
There may be an issue using this when the server has multiple aliases.
Solution #3: %{REQUEST_URI}
Use %{_URI} instead of %{_FILENAME} where possible
Not a real answer. Just pointing out that *_URI's won't have to be rewritten as they are not supposed to map to the filesystem, and therefore vhost/htaccess paths are the same. However, it is not always possible to use REQUEST_URI, and David Faux commented above that it is slower. Besides, thi

Resources