htaccess removing index.php only root folder - .htaccess

Matured one question which I can not handle himself and is forced to ask for help.
So...
I have configured .htaccess file:
RewriteEngine On
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /(.*)index\.php($|\ |\?)
RewriteRule ^ /%1 [R=301,L]
This code removes all directories index.php on site. I also need to remove it only in the site root. The problem is that the root can be any subdirectory as "/" and "/some_folder/"
Is it possible to do this without explicit basedir?
For example for CNC I managed to make it so:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . ./ [L]
Thus, the problem:
Site root = httр://site.com/sub/
Remove index.php need only in the root directory.
Rot directory may vary and the decision should be without explicit basedir

You can use this rule to remove index.php from root only:
RewriteCond %{THE_REQUEST} \s/index\.php [NC]
RewriteRule ^index\.php$ / [L,R=301,NC]

Related

Use of .htaccess hides phpmyadmin localhost folder

My problem is when I am using this .htaccess file in my root folder nfs, the folder becomes invisible.
RewriteEngine on
RewriteCond %{THE_REQUEST} !/phpmyadmin [NC]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^details1/([0-9]+)/([0-9-]+)$ details1.php?cus_id=$1&ad_id=$2 [NC, L]
Again when I remove the below last line, then nfs folder becomes visible.
RewriteRule ^details1/([0-9]+)/([0-9-]+)$ details1.php?cus_id=$1&ad_id=$2 [NC, L]
I wanted to add url rewrite rule. plz help
Try this:
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} -f
RewriteRule ^details1/([0-9]+)/([0-9]+) details1.php?cus_id=$1&ad_id=$2

htaccess for subdirectory only

i have a some directory on my www folder
-www (base)
--folder1
--folder2
--folder3
so i want to use some htaccess code for all files and folder (so i just create .htaccess in www folder)
but also i want to add this code just for folder 1,2,3 (and files inside them) and no for other folders or other files in base directory www or other directory
RewriteEngine On
RewriteCond %{DOCUMENT_ROOT}%{REQUEST_URI} !-f
RewriteRule \.m3u8 file.m3u8 [NC,L]
#RewriteCond %{HTTP_USER_AGENT} ^.*(vlc|LibVLC|Android|iPhone).*$ [NC]
# RewriteRule ^(.*)$ http://example.com [L,R=301]
You can add a RewriteCond to restrict your first rule for certain folders only:
RewriteEngine On
RewriteCond %{REQUEST_URI} ^/(folder1|folder2|folder3)/ [NC]
RewriteCond %{DOCUMENT_ROOT}%{REQUEST_URI} !-f
RewriteRule \.m3u8 file.m3u8 [NC,L]
# remaining rules go here

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]

Approach to minimize dir-hardcoding in a RewriteRule that works from root and subfolder

I have this project that I locally run from the root of a webserver. It's a schoolproject so it will be deployed to my school's servers which require me to put it inside a subfolder. This is causing troubles with the static+css redirects because it clearly doesn't work the way it is written now when run from a subfolder, and I don't want to hardcode every rewrite-rule to the subfolder because that's is ugly, and makes it hard to switch to using running it from root if I need that.
The project has the following rules in a .htaccess.
Options +FollowSymlinks
RewriteEngine On
RewriteCond %{THE_REQUEST} ^GET\ /static/
RewriteRule ^static/(.*) /$1 [L,R=301]
RewriteCond %{THE_REQUEST} ^GET\ /css/
RewriteRule ^css/(.*)$ static/css/$1 [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L]
Is there an approach that lets me get the above rewrites to work from both root and a subfolder no questions asked, or a way that makes the difference minimal? (where I don't need to hardcode the subfolder in every rule)
the way I see it (assuming the .haccess is also in the subfolder)
Options +FollowSymlinks
RewriteEngine On
#you might need a rewritebase, but not necessarily
#testing
#RewriteBase /
#production
#RewriteBase /subfolder
#less strict condition. will only be a problem if there is another folder also named 'static' inside 'static' :-)
RewriteCond %{THE_REQUEST} /static/
RewriteRule ^static/(.*) $1 [L,R=301]
#condition here is not needed
RewriteRule ^css/(.*)$ static/css/$1 [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L]

htaccess RewriteRule not letting me acces REAL directories

This is my .htaccess file
Options +FollowSymlinks
RewriteEngine On
RewriteBase /
# Force add trainling slash
RewriteCond %{REQUEST_URI} !(\.[a-zA-Z0-9]{1,5}|/)$
RewriteRule (.*)$ http://foo.bar/$1/ [R=301,L]
# Prepent www if not exist in URI
RewriteCond %{HTTP_HOST} ^foo\.bar$
RewriteRule (.*) http://www.foo.bar/$1 [R=301,L]
# If path is not a directory or file then apply RewriteRule
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
# Rewrite for profile view
RewriteRule ^([^/]+)/?$ profile.php?user=$1 [NC,L]
# Rewrite for page view
RewriteRule ^([^/]+)/([^/]+)/([^/]+)/?$ account.php?type=$1&user=$2&page=$3 [QSA,L]
# Condition to ignore regular links
RewriteRule ^library/templates/([^/]+)/([^/]+)/([^/]+)/?$ library/templates/$1/$2/$3 [QSA,L]
# Rewrite for dynamic page settings
RewriteRule ^([^/]+)/([^/]+)/([^/]+)/([^/]+)/([^/]+)/?$ account.php?type=$1&user=$2&page=$3&$4=$5 [QSA,L]
# Rewrite for account view
RewriteRule ^([^/]+)/([^/]+)/?$ account.php?type=$1&user=$2 [QSA,L]
# Rewrite for site default resources location
RewriteRule ^([^/]+)/([^/]+)/theme/s/([^/]+)/([^/]+)/([^/]+)/?$ library/themes/site/$3/$4/$5 [QSA,L]
I know this is kinda not the best way to go about this, feels like there is too much redundancy but I'm no expert but its working for me. If anyone can give me advise on how to make this more efficient I would highly appreciate it.
Ok so my problem is that I have a real directory at
www.foo.bar/path/to/real/dir
and I want to access a file in that directory, but my rewrite rules aren't letting access that directory unless I make a new rewrite rule redirecting my url to itself.
Check for files symlinks and directories as well (as your first rule):
RewriteCond %{REQUEST_FILENAME} -f [NC,OR]
RewriteCond %{REQUEST_FILENAME} -d [NC,OR]
RewriteCond %{REQUEST_FILENAME} -l [NC]
RewriteRule .* - [L]

Resources