Redirect root HTTP to root HTTPS - .htaccess

I want that if someone visits http://domain.com that he is redirected to https://domain.com
The .htaccess file is located at /var/www/html with the following content
<ifmodule mod_rewrite.c="">
RewriteEngine On
RewriteCond %{HTTPS} off
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} [R=301,L]
</ifmodule>
My apache2.conf contains:
<Directory /var/www/html>
Options FollowSymLinks
AllowOverride All
Require all granted
SSLRequireSSL
</Directory>
But it does not work, displaying:
Forbidden
You don't have permission to access / on this server.
Edit: I removed the SSLRequireSSL line and disabled the cache in Firefox that saves redirections. If I visit http://domain.com it redirects me to https://domain.com. But still it does not do it for subdirectories as well. If I visit http://domain.com/sub I want to be redirected to https://domain.com

The subdirectory I was talking about was a special one... Didn't think that it would matter. It is an owncloud installation which is shipped with an own .htaccess. My .htaccess was overwritten by that one which did not include my rewrite rule.
My second mistake was, like already mentioned that I had the SSLRequireSSL property set. If this is activated it seems that any .htaccess is just ignored.
I also removed the enclosing if statments from the .htaccess like anubhava suggested.

Related

Trailing slash vs no trailing slash NO REDIRECT

