MAMP Apache Server - One Subdirectory Works, Another Doesn't - apache

I am running an Apache server through MAMP.
I have two directories inside htdocs: TWS and SHR
SHR and TWS are two totally separate wordpress installations each with their own .htaccess files (which were previously working).
The .htaccess files are identical except TWS is replaced by SHR
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /TWS/
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /TWS/index.php [L]
</IfModule>
My problem is that localhost:81/TWS is accessible while localhost:81/SHR returns a 500 error.
apache/httpd.conf contains a directory for htdocs
<Directory "F:\MAMP\htdocs">
Options All
AllowOverride All
Order allow,deny
Allow from all
</Directory>
and the DirectoryIndex is set to use index.html or index.php.
Both TWS and SHR have an index.php file at their root level.
I have checked all of the logs that I can find related to apache, php, mamp, and even mysql. None of the logs indicate an error.
What else can I try?

It turns out there was just a simple syntax error in one of the PHP files that I was editing.
The error got logged inside the wordpress log itself rather than the MAMP php logs.
That is, MAMP/htdocs/SHR/wp-content/debug.log
Once I resolved the php syntax error, everything worked fine again.

Related

Deploy a CakePHP website fails. An apache2 config issue?

I'm deploying a CakePHP (1.3.2) website for the first time. It was hosted on an older server. I received the project as a zip file. I managed to install it on my localhost and made the changes I needed.
Now I have to deploy it to a new server, but I face a problem.
The routing doesn't seem to work. I guess it's an .htaccess issue.
When I access the root folder, it redirects me to /login but then I have a 404:
The requested URL /login was not found on this server.
My 3 main .htaccess files (/, /app and /app/webroot) are the following. (CakePHP is installed at the root of my virtual host)
Root
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteRule ^$ app/webroot/ [L]
RewriteRule (.*) app/webroot/$1 [L]
</IfModule>
/app
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteRule ^$ webroot/ [L]
RewriteRule (.*) webroot/$1 [L]
</IfModule>
/app/webroot
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ index.php?url=$1 [QSA,L]
</IfModule>
mod_rewrite seems to be activated on my server as it responds with this when I try to add it:
Module rewrite already enabled
But when I try something simple like that on top of my root .htaccess, it doesn't do anything:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteRule ^.*$ test.php
</IfModule>
(I was following this guide: https://docs.bolt.cm/howto/making-sure-htaccess-works)
Among a lot of things, I also tried to add that to all my .htaccess:
Option Indexes
But it didn't help.
Here is my website conf file too:
<VirtualHost xx.x.xx.xx:80>
ServerAdmin xxx#company.com
ServerName xxx.company.com
DocumentRoot /var/www/xxx.company.com
DirectoryIndex index.html index.php
php_value error_log "/var/log/apache2/xxx.company.com-phperror.log"
php_flag register_globals off
<Directory "/var/www/xxx.company.com">
Options Indexes FollowSymLinks
AllowOverride All
Order allow,deny
Allow from all
</Directory>
ErrorLog /var/log/apache2/xxx.company.com-error.log
CustomLog /var/log/apache2/xxx.company.com-access.log common
</VirtualHost>
(I added the Directory section that was not here in the first place)
After a lot of trials, I still haven't found anything that seems to solve my problem.
As I'm definitely not used to work on server side, It might be a simple thing that you will immediately spot. I hope so.
Thanks
I finally got it to work. Here are the two mistakes I made:
I had my document root set cake's root folder instead of the webroot folder. I added /app/webroot after DocumentRoot /var/www/xxx.company.com in my .conf file.
Also, I was using apache's reload function, which is actually not properly reloading. Using service apache2 restart instead does the job.

AllowOverride and RewriteCond/RewriteRule interactions

I'm trying to set up a site running on Apache 2.4.16 to redirect all www URLs to non-www URLs. I'm using HTML5 Boilerplate's Apache configs to do this (as well as everything else they provide).
https://github.com/h5bp/server-configs-apache/blob/master/dist/.htaccess
This happens on line 380, seen below:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{HTTPS} !=on
RewriteCond %{HTTP_HOST} ^www\.(.+)$ [NC]
RewriteRule ^ %{ENV:PROTO}://%1%{REQUEST_URI} [R=301,L]
</IfModule>
I'm using Include to add the whole file to my vhost config for the site, as well as an AllowOverride All for another .htaccess file at my doc root (same one that comes with Laravel 5):
production.vhost.conf (relevant part)
<Directory /var/www/hostname/production>
AllowOverride All
# Include H5BP server configs
Include server-configs-apache/dist/.htaccess
</Directory>
.htaccess (at doc root)
<IfModule mod_rewrite.c>
<IfModule mod_negotiation.c>
Options -MultiViews
</IfModule>
RewriteEngine On
# Redirect Trailing Slashes If Not A Folder...
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)/$ /$1 [L,R=301]
# Handle Front Controller...
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [L]
</IfModule>
Now, almost everything from H5BP's .htaccess was working, except for the redirect from www to non-www. After poking around I noticed that the redirect was only working when I'd remove AllowOverride All from the <Directory> block in the vhost. So the doc root .htaccess was somehow overriding the rewrite conditions.
I've actually already fixed my initial issue by moving the doc root .htaccess contents into the vhost file and removing the AllowOverride, but I'm more curious as to why this was happening; more specifically how AllowOverride interacts with RewriteCond and RewriteRule.
My hunch is that the .htaccess in my doc root was overriding the www to non-www redirect, but I'm not sure why that one specifically. For example, the http -> https redirect worked without issue (line 352 of H5BP, uncommented out in mine), it seemed to be just that one redirect. I didnt even think that those rules could be overridden since RewriteCond/RewriteRules feel unique to me.
If there are any, what are the rules that determine how an .htaccess can override a rewrite rule?
If there are any, what are the rules that determine how an .htaccess
can override a rewrite rule?
Conditions and rules don't dictate how .htaccess works. AllowOverride is what allows .htaccess usage. If you have AllowOverride All then .htaccess is allowed, if you have AllowOverride None, then it's not and it will be ignored. In 2.4 None is the default.
.htaccess is per directory so it will take precedence if it's located in a directory that has rules applied as long as .htaccess file usage is allowed there. Which is confgiured in the server config in VirtualHost or Directory directives.
Also using an include for .htaccess in a vhost is a very bad configuration. If you have access to the vhost file or the config, you should create another config and include it with the .htaccess contents.
You should not be using .htaccess files at all actually with access to the server config. See this apache recommendation on not using .htaccess.
https://httpd.apache.org/docs/2.4/howto/htaccess.html#when

