.htaccess - This webpage has a redirect loop - .htaccess

I'm cleaning my URLs and everything looks fine but whenever I try to access any directory such as images etc then chrome shows that "This webpage has a redirect loop." However I want to protect directories inside public_html.
The .htaccess file is inside public_html
<ifModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?/$1 [L]
RewriteRule ^(.+)/$ http://%{HTTP_HOST}/$1 [R=301,L]
</ifModule>
I want to protect images, css and javascript directories but want to allow access to the admin directory.
Thank in advance.

Change order of your rules and change your trailing slash removing rule:
<ifModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.+?)/$ $1 [R=301,L]
# block all directories except admin/
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule !^admin(/|$) - [NC,F]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?/$1 [L,QSA]
</ifModule>

You can use this remove all code and use this one
option index allow and disallow to be index directory like
This does the trick.
Options All -Indexes
or
IndexIgnore *
for more information please check below link
http://viralpatel.net/blogs/htaccess-directory-listing-enable-disable-allow-deny-prevent-htaccess-directory-listing/

Related

htaccess delete and ban all extensions and add / to the end of the URL

I am making a small cms in php. Through htaccess I would like to delete and block all extensions (.php .html. Htm etc.) and add a / at the end of the url.
ex. www.miosito/ article-name/
Options -Indexes
DirectoryIndex home.php
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^.*$ core.php
this is my current htaccess.
Sorry for my bad English and thank you very much in advance
With your shown samples/attempts, could you please try following htaccess Rules file. Please make sure to clear browser cache before testing your URLs.
RewriteEngine ON
##Excluding Indexes and MultiViews options here.
Options -Indexes -MultiViews
##Rule for url http://localhost:80/test here.
RewriteRule ^(test)/?$ $1/category [R=301,L]
RewriteRule ^(test/category)/?$ $1.php [NC,L]
##Rule for forbidding html OR htm OR php extension files.
RewriteRule \.(html|php|Htm)/?$ - [F,NC,L]
##Rule for adding / at end of urls.
RewriteCond %{REQUEST_URI} /+[^\.]+$
RewriteRule ^(.+[^/])$ %{REQUEST_URI}/ [R=301,L]
##Rules for non-existing pages to be served here.
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^ core.php [QSA,L]
Try out this code in site root .htaccess after completely clearing your browser cache:
Options -Indexes
DirectoryIndex home.php
RewriteEngine On
# block direct access to .php, .html, .html extensions files
RewriteCond %{THE_REQUEST} \.(html?|php)[?\s/] [NC]
RewriteRule . - [F]
## Adding a trailing slash
RewriteCond %{ENV:REDIRECT_STATUS ^$
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule [^/]$ %{REQUEST_URI}/ [L,NE,R=301]
# core handler
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . core.php [L]

Page URL named css or js doesn't work

In my site when I go to URL such as my-site.com/css or my-site.com/js, then browser displays the folder listing instead of the actual page saved in the database. If I use Options All -Indexes in my htaccess then it says forbidden. I have following rule in my htaccess to load any dynamic page:
RewriteRule ^(.*)$ page.php?url=$1 [QSA,L]
Since I have also folders named css and js in my project, so I think server displays the directory content instead of dynamic page. How can I prevent this conflict. I know if I use a suffix "page" before my page name then it can be sorted out but i don't want to use any parameter before the page url.
EDIT
Here's my complete htaccess file:
# my-site.com
Options +FollowSymlinks -MultiViews
#RewriteBase /
#Enable mod rewrite
RewriteEngine On
DirectorySlash off
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)$ page.php?category=$1 [QSA,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)/([^/]+)$ page.php?category=$1&post=$2 [QSA,L]
Since /js and /css are real directories your rewrite rules don't execute because you have this condition:
RewriteCond %{REQUEST_FILENAME} !-d
which means don't run for directories.
You can tweak your .htaccess to this:
Options +FollowSymlinks -MultiViews
RewriteEngine On
# add a trailing slash to directories
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule !/$ %{REQUEST_URI}/ [L,R=302]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^/]+)/?$ page.php?category=$1 [QSA,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^/]+)/([^/]+)/?$ page.php?category=$1&post=$2 [QSA,L]

How can I hide folder name from URL using .htaccess

I have a website with the root folder 'www', but I put all php files including index.php in a sub-folder of root.
I wrote myself a .htaccess file to redirect, so if I input www.test.com, it will jump to www.test.com/folder and display the index.php.
Below it's my .htaccess which I put in the root.
RewriteEngine On
RewriteBase /
RewriteRule ^folder2 - [L]
#ignore folder2 in which I put important files, but no works for the website
RewriteCond %{REQUEST_URI} !^/folder(.*)
RewriteRule (.*) /folder/$1
Right now, I want to change the .htaccess file to reach these goals:
When I input www.test.com, jump to www.test.com/folder as usual, but display the url without folder.
All the pages in folder will display the url without folder name.
Such as
www.test.com/shop/page1 -> www.test.com/page1
I searched some of the scripts, but none of them works.
I've found this related question which answers to your problem. The snippet from the answer is (note: it's adjusted to your needs):
RewriteEngine On
RewriteBase /
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s/+folder/([^\s]+) [NC]
RewriteRule ^ %1 [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule (?!^folder/)^(.*)$ /folder/$1 [L,NC]
Reference: https://stackoverflow.com/a/18361995/3673491
You can use below code.
RewriteEngine On
RewriteBase /
RewriteRule ^/?$ /folder/ [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /folder/$1 [L]
Based on your current rules. You can just make a slight adjustment.
RewriteEngine On
RewriteBase /
#ignore folder2 in which I put important files, but no works for the website
RewriteRule ^folder2/? - [L]
RewriteRule ^/?$ /folder/ [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /folder/$1 [L]

How to ignore subfolders and redirect to the root directory with .htaccess

I'm new in the world of .htaccess and I have a problem. I searched in google how to redirect always to a file, like wordpress do it, and I found this:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /demo/
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /demo/index.php [L]
</IfModule>
I tested this and it works but I want to ignore the subfolders and redirect like it does when you request a folder which doesn't exist. Does anyone how to ignore all the subfolders, and redirect without changing the URL?
Remove the second RewriteCond which checks if directory doesn't exist !-d
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /demo/
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule . /demo/index.php [L]
</IfModule>

Redirection Subfolder Htaccess

I want a redirect using htaccess I have a post name wordpressurl/curious and also a folder named curious
What I want is if a user tries to access
wordpressurl/curious
wordpressurl/curious/
wordpressurl/curious/subfolder
note trailing slash in 1 and 2 and 3 is subdirectory. All of them should give 404 error wordpress page using htaccess only
Edited
# BEGIN WordPress
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /Wordpress_Work/realestate/
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /Wordpress_Work/realestate/index.php [L]
</IfModule>
# END WordPress
You can access the wordpress post that is the same as a curious directory by making the changes indicated below. Change curious to whatever your actual directory/post is.
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /Wordpress_Work/realestate/
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
#add an OR
RewriteCond %{REQUEST_FILENAME} !-d [OR]
#OR if the URI is curious or curious/ or curious/subfolder
RewriteCond %{REQUEST_URI} ^/Wordpress_Work/realestate/curious(/.*)?$
RewriteRule . /Wordpress_Work/realestate/index.php [L]
</IfModule>
If you are putting RewriteCond %{REQUEST_FILENAME} !-d you can't have same name for the page and physical directory . that's the point of using it.
Try skipping the rewritecond , though I've never tried it before . for me , I'd change the page name or add a suffix.

Resources