I have to be able to direct a GET or POST request when it comes in to
/eSCL/ScannerStatus
or
/eSCL/ScannerStatus/
TO
/eSCL/ScannerStatus/index.php
I am NOT looking for a redirect, as that is what I am getting now. (301 Permanently moved)
It is to say whether trailing slash or not, the request references a directory and I need to load index.php in that directory with NO redirect
I have reviewed the docs here
https://httpd.apache.org/docs/2.4/mod/mod_dir.html
and quite frankly it does not seem to work as stated or maybe I am missing a mod?
I have tried many variations .
I have run
sudo a2enmod rewrite
and
sudo a2enmod dir
both now show as enabled.
Apache 2.4 running on Raspberry Pi3
I have tried MANY MANY different rules and I also see in google results that this is a problem for many others trying to do the same. I see numerous "solutions" posted many of which do not work.
I see no reason to ressurect the 20+ mod rewrite attempts that I have made and failed . I am looking for what works without a redirect
Edit-UPDATE-------------------------------------------------
I just got back to this , deleted .htaccess, and now server refuses to load index.php and shows a directory listing instead.
This leaves me perplexed. I do not believe I need .htaccess for PHP files as it was not there before.
EDIT UPDATE 2------------------------------------
I managed to get back on track with some directives that seemed to go away for some reason.
I now have in /var/www/html/.htaccess
DirectorySlash Off
RewriteEngine On
RewriteRule ^eSCL/ScannerStatus/?$ eSCL/ScannerStatus/index.php [L]
RewriteRule ^eSCL/ScannerCapabilities/?$ eSCL/ScannerCapabilities/index.php [L]
RewriteRule ^eSCL/ScanJobs/?$ eSCL/ScanJobs/index.php [L]
RewriteRule ^eSCL/Scans/?$ eSCL/Scans/index.php [L]
in the global secion of apache2.conf I have (this is apparently what I had to do to get index.php files to load as default again):
<Directory "/var/www/html">
DirectoryIndex index.php index.htm
</Directory>
<Directory "/var/www/html/scans/*">
DirectoryIndex index.php index.htm
</Directory>
<Directory "/var/www/html/eSCL/ScannerStatus">
DirectoryIndex index.php index.htm
</Directory>
<Directory "/var/www/html/eSCL/ScannerCapabilities">
DirectoryIndex index.php index.htm
</Directory>
<Directory "/var/www/html/eSCL/ScanJobs">
DirectoryIndex index.php index.htm
</Directory>
Here is what wireshark captures when requesting http://192.168.1.50/eSCL/ScannerStatus (no trailing slash)
HTTP/1.1 301 Moved Permanently
Date: Thu, 06 Feb 2020 16:56:00 GMT
Server: Apache/2.4.25 (Raspbian)
Location: http://192.168.1.50/eSCL/ScannerStatus/
Content-Length: 327
Keep-Alive: timeout=5, max=99
Connection: Keep-Alive
Content-Type: text/html; charset=iso-8859-1
<!DOCTYPE HTML PUBLIC "-//IETF//DTD HTML 2.0//EN">
<html><head>
<title>301 Moved Permanently</title>
</head><body>
<h1>Moved Permanently</h1>
<p>The document has moved here.</p>
<hr>
<address>Apache/2.4.25 (Raspbian) Server at 192.168.1.50 Port 80</address>
I have also specifically excluded the sites-enabled files in apache2.conf with
# Include the virtual host configurations:
# IncludeOptional sites-enabled/*.conf
I have also verified:
pi#raspberrypi:/etc/apache2 $ sudo a2enmod rewrite
Module rewrite already enabled
pi#raspberrypi:/etc/apache2 $ sudo a2enmod dir
Module dir already enabled
/eSCL/ScannerStatus
If ScannerStatus is a physical directory then by default, mod_dir will append the slash with a 301 redirect in order to "fix" the URL.
You need to override this behaviour and set DirectorySlash Off. You will then need to internally rewrite the request using mod_rewrite to the desired index.php document (instead of relying on DirectoryIndex as you would normally).
For example, in your root .htaccess file:
DirectorySlash Off
RewriteEngine On
RewriteRule ^eSCL/ScannerStatus/?$ eSCL/SCannerStatus/index.php [L]
Note that you will need to clear your browser cache as the 301 redirect to append the slash (by mod_dir) will most likely have been cached by the browser.
I do not believe I need .htaccess for PHP files as it was not there before.
You need .htaccess if you want to override default server behaviour and use physical directories without trailing slashes.
UPDATE:
<Directory "/var/www/html">
DirectoryIndex index.php index.htm
</Directory>
<Directory "/var/www/html/scans/*">
DirectoryIndex index.php index.htm
</Directory>
<Directory "/var/www/html/eSCL/ScannerStatus">
DirectoryIndex index.php index.htm
</Directory>
<Directory "/var/www/html/eSCL/ScannerCapabilities">
DirectoryIndex index.php index.htm
</Directory>
<Directory "/var/www/html/eSCL/ScanJobs">
DirectoryIndex index.php index.htm
</Directory>
The <Directory> container applies to the stated directory and all subdirectories. So in the above, all except the first <Directory "/var/www/html"> container are not required.
However, there's nothing here that is enabling .htaccess overrides? And the behaviour you are currently seeing would seem to suggest that .htaccess files are not enabled - since the .htaccess directives don't appear to be doing anything.
TEST: Add any "nonsense" to the top of the .htaccess file. If .htaccess files are being processed then this will break horribly and result in a 500 Internal Server Error. If no error then the .htaccess file is being ignored (ie. .htaccess files are not enabled).
The rewrite we added in .htaccess to route requests to index.php negates the need for DirectoryIndex - but there is no harm adding this.
To enable .htaccess overrides you need to set AllowOverride in the relevant <Directory> container. For example:
<Directory "/var/www/html">
# Allow .htaccess files to override config directives
AllowOverride All
# Allow mod_dir to issue an internal subrequest for the DirectoryIndex
# when requesting /subdirectory/ (note the trailing slash)
DirectoryIndex index.php index.htm
# Access needs to be permitted (somewhere)
Require all granted
</Directory>
If you have access to the server config then you don't need .htaccess. All the directives in .htaccess (a directory context) can go directly in the relevant <Directory> container. (Although development/distribution requirements could make the use of .htaccess files easier.)
Incidentally, the DirectoryIndex directive can be used in .htaccess.
Aside:
As noted earlier, the above rewrite in .htaccess negates the need for DirectoryIndex index.php. However, they can be modified slightly to make use of this. For example (an academic excercise):
DirectoryIndex index.php
DirectorySlash Off
RewriteEngine On
# Note the different regex having removed "/?"
RewriteRule ^eSCL/ScannerStatus$ eSCL/SCannerStatus/index.php [L]
Now, a request for /eSCL/ScannerStatus (no trailing slash) is rewritten using mod_rewrite to index.php. But a request for /eSCL/ScannerStatus/ (with a trailing slash) is now handled by DirectoryIndex which returns index.php via an internal subrequest. With the earlier RewriteRule, both these requests were handled by mod_rewrite.
Note that the 4 rules for the 4 different sub-subdirectories can potentially be combined into a single rule.

Redirect non www. URL to URL with www

To let people access my website whether they type the www. in front or not where do I place the .htaccess file to do this as I have tried it and it won't work.
I now know that I need to place the .htaccess on the non www. server and need to know how to access this to put it there.
You need to place this file in the root of the folder. Make sure the file is called .htaccess and does not have an extension at the end ie .htaccess.txt.
If the directory in which your website is held for example is
/var/www/mysite
then this is the directory where the file needs to be placed. Depending on how your web server is set up, apache may not allow the use of htacess files. To overcome this do the following.
cd /etc/apache2/sites-available
and open “default” up in your editor or choice, eg
sudo nano default
Default for AllowOverride is none, it should be All, so your overall “default” file should look like this:
NameVirtualHost *
ServerAdmin admin#site.com
DocumentRoot /var/www/
Options FollowSymLinks
AllowOverride None
Options Indexes FollowSymLinks MultiViews
AllowOverride All
Order allow,deny
allow from all
If you do not have ssh access, you may need to speak to your webhost.
Additional Comments
If the htaccess file is confirmed as working then you need to add the following code to the htaccess file:
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC]
RewriteRule ^(.*)$ http://%1/$1 [R=301,L]
This would forward all traffic to http://site.com
Or for the other way around
RewriteEngine On
RewriteCond %{HTTP_HOST} ^example.com
RewriteRule (.*) http://www.example.com/$1 [R=301,L]
Both of these will obviously need you to replace example.com with your site address. This is one way to do this, or you could add a ServerAlias in your apache vhost file for that particular site.
This has nothing to do with htaccess. You need to make sure that when someone types www.example.com, it goes to the exact same place as example.com. This is a DNS issue. Find your domain registrar and set that up.
Then on your server, allow for both in your vhost config. You should see something like this:
ServerName example.com
ServerAlias www.example.com