mod_rewrite all to index.php in one environment

I know there are a million of questions about this, but I've tried the solutions in other questions and haven't got it to work in my case. I'm trying to redirect everything to index.php:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [L]
</IfModule>
This works in my local environment, but in my staging environment it fails and I get a 404 error. I've checked that mod_rewrite is enabled with phpinfo(). I've tried changing a few things like using ^(.*)$ instead of ^ and using /index.php instead of index.php
What else could be the problem?
Figured it out. It was a problem with the configuration of apache. Whoever set it up for the site I'm working on did it like this in the site's .conf file:
<Directory "/data/path/to/directory">
Options FollowSymLinks
AllowOverride None
Order allow,deny
Allow from all
# and a bunch of rewrite rules...
</Directory>
The AllowOverride None prohibits the use of .htaccess. In our local and development environments, the directory structure is different, so this code doesn't even get applied. That's why the .htaccess was working.
To solve the problem I'll either have to change the line mentioned above to AllowOverride All or add another configuration for the website I'm working on like the one above that does what I wanted to do with .htaccess.
Problems like this shouldn't come up for anyone if their environments are configured appropriately, but if anyone does run into a similar situation, check the configuration of the site in apache!

.htaccess error 500 or access forbidden

Here is my .htaccess:
<Files.htaccess>
order allow,deny
</Files>
Options +FollowSymLinks
RewriteEngine On
RewriteRule ^(\w+)$ ./redirect.php?url_token=$1
My script's logic:
My script is not in the root of the website, it's in a folder (ex: www.mywebsite.com/script/). I have the .htaccess file in this folder.
The PHP script gets that url_token value and processes it, it is working well, i've tested it.
But when I use this .htaccess file and I try to access an URL like www.mywebsite.com/script/fa34d where fa34d is a random generated code, I get:
Error 500 on XAMPP and Access Forbidden on another online host.
What can be the problem?
I've already spent more time with this than the rest of the script.
You're likely creating a loop or you're redirecting to a file outside of your document root. If the folder accessed as the root is that same as what displays as www.mywebsite.com then you can set RewriteBase / in your access file. Remember all .htaccess files work together and also work with your VirtualHosts directives on the server.
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(\w+)$ /redirect.php?url_token=$1
You just need to add this line to hide your directory from http request in your .htaccess file
Options -Indexes
Enjoy!!

Making pretty permalinks work in WAMP

I am not able to switch to pretty permalinks in WAMP. Changing to any form other than default gives 404 error.
I have switched on the rewrite_module in Apache. I Googled the problem and found that following changes should be made to httpd.conf file. My httpd.conf file stands as
<Directory />
Options Indexes FollowSymLinks
AllowOverride All
Order deny,allow
Deny from all
</Directory>
I also checked that the .htaccess file is getting created. It reads as
# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /vit%20web/events/
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /vit%20web/events/index.php [L]
</IfModule>
# END WordPress
All you need to do is turn on the mod_rewrite in the wamp settings tab.
Click Wamp -> Apache -> Apache Modules -> rewrite_module
If you select that then turn on %postname% it should work
Did you reboot Apache after editing httpd.conf?
A bulletproof check for mod_rewrite is to remove the <IfModule>...</IfModule> tags and try running WordPress - if you get a 500 Server Error, mod_rewrite isn't installed.
Also I would recommend changing <Directory /> to <Directory "C:/path/to/server/root"> (note the forward slashes too, even on Windows).
And the deny order should be switched if you're only on a development server;
Order allow,deny
Allow from all
The problem may be:
Whenever you have permalinks in subfolder of your main www folder (i.e.
RewriteBase /subdirectory/
you may need to have a .htaccess file in your main c:\wamp\www folder too.