.htaccess without matching ifmodule? - apache

I am trying to deploy a laravel api with apache, but I am constant getting "internal server error".In errorlog, error is defined with "C:/Apache24/htdocs/ErenA/public/.htaccess: without matching section". I tried changing .htaccess file, delete the ifmodule element in httpd.conf and keeping only "directory index index.html index.php" but nothing seems to working
so here is my .htaccess code:
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteCond %{REQUEST_URI} !^public
RewriteRule ^(.*)$ public/$1 [L]
RewriteRule ^ index.php [L]
</IfModule>
RewriteEngine On
# Handle Authorization Header
RewriteCond %{HTTP:Authorization} .
RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]
# Redirect Trailing Slashes If Not A Folder...
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} (.+)/$
RewriteRule ^ %1 [L,R=301]
# Send Requests To Front Controller...
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [L]
</IfModule>
And Also I will add httpd.conf ifModule:
<IfModule dir_module>
DirectoryIndex index.php index.html
</IfModule>
All AllowOverride is All and require all is granted in config file.
Can someone please help me?

You have 2 IfModule closing (</IfModule>), but only 1 opens <IfModule dir_module>
So your content should be like this:
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteCond %{REQUEST_URI} !^public
RewriteRule ^(.*)$ public/$1 [L]
RewriteRule ^ index.php [L]
# Handle Authorization Header
RewriteCond %{HTTP:Authorization} .
RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]
# Redirect Trailing Slashes If Not A Folder...
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} (.+)/$
RewriteRule ^ %1 [L,R=301]
# Send Requests To Front Controller...
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [L]
</IfModule>
See that I have removed the middle </IfModule> and because you continue using Rewrite module (RewriteCond and RewriteRule), I just put those inside...

Related

Htaccess rewrite with %REQUEST_FILENAME condition

I stucked. I need to:
all request with domain.com/api/... rewrite to public/ directory
all others request rewrite to frontend/index.html
but if request is a file, then rewrite just to 'frontent/'
I had two htaccess files:
.htaccess
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REQUEST_URI} ^/api/ [NC]
RewriteRule ^(.*)$ public/$1 [L]
RewriteRule ^(.*)$ frontend/$1 [L]
</IfModule>
frontend/.htaccess
<IfModule mod_rewrite.c>
RewriteEngine On
# Send Requests To Front...
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.html [L]
</IfModule>
It was working but now I need to remove the one from frontend directory without loosing it's rules. I changed the main .htaccess:
<IfModule mod_rewrite.c>
RewriteEngine On
# Send request to backend
RewriteCond %{REQUEST_URI} ^/api/ [NC]
RewriteRule ^(.*)$ public/$1 [L]
# Send request to frontend
RewriteCond frontend/%{REQUEST_FILENAME} !-d
RewriteCond frontend/%{REQUEST_FILENAME} !-f
RewriteRule ^ frontend/index.html [L]
# Request for file
RewriteRule ^(.*)$ frontend/$1
</IfModule>
But it doesn't work. I think that this conditions are wrong:
RewriteCond frontend/%{REQUEST_FILENAME} !-d
RewriteCond frontend/%{REQUEST_FILENAME} !-f
because all request (others than api) are rewrited to frontend/index.html which i mentioned.
Could anybody show me where did I mistake?

htaccess trailing slashes just for one URL

Having the following .htaccess rules
<IfModule mod_rewrite.c>
<IfModule mod_negotiation.c>
Options -MultiViews -Indexes
</IfModule>
RewriteEngine On
RewriteRule ^dashboard/(.*)$ back/index.html [L]
# Handle Authorization Header
RewriteCond %{HTTP:Authorization} .
RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]
# Redirect Trailing Slashes If Not A Folder...
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} (.+)/$
RewriteRule ^ %1 [L,R=301]
# Handle Front Controller...
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [L]
</IfModule>
I want to redirect all incomings requests if they match /dashboard to end with a trailing slash. But only for this case.
if I use
RewriteRule ^dashboard/(.*)$ back/index.html [L]
RewriteCond %{REQUEST_URI}dashboard /+[^\.]+$
RewriteRule ^(.+[^/])$ %{REQUEST_URI}/ [R=301,L]
will fail because all my URL's are becoming a trailing slash.
How can I have a trailing slash only for this case?
Have it this way:
<IfModule mod_rewrite.c>
<IfModule mod_negotiation.c>
Options -MultiViews -Indexes
</IfModule>
RewriteEngine On
RewriteRule ^dashboard/(.*)$ back/index.html [L]
# Handle Authorization Header
RewriteCond %{HTTP:Authorization} .
RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]
# add / after /dashboard
RewriteRule ^dashboard$ $0/ [L,R=301,NC]
# remove / from all other URIs
# Redirect Trailing Slashes If Not A Folder...
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} (.+)/$
RewriteRule !^dashboard/ %1 [L,R=301,NC]
# Handle Front Controller...
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [L]
</IfModule>
Make sure to test it after completely clearing browser cache.