Symfony .htaccess rewrite and redirect not working

I know very little about symfony framework. I copied a website www.example.net to www.example.com site works with url www.example.com/www/app.php or even with www.example.com/www/ but I want it to redirect automatically to app.php without showing it in the url. My htaccess in the www (web) directory is as follows:
&ltIfModule mod_rewrite.c&gt
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ app.php [QSA,L]
&ltIfModule&gt
It doesn't redirect.
Thanks
In your Vhost:
Change this line (Directory section):
AllowOverride None
to:
AllowOverride All
Instead of that rule can you try:
RewriteBase /www/
RewriteRule ^((?!app\.php).*)$ app.php/$1 [NC,L]
Sorry I can't test with Symphony as I am not near my home computer.
A bit late to this game but I just found an Apache directive that saves so much time and makes your htaccess cleaner. The only caveat is you must have Apache 2.2.16+. To have all urls except valid files (images, etc) use app.php as a front controller use the FallbackResource directive.
<Directory "/web/symfony-project">
FallbackResource /app.php
</Directory>

Codeigniter doesn't work without index.php

I'm using MAMP with alias named works.
Codeigniter is at localhost/works/project/web
controllers doesn't work without index.php before them (localhost/works/project/web/index.php/auth/register)
$config['base_url'] = 'localhost/works/project/web/'; //with http://
$config['index_page'] = '';
$config['uri_protocol'] = 'AUTO';
(I tried all of them for uri_protocol)
I created and edit .htaccess file at /User/me/works/project/web/
I tired all the .htaccess files about this issue.
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /Users/me/works/project/web
#Removes access to the system folder by users.
#Additionally this will allow you to create a System.php controller,
#previously this would not have been possible.
#‘system’ can be replaced if you have renamed your system folder.
RewriteCond %{REQUEST_URI} ^system.*
RewriteRule ^(.*)$ /index.php/$1 [L]
#Checks to see if the user is attempting to access a valid file,
#such as an image or css document, if this isn’t true it sends the
#request to index.php
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
#This last condition enables access to the images and css folders, and the robots.txt file
#Submitted by Michael Radlmaier (mradlmaier)
RewriteCond $1 !^(index\.php|images|robots\.txt|css)
RewriteRule ^(.*)$ index.php/$1 [L]
</IfModule>
<IfModule !mod_rewrite.c>
# If we don’t have mod_rewrite installed, all 404’s
# can be sent to index.php, and everything works as normal.
# Submitted by: ElliotHaughin
ErrorDocument 404 /index.php
</IfModule>
creates error, which is:
[error] [client ::1] File does not exist: /Users/me/works/project/web/auth
it is same when I use RewriteBase /
mod_rewrite is active in phpInfo.
I couldn't find the solution.
Your RewriteBase is wrong. It should not be the relative file path for your server, it should be the relative URL path for your rewrite rules and such. More info here.
If you've ever used HTML's <base> tag, it's pretty much the same principle. If your main localhost URL is http://localhost, and your project is in the subfolder http://localhost/my-project, then you would use RewriteBase /my-project/.
If you didn't use RewriteBase, and your project is in a subfolder, you'd have to add the subfolder to every one of your URLs and rewrites, like this:
RewriteRule ^(.*)$ /my-project/index.php/$1 [L]
Food for thought:
Start your .htaccess files as simple as possible. The more you throw at it right away, the more that can go wrong, and the harder it is to debug. Especially if you're a noob to .htaccess files. Don't blindly copy and paste -- figure out what things actually do.
The system folder access part is redundant now -- the system folder has its own .htaccess to restrict requests, just like the application folder.
1.Make below changes in application/config.php file
$config['base_url'] = 'http://'.$_SERVER['SERVER_NAME'].'/Your Ci folder_name';
$config['index_page'] = '';
$config['uri_protocol'] = 'AUTO';
2.use this in .htaccess file:
RewriteEngine on
RewriteCond $1 !^(index\.php|resources|robots\.txt)
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L,QSA]
and enable rewrite mode using below command
a2enmod rewrite
and Edit the file /etc/apache2/sites-enabled/000-default
change the AllowOverride None to AllowOverride All.
and finally Restart your server
You certainly don't have mod_rewrite on or you did not install it with MAMP
Open your httpd config file and change
# AllowOverride controls what directives may be placed in .htaccess files.
# It can be "All", "None", or any combination of the keywords:
# Options FileInfo AuthConfig Limit
#
AllowOverride None
To
AllowOverride All
uncomment this line:
#LoadModule rewrite_module libexec/apache2/mod_rewrite.so
Also make some changes to users here: /etc/apache2/users/username.conf
LoadModule php5_module libexec/apache2/libphp5.so
DocumentRoot "/Users/username/where/ever/you/like"
<Directory "/Users/username/where/ever/you/like">
Options Indexes MultiViews +FollowSymLinks
AllowOverride All
Order allow,deny
Allow from all
</Directory>
Get more info here http://adam.merrifield.ca/2010/08/09/apache-mod_rewrite-php-and-os-x-10-6-snow-leopard/
Restart MAMP
To find out if mod_rewrite is on, follow this answer in Stackoverflow:
How to check if mod_rewrite is enabled in php?
Just to be sure your .htaccess is not the issue, could you try this one. It's been tested:
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteBase /Users/me/works/project/web/
# If the start of the URL doesn't match one of these...
RewriteCond $1 !^(index\.php|robots\.txt|assets|cache|themes|uploads|css|images|js)
# Route through index.php
RewriteRule ^(.*)$ index.php/$1 [L]
</IfModule>
Access to certain folders are granted above and the rewritebase has a trailing slash.
Make sure you have any other nested .htaccess overriding this very one.
I fixed the problem by instead of using .htacces, I write them to httpd.conf.
I checked the AllowOverride is All but I'm not sure why .htaccess isn't working. Because of I lost too much time with this issue, I'm going to use this way.
Thanks for the answers.
Use AllowOverride All on your vhost:
<Directory "/path/to/your/site">
....
AllowOverride All
</Directory>
Open your httpd config file
LoadModule rewrite_module modules/mod_rewrite.so then AllowOverride All
<Directory "C:/xampp/htdocs"> //example for xampp
Options Indexes FollowSymLinks Includes ExecCGI
AllowOverride All
Order allow,deny
Allow from all
</Directory>
I have installed codeigniter in a url like the format: http://11.12.34.45/~project/
I tried different suggestions and finally this following url provided the solution:
https://ellislab.com/expressionengine/user-guide/urls/remove_index.php.html
You need to change two files of Apache,
httpd.conf
httpd-vhosts.conf
change this in httpd.conf
<Files ".ht*">
Require all denied
</Files>
to,
<Files ".ht*">
Require all granted
</Files>
and add below code in httpd-vhosts.conf,
<VirtualHost *:8000>
ServerName localhost
DocumentRoot c:/wamp64/www
<Directory "c:/wamp64/www/">
Options +Indexes +Includes +FollowSymLinks +MultiViews
AllowOverride All
Allow from all
</Directory>
</VirtualHost>

