.htaccess causes issues in sub-folders - php

Here is my .htaccess to rewrite public/index.php
I'm using the Laravel php framework.
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteCond %{THE_REQUEST} /(code|tmp) [NC]
RewriteRule ^ - [L]
RewriteRule ^((?!public/).*)$ public/$1 [L,NC]
</IfModule>
Here the folders 'code' and 'tmp' can be accessed like normal.
If I add a set of nested folders, and add the default Laravel htaccess to remove the public/index.php
<IfModule mod_rewrite.c>
<IfModule mod_negotiation.c>
Options -MultiViews
</IfModule>
RewriteEngine On
# Redirect Trailing Slashes...
RewriteRule ^(.*)/$ /$1 [L,R=301]
# Handle Front Controller...
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [L]
</IfModule>
It doesn't work inside the nested folder.
It works good if I don't have the firstly given .htaccess in the root directory. Once I given my .htaccess it is not working.
Is the .htaccess in the root directory causing effect to the sub folder ?
If so , how can I fix that
Note : This is the question where I got .htaccess previously
.htaccess exception makes issue on main directory

Related

request exceeded the limit of 10 internal redirects laravel 5.5

I have a problem when uploading a new laravel 5.5 project. When I alter the htaccess in the public_html folder on my server it keeps redirecting. However this problem wasnt there on laravel 5.0. Here's my htaccess from the public_html folder:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteRule ^(.*)$ public/$1 [L]
</IfModule>
And here's the htaccess in the public folder:
<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>
Any suggestions on how to solve this problem?
Beside the fact that it is really not good idea to put a Laravel project in your Document Root, you have a problem with your htaccess file in your Root directory.
Additional I would at least protect all files in your root folder from direct access:
<IfModule mod_rewrite.c>
RewriteEngine On
# Fake 404 for all files in /
RewriteCond %{REQUEST_FILENAME} -f
RewriteRule ^/?[^/]+$ - [R=404,L]
# Protect rewrite loop: exclude /public
RewriteCond %{REQUEST_URI} !^/?public(/.*)?$
RewriteRule ^(.*)$ /public/$1 [L]
</IfModule>

Show requests from root folder to subfolder

I'm using Laravel for my website, but i'm encountering a problem.
My website does not have any public_html or htdocs folder, but the root itself it is already public. Because of this i'm obliged to put all the Laravel files to the root folder.
To who doesn't know, when using Laravel the public folder is generally the subfolder public, therefore right now if I want to access my Laravel web applicaton I have to navigate to http://www.mywebsite.com/public.
I need to edit the .htaccess in order to show from http://www.mywebsite.com/ the subfolder public files and deny any access to the root files.
So http://www.mywebsite.com/public/index.php should be http://www.mywebsite.com/index.php, http://www.mywebsite.com/public/images/logo.png should be http://www.mywebsite.com/images/logo.png and so on.
Now I'm wondering, is it even possible?
Laravel .htaccess original file is:
<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]
</IfModule>
I've been successful to solve this myself by editing the original .htaccess to:
<IfModule mod_rewrite.c>
<IfModule mod_negotiation.c>
Options -MultiViews
</IfModule>
RewriteEngine On
# Redirect Trailing Slashes If Not A Folder...
RewriteCond public/%{REQUEST_FILENAME} !-d
RewriteRule ^(.*)/$ public/$1 [L,R=301]
# Handle Front Controller...
RewriteCond public/%{REQUEST_FILENAME} !-d
RewriteCond public/%{REQUEST_FILENAME} !-f
RewriteRule ^ public/index.php [L]
</IfModule>

http://127.0.0.1/tp/index.php/movies change this url to http://127.0.0.1/tp/movies using .htaccess

I am using laravel framework for mtdb i wanna change http://127.0.0.1/tp/index.php/movies this url to
http://127.0.0.1/tp/movies means i want to add to remove index.php using .htaccess
Here is the .htaccess
I have
<IfModule mod_rewrite.c>
<IfModule mod_negotiation.c>
Options -MultiViews
</IfModule>
RewriteEngine On
# Redirect Trailing Slashes...
RewriteRule ^(.*)/$ /$1 [L,R=301]
# Handle Front Controller...
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [L]
There are many solutions for removing the index.php such as Laravel 4 remove Index.php from URL
But i would suggest you to rename the server.php in the your Laravel root folder to index.php and copy the .htaccess file from /public directory to your Laravel root folder.

.htacess for removing public/index.php works good in root and not in sub directories

Here is my .htaccess which i have default in laravel it helps me to prevent typing public/index.php
<IfModule mod_rewrite.c>
<IfModule mod_negotiation.c>
Options -MultiViews
</IfModule>
RewriteEngine On
RewriteRule ^(.*)/$ /$1 [L,R=301]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [L]
</IfModule>
So, if i need to access
localhost/public/index.php/home
localhost/public/index.php/login
It is enough to me to type
localhost/home
localhost/login
I want to make exception to for few directories such as 'testproject' and 'betaproject' i.e., where i will have a regular php or html directory or i will have another fresh project and it will have same .htaccess also.
How can i modify my .htaccess file to make it possible.
So that if i want to access
localhost/testproject/public/index.php/home should be accessible by localhost/testproject/home
In your root .htaccess have this rule to skip 2 directories:
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(testproject|betaproject)/(\w+)/?$ $1/public/index.php/$2 [L,NC]
Make sure this rule is very first just below RewriteEngine On line.

Laravel: Setup .htaccess of Folder outside of Laravel App

I have Laravel installed in /var/www/html
I have a PHP script installed in /var/www/html/i/
The htaccess of /var/www/html/ is
<IfModule mod_rewrite.c>
<IfModule mod_negotiation.c>
Options -MultiViews
</IfModule>
RewriteEngine On
# Redirect Trailing Slashes...
RewriteRule ^(.*)/$ /$1 [L,R=301]
# Handle Front Controller...
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [L]
The htaccess of /var/www/html/i/ is
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteRule ^(.+)$ /?variable=$1
</IfModule>
Trouble is whenever I go to
/i/somevariable
I get a 404 error from my laravel app.
Make a .htaccess file in your root folder with this content:
RewriteEngine On # Turn on the rewriting engine
RewriteRule ^([A-Za-z0-9-]+)/i/?$ index.php/i/?s=$1 [NC,L]
This should work fine.
if you are using apache
read mod_rewrite
http://httpd.apache.org/docs/current/mod/mod_rewrite.html
dont just turn on,
use rewrite rule

Categories