How to redirect users using .htaccess?

I am using Apache on my VM. I have this .htaccess right now.
<IfModule mod_rewrite.c>
<IfModule mod_negotiation.c>
Options -MultiViews -Indexes
</IfModule>
RewriteEngine On
# Handle Authorization Header
RewriteCond %{HTTP:Authorization} .
RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]
# Redirect Trailing Slashes If Not A Folder...
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} (.+)/$
RewriteRule ^ %1 [L,R=301]
# Handle Front Controller...
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [L]
</IfModule>
Result
If I do
http://dips.awsri.com/
it works fine.
If I do
http://www.dips.awsri.com/
it redirects me to my log in screen, which I don't want 😓
http://www.dips.awsri.com/login
How can I stop that ? and instead redirect my user to
http://dips.awsri.com/

http url added to the https url when viewed

I have a strange problem. When I open my domain in SSL mode , the http url get appended to it.
This is my http url
This is my SSL url
** other url for https are opening normally , although CSS and images are not loaded. eg Login page
I am clueless whats going on. Please help
Edit : This is my .htaccess
<IfModule mod_rewrite.c>
<IfModule mod_negotiation.c>
Options -MultiViews
</IfModule>
RewriteEngine On
RewriteCond %{HTTPS} on
RewriteRule (.*) http://%{HTTP_HOST}%{REQUEST_URI}
# 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]
# Handle Authorization Header
RewriteCond %{HTTP:Authorization} .
RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]
</IfModule>
Try this
# Force https
RewriteCond %{HTTPS} !=on
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
Edit:
Below is my live server setting working with laravel 5.2.
<IfModule mod_rewrite.c>
<IfModule mod_negotiation.c>
Options -MultiViews
</IfModule>
RewriteEngine On
# Force https
RewriteCond %{HTTPS} !=on
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
# Force Non-www
RewriteBase /
RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC]
RewriteRule ^(.*)$ https://%1/$1 [R=301,L]
# 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]
# Handle Authorization Header
RewriteCond %{HTTP:Authorization} .
RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]
</IfModule>

htaccess RewriteRule rewrite to subfolder

I am trying to rewrite the url 'test' to a subdirectory in /public. This is a Laravel App end I want to run the content in public/test independend of Laravel so I should be prevented to go through the Laravel router.
/test/whatever should be invisbly forwarded to /public/test/whatever (The url should stay /test/whatever).
I now have this:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REQUEST_URI} ^/test/ [NC]
RewriteRule ^(.*)$ public/test/$1 [L]
RewriteRule ^(.*)$ public/$1 [L]
</IfModule>
So far this is forwarding to: /public/test/test/(index.php)
I think I am close...
Update:
In the public directory is also a .htaccess file (Original, from Laravel 5.4):
<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]
# Handle Authorization Header
RewriteCond %{HTTP:Authorization} .
RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]
</IfModule>
You only need the last rule
RewriteRule ^(.*)$ public/$1 [L]
for the root dir .htaccess file. This will rewrite everything to the public/.htaccess, including requests starting with /test.
As long as public/test/ is a directory, and any requested URLs starting with /test/ are real files inside public/test, this will be sufficient, because the conditions
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
will prevent existing files and directories to be handled by index.php.
If test is not an existing directory, or the requests for /test/... are not real files, you need additional rules. Possibly in /public/.htaccess or in /public/test/.htaccess.
The reason for /test being rewritten to /public/test/test was the combination of
# /.htaccess
RewriteRule ^(.*)$ public/test/$1 [L]
where (.*) already includes test, which becomes public/test/test, and then
# /public/.htaccess
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [L]
because public/test/test is not an existing file or directory (-> RewriteCond), and is therefore handled by this rule.