Enable mod_rewrite Apache 2.4 on Ubuntu 14.04 - apache

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.

Related

.htaccess RewriteRule Not Working with Apache2

I know there are a million posts about this, but I have read through a lot of them and made so many changes to get this to work, but I am at a loss. I have setup a redirect rule in my apache2 server web server, but it does not redirect.
My web server root folder structure:
/var/www/example/
|
|----l
|
|---index.php
|---.htaccess
/var/www/example/l/.htaccess content is:
$ cat /var/www/example/l/.htaccess
RewriteEngine On
RewriteRule ^l/([a-zA-Z0-9]{6})$ l/index.php?redirect=$1 [R=302,L]
I have also tried putting it in the root folder /var/www/example/.htaccess
My rewrite rule appears to be working correctly from this site:
https://htaccess.madewithlove.be?share=6cae684f-740d-4add-b711-53cdb5986681
mod_rewrite.so is installed and rewrite_module is loaded.
I ran sudo a2enmod rewrite and it was turned on
APache2.conf:
$ cat /etc/apache2/apache2.conf
<Directory "/var/www/example">
Options Indexes FollowSymLinks
AllowOverride All
Require all granted
</Directory>
When ever I go to https://example.com/l/i4Ikn2 I just get a 404 error.
I just want to know if I am missing something or somewhere to look. I checked the apache2 error.log and there are no error in there pertaining to this.
As #mrwhite and #anubhava mentioned in the comments below my OP, I was mixing up the way my .htaccess file was being used.
I removed the sub-directory flags (the l in the pattern) and added the QSA instead of the redirect (R=302).
This was because I was using the .htaccess file in the sub-directory.
The QSA was the correct 'option' for the redirect/rewrite (still unsure of the difference).
Original:
RewriteRule ^l/([a-zA-Z0-9]{6})$ l/index.php?redirect=$1 [R=302,L]
New (working):
RewriteRule ^([a-zA-Z0-9]{6})$ index.php?redirect=$1 [QSA,L]

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!

Redirect https to http on ispconfig+apache

I know that question has already been asked, but for some reason, no matter how I try, redirect from https to http just doesn't work. I tried my default approach to redirect (always worked perfectly fine with http to https, thought it would go that smooth the other way as well) and a few solutions from here, but no effect. For now, this is the last method I've tried:
In Apache sites-available directory, I modified relevant .vhost entry, so the changed part now looks like this:
<Directory /var/www/SOMEWEBSITE/web>
Options FollowSymLinks
AllowOverride All
Order allow,deny
Allow from all
RewriteEngine On
RewriteCond %{HTTPS} on
RewriteRule (.*) http://%{HTTP_HOST}%{REQUEST_URI} [NC,R=301,L]
</Directory>
<Directory /var/www/clients/client1/web1/web>
Options FollowSymLinks
AllowOverride All
Order allow,deny
Allow from all
RewriteEngine On
RewriteCond %{HTTPS} on
RewriteRule (.*) http://%{HTTP_HOST}%{REQUEST_URI} [NC,R=301,L]
</Directory>
Still, connecting via https://SOMEWEBSITE gives me standard apache blank site
(It works!
This is the default web page for this server.
The web server software is running but no content has been added, yet.)
I tried inserting the rewrite rules directly in VirtualHost block and modyfing site's .htaccess file (same effect as above), also tried to create VirtualHost listening on port 443 and redirecting traffic to http, but this approach gave me an SSL error. Is there any other way or something I might be doing wrong? Or is that some ISPConfig issue I am not aware of?
You could try this solution, here on stackoverflow. It is not done within ISPConfig, but I do not think there is a way to do this from within the ISPConfig GUI anyway.

Redirecting https to http ISPConfig & Apache

some time ago I managed to find a way to redirect all my https to http (and not the opposite) with Apache.
I wanted to do that because I don't have any SSL certificate but all the browser I use force https, therefore leading to SSL error, or timeout depending on if I listen to the 443's port or not.
But because I'm stupid, I forgot to save the vhost files for Apache containing that solution and I can't make it work again.
I tried everything on the internet but nothing worked.
If that changes anything, I use ISPConfig with Apache.
I tried putting rewrite rules in the .htacess and nothing happened.
Tried these same rules in the default-ssl.vhost of apache and nothing happened again.
It's driving me mad.
This is the rules I tried and seems to work for everyone but me.
RewriteEngine On
RewriteCond %{HTTPS} on
RewriteRule (.*) http://%{HTTP_HOST}%{REQUEST_URI}
I may not be putting this at the right place, since I don't know a lot about Apache and server configuration, but since I tried to put it everywhere I'll be damn if I missed the spot.
Can someone please indicate me where to put these rules, or help me find a solution for my redirection problem before I cry ?
After wasting my whole night on that idotic problem, I finally found out that since Zend put the boostrap and access files in the root/public directory, I just had to add
<Directory "/var/www/<link to site made by ISPConfig>/web/public">
AllowOverride All
Order allow,deny
Allow from all
RewriteEngine On
RewriteCond %{HTTPS} on
RewriteRule (.*) http://%{HTTP_HOST}%{REQUEST_URI} [NC,R=301,L]
</Directory>
<Directory "/var/www/clients/client0/web1/web/public">
AllowOverride All
Order allow,deny
Allow from all
RewriteEngine On
RewriteCond %{HTTPS} on
RewriteRule (.*) http://%{HTTP_HOST}%{REQUEST_URI} [NC,R=301,L]
</Directory>
Instead of
<Directory "/var/www/<link to site made by ISPConfig>/web">
AllowOverride All
Order allow,deny
Allow from all
RewriteEngine On
RewriteCond %{HTTPS} on
RewriteRule (.*) http://%{HTTP_HOST}%{REQUEST_URI} [NC,R=301,L]
</Directory>
<Directory "/var/www/clients/client0/web1/web">
AllowOverride All
Order allow,deny
Allow from all
RewriteEngine On
RewriteCond %{HTTPS} on
RewriteRule (.*) http://%{HTTP_HOST}%{REQUEST_URI} [NC,R=301,L]
</Directory>
It was just a damn story of Directories
I hope that it will somehow help someone or at least help myself when I'll forget again.

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.