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.
Related
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.
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.
I have a file with an identical name that exists in 15 subdirectories. Let's say it's called blah.txt.
I'm looking to do a global rewrite within httpd.conf to match any subdirectory's blah.txt and Rewrite it to a singular http://myserver/blah.txt.
If I add the following to an .htaccess, it works great. I hit the subdirectory's txt file, and I'm 301 forwarded. However doing this means I need to manage 15 .htaccess's. I'm trying to avoid that.
RewriteEngine On
RewriteRule ^(blah)\.txt http://myserver/blah.txt [L,R=301]
If I put this same text into httpd.conf under the <Directory /> section, I get a 404 on the subdirectory's blah.txt.
<Directory />
Options All Indexes FollowSymLinks
AllowOverride All
Allow from all
RewriteEngine On
RewriteRule ^(blah)\.txt http://myserver/blah.txt [L,R=301]
</Directory>
How can I have this global match within httpd.conf?
Edit: If I change the httpd.conf rewrite to the below, it seems like it wants to work, however I get stuck in an infinite loop because it's constantly matching the same file name.
RewriteEngine On
RewriteRule blah.txt http://myserver/blah.txt [L,R=301]
Still looking for a way to have it match in subdirectories and redirect to the same filename.
Edit 2: I think I got this working - but would love confirmation!
I added a RewriteCond to my rule. Basically, I think, this means that if the URI does not contain "firmware", then rewrite.
<Directory />
Options All Indexes FollowSymLinks
AllowOverride All
Allow from all
RewriteEngine On
RewriteCond %{REQUEST_URI} !(firmware) [NC]
RewriteRule blah.txt firmware/series/version/blah.txt [R=301,L]
</Directory>
It seems to be working without looping
As per my Edit 2: I think I got this working.
I added a RewriteCond to my rule. Basically, I think, this means that if the URI does not contain "firmware", then rewrite.
<Directory />
Options All Indexes FollowSymLinks
AllowOverride All
Allow from all
RewriteEngine On
RewriteCond %{REQUEST_URI} !(firmware) [NC]
RewriteRule blah.txt firmware/series/version/blah.txt [R=301,L]
</Directory>
After quite a bit of testing, it seems to be working without looping
I have a website say www.abcd.com. Its working fine if i access it using url http://abcd.com. But what i want is that if user go with url http://abcd.com, then web server should be able to convert it into url www.abcd.com.
%LOCATION_CONTAINS_HTACCESS_FILE% = Some Path
Some important changes in httpd.config are:
1. DocumentRoot "/var/www/html"
2. <Directory />
Options FollowSymLinks
AllowOverride All #changed to All
</Directory>
3. <Directory "%LOCATION_CONTAINS_HTACCESS_FILE%">
AllowOverride All
</Directory>
4. AccessFileName .htaccess #default
5. %LOCATION_CONTAINS_HTACCESS_FILE%/.htaccess # I added
6. <Files ~ "^\.ht">
Order allow,deny
Deny from all
</Files>
My .htaccess
RewriteCond %{HTTP_HOST} !^$
RewriteCond %{HTTP_HOST} !^www\. [NC]
RewriteCond %{HTTPS}s ^on(s)|
RewriteRule ^ http%1://www.%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
With these changes I am getting error in line 5(from above, i think) when executing "service httpd restart":
Starting httpd: Syntax error on line 415 of /etc/httpd/conf/httpd.conf:
Invalid command '%LOCATION_CONTAINS_HTACCESS_FILE%/.htaccess', perhaps misspelled or defined by a module not included in the server configuration
I went through most of links on web, but couldn't get any specific solution to this.
I don't have much knowledge about Web services. But waiting for a simpler solution.
Hoping you guys have faced this issue & surely solved.
I always use the snippet below:
RewriteEngine On
RewriteBase /
# Redirect non-canonical domains
RewriteCond %{HTTP_HOST} !^www.yourdomain.com$ [NC]
RewriteRule ^(.*)$ http://www.yourdomain.com/$1 [L,R=301]
# Redirect non-www to www version
RewriteRule %{HTTP_HOST} !^www\. [NC]
RewriteCond %{HTTP_HOST} ^([^.]+\.[a-z]{2,6})$ [NC]
RewriteRule ^(.*)$ http://www.%1/$1 [R=301,L]
It does two things:
The first snippet (below "Redirect non-canoncial domains") redirects visitors who come in from an alias (eg. my-domain-alias.com) to the main domain (yourdomain.com). It uses a 301 redirect (permanent) to let search engines know this is the right address (and not some duplicate content).
It checks if the www prefix is used. If not, it redirects, and also redirects using the 301 status code.
Note: the NC flag tells apache to check ignoring the case.
-- Edit:
I'd change the order directive to:
# Protect hidden files from being viewed
<Files .*>
Order Deny,Allow
Deny From All
</Files>
This will deny access to ALL hidden files (eg. your php .user.ini file, if present), not only .htaccess/.htpasswd files etc.
Really can't find my mistake, I can access the sitemap/ link, but all the others give a 404 Not Found Error.
DirectoryIndex index.php index.php?page=home index.php?page=error
Options Indexes FollowSymLinks MultiViews
AllowOverride All
Order allow,deny
Allow from all
<IfModule mod_rewrite.c>
RewriteEngine On
# some other stuff #
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteBase /
RewriteRule ^sitemap/?$ sitemap.xml.php [L]
RewriteRule ^([a-zA-Z]+)/?$ index.php?page=$1 [L]
RewriteRule ^products/?$ index.php?page=products [L]
RewriteRule ^products/([0-9]+)/?$ index.php?page=products&id=$1 [L]
</IfModule>
I'd really appreciate some help, I definitely suck at .htaccess files...
Rewriting rules aside, you have other problems here:
DirectoryIndex index.php index.php?page=home index.php?page=error
This means, that, for a folder a file showing index will be selected in this order (first one existing wins):
index.php
index.php?page=home (this is the filename)
index.php?page=error (this is the filename)
I doubt this was your intention.
Next:
AllowOverride All
If you're already in .htaccess and AllowOverride was restrictive, this will not help, and if it wasn't, then there is no point of writing this again.
Order allow,deny
Allow from all
This basically says "allow everyone everywhere" which is the default anyway. Unless it was restricted in some way on upper level or in apache configuration, this is redundant.
As for rewrite:
RewriteRule ^([a-zA-Z]+)/?$ index.php?page=$1 [L]
RewriteRule ^products/?$ index.php?page=products [L]
The second rule is redundant anyway, and will never be reached (as the first one matches it as well).
As for the error you're seeing:
[Wed Jul 04 02:56:30 2012] [error] [client 127.0.0.1] File does not exist: /var/www/home"
This may mean problems with DocumentRoot definitions. Please, post your VirtualHosts configuration.