Having multiple htaccess in a root directory - php

I have directory in my website where user can upload projects or create sub directories, in the root dir i have a default .htaccess file and default.php which i don't want the user to get access to because i rewrite the dir to load default.php as default index page and also this to file will be copied to any sub dir that the user create.
Now my problem is when user try to save .htaccess for the project he want to save it will show that is already exist. is there anything i can do to have multiple htaccess or write my own as default htaccess.
Second. is it possible to use one htaccess in all the sub dir so user can freely add their own?
Example: bellow is how my directory look
WWW.example.com/ project /userprojectname/sub1/sub2/sub3/etc...
In project it has htaccess and users are not allow to do anything but their projectname will save there.
In userprojectname it has an htaccess and default.php, they can also add more file or create directories and any sub dir will also have it own htaccess and default.php.
Now can i make htaccess in project work in all page? and as for default, i used jquery to disallow users creating file name default.php also is there a better way i can fix all this?
Bellow is my dirs and htacess file
Options +FollowSymlinks
RewriteEngine on
#This will block from accessing root folder
#RewriteCond %{REQUEST_FILENAME} !-f
#RewriteRule ^([^\.]+)$ $1.php [NC,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([a-zA-Z0-9]+)$ $1.php [NC,L]
DirectoryIndex default.php

Related

How can I maintain a website where a folder and file have the same name but also hide the extension of that file?

I am trying to improve the structure of my urls by using .htaccess. I have a file named foo.php and a folder named /foo. I want to be able to access example.com/foo and show example.com/foo.php. I also want to be able to access example.com/foo/bar and show example.com/foo/bar.php. Lastly I want to reditrect from example.com/foo/ to example.com/foo. Does anyone know how to do this?
This is my code:
RewriteEngine On
DirectorySlash Off
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([^.]+)$ $1.php [NC,L]
I am able to remove the .php extension by I am not able to redirect from the folder to the file. I also tried the solutions below but none of them resulted in the desired behaviour.
htaccess - Rewrite files that have a directory with the same name
.htaccess, rewriting of filename with same name as directory
.htaccess, proper rewriting of directory and file with same name
I found a workaround solution to this problem. In order to remove the extension from file names, I used the following code in my .htaccess file:
RewriteEngine On
DirectorySlash Off
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ $1.php
The above code allows me to go to example.com/foo and show example.com/foo.php. In order to redirect from example.com/foo/ to example.com/foo, I created an index.php file in the /foo directory. I added the following PHP code to the file
<?php
$page = str_replace('/index.php', '', $_SERVER['PHP_SELF']);
header("Location: $page");
?>
This redirects from the directory to the file. Hope this helps.

htaccess RewriteRule using slash as get params: can't access other files in the folder

I have a simple user system in my site. The URL works like this: /profile/user?id=user_id
I wanted to do something like /profile/user/user_id instead, and I found the following code to use in htaccess.
RewriteRule ^user/(.*)$ user.php?id=$1 [L]
The code does work as intended, but after doing that I found out that I can't access any other file inside of the profile directory besides the index file. All of them return a 404 error.
There's a couple more of important pages in there, so I would need to access them. I can't access user.php without an ID either, but that's not my main concern, even though I would also like to access it if possible.
I'm not really used to use RewriteRule, so I'm not really sure what's going on here. What can I do?
EDIT: I was asked to give more info, here we go.
The profile folder full route is /en/profile
Structure of the profile folder:
profile/settings.php (directory index)
profile/user.php
profile/change_avatar.php
profile/change_avatar_upload.php
profile/change_email.php
profile/change_password.php
profile/custom_utgen_characters.php
profile/custom_utgen_character_editor.php
profile/reset_password.php
Other lines in the htaccess file:
DirectoryIndex settings.php
htaccess file in the root folder of the website:
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ $1.php
RewriteCond %{HTTPS} off
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} [R,L]
This is all the relevant option I could get, I hope it helps.
I tested your rewrite rule and directory structure and had no problems to access files in the profile directory. Can you provide more information like the complete .htaccess file and the directory structure laying under profile?
EDIT: The .htaccess in your profile folder overwrites the .htaccess in your root folder. To prevent this, you have to add the line
RewriteOptions inherit
to the .htaccess file in the profile folder.

Remove public_html from url through htaccess

I have a legacy project on a server where the actual framework is in a folder above the public_html folder. So the directory structure is like this:
domainname.com
app/
framework/
public_html/
index.php
Now I want to place this on our own server. This is an application created by the hosting company. The root folder is default. So now my directory structure is like this:
default/
app/
framework/
public_html/
index.php
Now he's pointing to the default folder instead of the public_html folder. That's the only difference with the old version. So I've added a .htaccess file to the root folder default. The content of this file is:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ /public_html/$1 [QSA,L]
</IfModule>
In my public_html folder I have a .htaccess file like this:
Options +FollowSymLinks
RewriteEngine on
RewriteBase /
RewriteCond %{REQUEST_FILENAME} -f
RewriteRule ^ - [L]
RewriteRule . index.php [QSA,NS]
RewriteCond %{ENV:REDIRECT_STATUS} 200
RewriteRule . /index.php [L,QSA,NS]
So when I go to my website it works. But when I click on a link I get: http://domainname.com/public_html/theclickedpage/.
But I don't want the public_html in my url. How can I fix this?
You may want to search for a way to make a virtual host. I know some of my webhost let me configure the vhost through my admin page. It will let your server consider the url without including the specified folder, but back in the stage it performs needed redirection.
You applied correct rule ,
but the problem is link for the solution of this issue you have to
remove public_html from
your all links and it will automatically redirect to your required
path without public_html.

Making server redirect to another folder with .htaccess

Can i make a htaccess file that redirect from the root of my project to another folder.
So if i usually write 192.168.1.39/public to get to my library where i store the files that i want the users to see.
I would like to only write 192.168.1.39 instead.
Can you do that with a .htaccess file?
In the htaccess file in root, add this rule :
RewriteEngine on
RewriteRule ^$ /folder [NC,L]
^$ matches the hompage and rewrites it to the folder.

Skip .htaccess rules for some particular files

Now i am using the following .htaccess rules to modify my urls.My website contains more images and similar files.I found that .htaccess rules increases site's loading time.I wish to skip execution of some codes for domain.com/images and domain.com/scripts folders.
Options +FollowSymLinks
RewriteEngine On
RewriteRule ^([0-9]+)/([0-9]+)/([^.]+).html index.php?perma=$3
RewriteRule ^movies/([^.]+).html gallery.php?movie=$1
RewriteRule ^album/([^.]+).html gallery.php?album=$1
RewriteRule ^img/([^.]+)/([^.]+).html gallery.php?img=$2
RewriteRule ^movies.html gallery.php
i want to modify it to
if it is a file in image or script folder skip following
else
RewriteRule ^([0-9]+)/([0-9]+)/([^.]+).html index.php?perma=$3
else
RewriteRule ^movies/([^.]+).html gallery.php?movie=$1
else
RewriteRule ^album/([^.]+).html gallery.php?album=$1
else
RewriteRule ^img/([^.]+)/([^.]+).html gallery.php?img=$2
else
RewriteRule ^movies.html gallery.php
Any way ?
Instead of having one .htaccess file put one in, for example, the movies folder with the RewriteRule for the movies folder or put one in the img folder with only the RewriteRule that is used in/for the /img/ folder. This means that it will load different .htaccess files based on its directory instead of using one.

Categories