mod_rewrite all to index.php in one environment - apache

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!

Related

Croogo - CakePHP site on new VPS not loading js and css

I've recently purchased a VPS on inmotionhosting.com, and having difficulty deploying my Croogo 2.3.2 when loading any js, css, img files under my custom theme /View/Themed/Mosaic/webroot and under my Croogo plugins /Vendor/croogo/croogo/Croogo/webroot/.
Of course, I've included the said resources using the proper CakePHP convention in my /View/Themed/Mosaic/Layouts/default.ctp:
echo $this->Html->css(array(
'bootstrap',
'bootstrap-theme',
'/croogo/css/reset',
'font-awesome.min',
'../slick/slick',
'../slick/slick-theme',
'/jquery-ui/jquery-ui.min',
'slicknav.min',
'styles'
));
echo $this->Layout->js();
echo $this->Html->script(array(
'/croogo/js/html5',
'/croogo/js/jquery/jquery-3.2.0.min',
'/jquery-ui/jquery-ui.min',
'/croogo/js/jquery/jquery.slug',
'/croogo/js/jquery/jquery.cookie',
'/croogo/js/jquery/jquery.hoverIntent.minified',
'/croogo/js/jquery/superfish',
'/croogo/js/jquery/supersubs',
'/croogo/js/jquery/jquery.tipsy',
'/croogo/js/jquery/jquery.elastic-1.6.1.js',
'/croogo/js/jquery/thickbox-compressed',
'/croogo/js/underscore-min',
'bootstrap.min',
'../slick/slick.min',
'jquery.slicknav.min',
'theme.js',
));
echo $this->fetch('script');
echo $this->fetch('css');
Croogo 2.3.2 has itself and cakephp run as a Vendor:
/Vendor/croogo
/Vendor/cakephp
Therefore, I have two .htaccess files.
One under /home/mosaic40/v2.mosaicmarble.com/:
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteBase /
RewriteRule ^$ webroot/ [L]
RewriteRule (.*) webroot/$1 [L]
</IfModule>
One under /home/mosaic40/v2.mosaicmarble.com/webroot/:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ index.php?/$1 [QSA,L]
</IfModule>
mod_rewrite is installed and activated.
Last but not least, before anyone starts suggesting about my httpd config, here it is:
DocumentRoot /home/mosaic40/v2.mosaicmarble.com/
<Directory />
Options FollowSymLinks
AllowOverride All
</Directory>
<Directory />
Options Indexes FollowSymLinks MultiViews
AllowOverride All
Order Allow,Deny
Allow from all
</Directory>
I can't seem to put my finger on it. in short, the plugins' and theme's resources (js/css/img) are not being loaded for some reason.
UPDATE 1:
It appears /webroot assets are not being loaded as well (example: http://v2.mosaicmarble.com/uploads/FG204-G.jpg).
The said link is http://v2.mosaicmarble.com/ (to see what's being rendered).
Croogo v2.3.2
CakePHP v2.9.3
Linux Server with NGINX installed
mod_rewrite active (click on any link, you'll see pretty urls)
When you are fetching assets for plugins via the dispatcher. You have to use a plugin namespace as the URL.
echo $this->Html->script('Croogo.html5');
See the manual on linking to plugin assets:
https://book.cakephp.org/3.0/en/plugins.html#linking-to-assets-in-plugins
The evident solution in the end was to disable NGINX... which is weird, because I own several other servers like Media Temple's VPS with NGINX installed and all my websites run flawlessly.
So, yea... disabling NGINX did the job.
Thank you #cgTag for your prompt response, it does help others in the future who may face a similar issue.

.htaccess not working on SSL apache

Trying to fix this .htaccess, so it can work on a SSL apache server. Before it was hosted on another Linux server (http://) and was working without problems, but when uploading the files to another Linux server with apache and SSL (https://), it stopped working. The main function is to hide the .php extension...
HereĀ“s what I was using:
Options +FollowSymLinks -MultiViews
# Turn mod_rewrite on
RewriteEngine On
RewriteBase /
# To externally redirect /dir/foo.php to /dir/foo
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s([^.]+)\.php [NC]
RewriteRule ^ %1 [R,L,NC]
## To internally redirect /dir/foo to /dir/foo.php
RewriteCond %{REQUEST_FILENAME}.php -f [NC]
RewriteRule ^ %{REQUEST_URI}.php [L]
Thanks for your help!
So, .htaccess files may not be allowed, and by default on many systems they are not allowed. To see if .htaccess is allowed, make this your .htaccess:
BREAK IT!
That should be the only contents in your .htaccess. Attempt to load any page on your website, and if .htaccess usage is enabled, you would see "Internal Server Error" or possibly some other error, but you would not see your actual page.
If you do see the error, that's actually good and means .htaccess usage is enabled. If you don't see the error, it's likely that you will have to find your Apache .conf file and inside look for the line(s):
AllowOverride None
Change that to:
AllowOverride All
If after doing that you still can't use .htaccess, then there may be other apache related files that have "AllowOverride None". On that comes to mind is your virtual host file, and on my system that is located at /etc/apache2/sites-available/.
If you still have problems, check this out:
https://docs.bolt.cm/3.3/howto/making-sure-htaccess-works
Solution is for the issue, need to change in apache2.conf file after that it will works,
Change this file /etc/apache2/apache2.conf
update it same
OLD:
<Directory /var/www/>
Options Indexes FollowSymLinks
AllowOverride None
Require all granted
</Directory>
New Updated Code:
<Directory /var/www/>
Options Indexes FollowSymLinks
AllowOverride All
Require all granted
</Directory>
I had a similar problem. Apache 2.4.23 with .htaccess error 404 set up.
Using it with HTTP works fine; access it with HTTPS didn't work.
I seted AllowOverride All in the http.conf and error 404 wors well.

Enable mod_rewrite Apache 2.4 on Ubuntu 14.04

So I've gone through most of the questions on this topic, but I still seem to having issues. I can't seem to rewrite any URLs using an htaccess.
Details: Using Apache 2.4 and Ubuntu 14.04
I do have mod_rewrite enabled as can be seen by the following two pieces of information:
$: apachectl -M
Loaded Modules:
....
rewrite_module (shared)
....
$: ls /etc/apache2/mods-enabled/
.... rewrite.load ....
I tried restarting apache using the following three methods:
$: service apache2 restart
$: apachectl restart
$: apachectl graceful
And nothing is working. In order to test things fully, I added a log level in /etc/apache2/apache2.conf as follows:
LogLevel notice rewrite:trace8
Which if I understand correctly means that almost all information on rewrite should be going to my error logs, BUT I am not getting any log information.
Finally, the rewrite portion in my .htaccess is as follows:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^[^.]+\.[^.]+$
RewriteRule ^(.*)$ http://www2.%{HTTP_HOST}/$1 [L,R=301]
I put www2 instead of www because for some reason it's already redirecting website.com to www.website.com so I'm wondering if it's the browser that's automatically doing it. Oddly, it also automatically changes website.com/index.php to www.website.com
Is there anything I might be missing that might be causing this? It seems like it's rewriting something (or it's the browser doing something), but when I try and rewrite it to a bad url for it to fail, it doesn't fail and instead goes to the proper url.
Any thoughts?
Edit
Forgot to include that, I have the following in my directory:
<Directory />
Options FollowSymLinks
AllowOverride None
Require all denied
</Directory>
<Directory /var/www/>
Options Indexes FollowSymLinks
AllowOverride All
Require all granted
</Directory>
So I do have AllowOverride set to All for the directory that everything lives in.
I assume that you want to force www URLs.
Now, if you're using just one domain (i.e: example.com), try:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^example\.com$
RewriteRule ^(.*)$ http://www.example.com/$1 [L,R=301]
If you have several domains over there, try:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^[^\.]+\.[^\.]+$
RewriteRule ^(.*)$ http://www.%{HTTP_HOST}/$1 [L,R=301]
Note that this one won't match third (or more) level domains. To deal with these, consider using RewriteCond %{HTTP_HOST} !^www\., but again, watch out for sub-domain cases.
Also watch for QSA flag in your RewriteRules, you may need to add it.

Mod_ReWrite doesn't work on cakephp

I have recently deployed CakePHP however mod_Rewrite is not currently working:
I have the following in my apache2.conf
<Directory "/path/to/the/app">
Options FollowSymLinks
AllowOverride All
Require all granted
</Directory>
i have run
sudo a2enmod rewrite
which stated the module is already enabled and i have also checked the .htaccess file which has
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteRule ^$ webroot/ [L]
RewriteRule (.*) webroot/$1 [L]
</IfModule>
has anyone got any other ideas as to why mod_rewrite isn't working?
Please note i have restarted apache with no sucess
There must be 3 .htaccess files in these locations
/.htaccess
/app/.htaccess
/app/webroot/.htaccess
Do you have them?
Turns out mod_rewrite was working, however I was using cakephp 1.3 which doesn't detect if the mod_rewrite works but applys a CSS style which wasn't applying to the div. it works now anyways

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.