Can't remove index.php on CodeIgniter2.1.2 with RHEL

I have followed from many links to solve this problem. However, It doesn't work with my project.
Move .htaccess file to CI root directory
Config about mod_rewrite in .htaccess file (all structures that I found on the Google)
change config[‘index_page’] to an empty
Set config['uri_protocol'] = 'REQUEST_URI' or 'QUERY_STRING'
then, it shown
Not Found The requested URL /class/method was not found on this
server.
I don't know how to fixed it further. I spent so long time for this problem.
Install a fresh copy of CodeIgniter
Change $config['index_page'] to = '';
Copy this to your .htaccess
.htaccess
RewriteEngine on
RewriteCond $1 !^(index\.php|robots\.txt)
RewriteRule ^(.*)$ /index.php?/$1 [L]
Visit http://yourhost/codeigniter/welcome/index.
Does it work? If not, change the last line of the .htaccess above to
RewriteRule ^(.*)$ /index.php/$1 [L]
If it does not work at this point, check if your Apache supports mod_rewrite and it is enabled.
Ensure that mod_rewrite is enabled: If you're running Ubuntu open a terminal and type sudo a2enmod rewrite.
Furthermore, ensure that your .htaccess file is even being parsed. Again, on Ubuntu locate: /etc/apache2/sites-available/default and modify the <Directory> directive to:
<Directory /var/www/>
Options Indexes FollowSymLinks MultiViews
AllowOverride All
Order allow,deny
allow from all
</Directory>
Google search to find related Windows/Mac commands and file locations.

Resources