How to point directory to load index with .htaccess - php

I am trying to configure .htaccess for my web site.
there are two folders in my web site 1 - app , 2 - public.
i want to load index.php from public folder and restrict all direct access to app folder
the .htaccess i am using returns result 403 Forbidden, but if i move index.php in root directory it is working, i just want to point that index.php is in public directory and send all requests there.
Options -Indexes
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /public/
# Force to exclude the trailing slash
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} (.*)/$
RewriteRule ^(.+)/$ $1 [R=307,L]
# Restrict php files direct access
RewriteCond %{THE_REQUEST} ^.+?\ [^?]+\.php[?\ ]
RewriteRule \.php$ - [F]
# Allow any files or directories that exist to be displayed directly
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?$1 [QSA,L]
</IfModule>

Well i solved this problem like this,
for root directory i managed .htaccess like the code given down, the idea is to redirect all calls to public dir.
Options -Indexes
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteRule ^$ public/ [L]
RewriteRule (.*) public/$1 [L]
</IfModule>
And for filtering requests i used to put second .htaccess to public dir
Options -Indexes
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
# Force to exclude the trailing slash
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} (.*)/$
RewriteRule ^(.+)/$ $1 [R=307,L]
# Restrict php files direct access
RewriteCond %{THE_REQUEST} ^.+?\ [^?]+\.php[?\ ]
RewriteRule \.php$ - [F]
# Allow any files or directories that exist to be displayed directly
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?$1 [QSA,L]
</IfModule>

Related

Can't redirect to the same url using htaccess when www is used

I want to keep my laravel website without www in the beginning of the url.
I am using two .htaccess files. One at root directory and one at public_html folder. Here is my directory tree structure:
-mysite
-app
-bootstrap
-config
...
-public_html
.htaccess
index.php
-css
-js
-images
...
-resources
-routes
...
.htaccess
This is what I want to do.
Input:
www.mysite.com
mysite.com
Output that I need:
mysite.com
Now, the problem:
www.mysite.com gives me: mysite.com/public_html/index.php
(The www is being removed successfully)
These are my htaccess files:
Root directory:
Options +SymLinksIfOwnerMatch
RewriteEngine On
# Allow Installatron requests
RewriteCond %{REQUEST_FILENAME} deleteme\.\w+\.php
RewriteRule (.*) - [L]
RewriteRule ^ public_html/index.php [L]
public_html directory:
<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}]
# Remove www
RewriteCond %{HTTP_HOST} ^www\.(.*)$ [NC]
RewriteRule ^(.*)$ http://%1%{REQUEST_URI} [R=301,QSA,NC,L]
# 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>
I'm using hostinger by the way.
Any ideas? Thank you!!

.htaccess help within root and public

I have a small mvc application where I'm trying to force the url to go from www.example.com to www.example.com/public.
In order to do this, I have a .htaccess file in the root (/) and then another .htaccess within the /public folder to point to index.php with friendly urls. I believe the issue is within the htaccess file that sits in the public folder, I need all requests to go through index.php
I have tried a few threads on stackoverflow but seems to give me a server 500 error when I access www.example.com
.htaccess of root
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /public/
</IfModule>
.htaccess of public folder
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REQUEST_FILENAME}% !-f
RewriteCond %{REQUEST_FILENAME}% !-d
#allow images, css and js links
RewriteCond %{REQUEST_URI} !\.(css|js|jpe?g|gif|png)$ [NC]
RewriteRule ^(.*)$ index.php?url=$1 [PT,L]
</IfModule>
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !^/index.php
RewriteRule (.*) /public/index.php

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>

How to install laravel app in subfolder of shared host?

I install laravel app in root of my shared host "public_html now I want to install Russian version of this app in ru/ subfolder but when I go to example.com/ru I got 404 Page not found error. I use apache web server my .htaccess file in root folder contain these code
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteRule ^(.*)$ public/$1 [L]
</IfModule>
how should i change this configuration?
Thanks.
You will do it in the same way you installed it in the root directory. I will tell you how I installed it on my shared hosting account, both main and sub domains. After I uploaded all my project to the subfolder, and your will be example.com/ru, do the following:
In your public folder there is your .htaccess file. since you are basically making this a subdomain, it will need to be in the root of that subdomain, so transfer the .htaccess from public folder to the root of ru folder.
Open the .htaccess and change/add the following:
DirectoryIndex public/index.php
and in the RewriteRule change it to this:
public/index.php
And just to be clear, your .htaccess should be like this at the end after your changes:
DirectoryIndex public/index.php
<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 ^ public/index.php [L]
# Handle Authorization Header
RewriteCond %{HTTP:Authorization} .
RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]
</IfModule>
Create a .htaccess file in a root folder and paste the code and save it.
<IfModule mod_rewrite.c>
<IfModule mod_negotiation.c>
Options -MultiViews
</IfModule>
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} -d [OR]
RewriteCond %{REQUEST_FILENAME} -f
RewriteRule ^ ^$1 [N]
RewriteCond %{REQUEST_URI} (\.\w+$) [NC]
RewriteRule ^(.*)$ public/$1
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ server.php

Why can't I redirect index.php to domain?

New .htaccess file (still not working, it sends me to localhost/xampp
RewriteEngine on
RewriteBase /sample/
# set root to index.php
DirectoryIndex index.php
# prevent directory listing
Options -Indexes
# remove index.php
RewriteCond %{THE_REQUEST} /index\.php [NC]
RewriteRule ^(.*)index.php$ $1 [R=301,L,NC]
# removing extensions
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^([^.]+)/?$ $1.php [L]
Old .htaccess file below:
RewriteEngine on
# set root to index.php
DirectoryIndex index.php
# prevent directory listing
Options -Indexes
# removing extensions
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^\.]+)$ $1.php [NC,L]
# remove index.php
Options +FollowSymLinks
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /index\.php\ HTTP/
RewriteRule ^index\.php$ http://localhost/sample/ [R=301,L]
Domain is http://localhost/sample/. I have tried many things that Google has given me but none of them work. Every time I go to localhost/sample/index.php it doesn't redirect to localhost/sample/. I also used the Redirect 301 /index.php /.
I tried
RewriteCond %{THE_REQUEST} ^.*/index\.php
RewriteRule ^(.*)index.php$ /$1 [R=301,L]
But it sends me to http://localhost/xampp/splash.php.
I am still new to .htaccess so I really don't have any idea what I am doing wrong. I've tried the answers that have been given to the questions where the OP said the answer worked.
You can use:
# set root to index.php
DirectoryIndex index.php
# prevent directory listing
Options -Indexes
RewriteEngine on
RewriteBase /sample/
# remove index.php
RewriteCond %{THE_REQUEST} /index\.php [NC]
RewriteRule ^(.*)index.php$ $1 [R=301,L,NC]
# removing extensions
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME}.php -f
RewriteRule ^([^.]+)/?$ $1.php [L]
RewriteBase /sample/ is needed to make sure redirect happens from raltive path of current directory.
Use this:
RewriteEngine On
# prevent directory listing
Options -Indexes
# allows leaving off the extension (.php) in urls
Options +MultiViews
# removing extensions
RewriteCond %{REQUEST_FILENAME} !-f [NC]
RewriteCond %{REQUEST_URI} !-d
RewriteRule ^index(\.php)?$ http://localhost/sample [R=301,L]

Categories