Htaccess ignore folder - php

I'im trying to make a admin system for a site, but the htaccess ruins it D:
The admin part is in a folder named admin.
My htacces to far:
Options +FollowSymLinks -Indexes
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(admin)$ admin/index.php
RewriteRule ^([\wæøå]+)$ index.php?page=$1 [QSA]
RewriteRule ^([\wæøå]+)/$ index.php?page=$1 [QSA]
RewriteRule ^(nyhed)/([\w\d\-æøå]+)$ index.php?page=$1&nyhed=$2 [QSA]
RewriteRule ^(nyhed)/([\w\d\-æøå]+)/$ index.php?page=$1&nyhed=$2 [QSA]
But won't let me in that folder?
So I did a if in my index:
if($_GET["page"] == "admin"){
header("location:http://google.com");
}
But that won't do anything either, and no errors D:

RewriteCond lines can be strung together as you have them, but they only impact the first rule they come to.
Options +FollowSymLinks -Indexes
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ index.php?page=$1 [QSA]
This will send all requests for non-existent directories and files to index.php. Files and directories that exist won't be impacted. The other rules are unnecessary.

This is your problem:
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
You are rewriting to a directory named admin which obvously exists in your file system as such Apache says:
if this path does not exist as a directory path or as a file path continue with the rewrites
That is why it is not quite rewriting the way you would expect.

Related

Rewrite ALL requests to a folder with .htaccess file

I'm trying to rewrite all requests in a folder like this:
https://www.test.com/banana/apple (or whatever) goes to https://www.test.com/public but in the url is still https://www.test.com/banana/apple.
How can I do this?
Inside that specific folder you can create a .htaccess with this rule:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^ /public/ [L]
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^.*$ /public/index.php [NC,L,QSA]
This .htaccess will rewrite all requests to /public/index.php. Existing files and directories will still be served.

.htaccess exclude images from rewrite condition

I've been trying to exclude png and jpg files from being excluded from the rewrite condition. Currently, I have a set of pictures that are being presented as broken images. What is causing the error is a rewrite rule in my .htaccess. That rule overwrites the URL of a folder so it will appear with a different name in the URL, instead of /volley it appears as JeepVolleyballChampionship. I've tried to get rid off the last part that redirects to the home page if a directory doesn't exist, but nothing seems to work. I've also tried to include an exclusion of files with extensions of png and jpg from the rewrite block that rewrites the directory...
What I assume is that I'm not implementing that exclusion correctly. My page still loads fine, except with the broken images. Can you provide insight of what I'm doing wrong in this block?
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteCond %{REQUEST_URI} !(\.css|\.js|\.png|\.jpg|\.gif)$ [NC]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^JeepVolleyballChampionship(.*)$ /volley [L,QSA]
</IfModule>
My whole .htaccess is provided here:
#the non-existent JeepVolleyballChampionship folder leads to the volley folder without showing it or the extension
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.html -f
RewriteRule ^(.*)$ $1.html
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.php -f
RewriteRule ^(.*)$ $1.php
DirectorySlash Off
RewriteEngine on
#try to make it so that /volley/ is eliminated from folders and name changes but it is still under this structure
RewriteRule ^volley$ /volley/index.php [L,E=LOOP:1]
RewriteCond %{ENV:REDIRECT_LOOP} !1
RewriteRule ^volley/$ /volley [R=301,L]
RewriteCond %{ENV:REDIRECT_LOOP} !1
RewriteRule ^volley/index.php$ /volley [R=301,L]
#the non-existent JeepVolleyballChampionship folder leads to the volley folder without showing it or the extension
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteCond %{REQUEST_URI} !(\.css|\.js|\.png|\.jpg|\.gif)$ [NC]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^JeepVolleyballChampionship(.*)$ /volley [L,QSA]
</IfModule>
#non-existent folder to default root
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ / [L,QSA]
</IfModule>
jpg and jpeg are treated as separate extensions here. Add jpeg as well on the line no 3. It may fix your issue, if your extension is "jpeg".
It will look as follows:
RewriteCond %{REQUEST_URI} !(\.css|\.js|\.png|\.jpg|\.jpeg|\.gif)$ [NC]
You should not be entering "RewriteEngine on" multiple times, and you also need to be careful about the order of your rules. Off the top of my head I think all you should need is this:
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^/volley /JeepVolleyballChampionship [R=301]
The !-f condition will prevent the images from being rewritten (along with all other files) so long as they exist, this is the canonical way to exclude all existing files like js, css, etc.
Then the pattern in the rule will only rewrite URLs that start with /volley, so you do not need a condition to match it -- in fact, the rule pattern is checked BEFORE the conditions, so matching there is faster than matching everything and then checking the conditions.

.htaccess PHP - Prevent rewriting when only domain is entered

I have a site located at
application
controllers
views
...
public <- the root of the site
index.php <- entry point
css
...
So the url is http://localhost/application/public. I would like when the user enters only http://localhost/application/public to do nothing except calling index.php without showing it.
Here are some examples:
http://localhost/application/public -> http://localhost/application/public or http://localhost/application/public/
BUT
http://localhost/application/public/asdf -> Rewrite internally to http://localhost/application/public/index.php and show only http://localhost/application/public/asdf
I have written the .htaccess. The only problem is that when entering http://localhost/application/public it opens http://localhost/application/public/index.php instead of http://localhost/application/public
One more thing - I want existing directories to be rewritten, so only existing files should not be rewritten to index.php So basically http://localhost/application/public/css should be rewritten to index.php. The reason I want this is to ommit the 403 Forbidden error, and kindly tell the user that the path does not exist.
HTACCESS
Options +FollowSymLinks -MultiViews -Indexes
RewriteEngine On
RewriteBase /application/public/
#Remove trailing slash
RewriteCond %{THE_REQUEST} \s(.+?)/+[?\s]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.+?)/$ $1 [R=301,L]
#Do the rewriting
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [NC,L]
The reason I put RewriteCond %{REQUEST_FILENAME} !-d is to prevent http://localhost/application/public from rewriting. If http://localhost/application/public is rewritten, it loads http://localhost/application/public/index.php
Actually I know there are needed some modifications on RewriteCond %{REQUEST_FILENAME} !-d. I want the root, public folder to not get rewritten, it should be the only exception. All of the rest physical folders like css, js, have to be rewritten, like not existing. Files should not be rewritten. Thanks!
Ok, I think I found a solution.
Options +FollowSymLinks -MultiViews -Indexes
RewriteEngine On
RewriteBase /application/public/
RewriteRule ^(css/?|js/?)$ index.php [NC,L] <- Folders css, js will be rewritten, but root directory won't be rewritten. Files inside them also won't be rewritten.
#Remove trailing slash
RewriteCond %{THE_REQUEST} \s(.+?)/+[?\s]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.+?)/$ $1 [R=301,L]
#Do the rewriting
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [NC,L]

HTAccess Url rewriting rules

I'm trying to simply the access to my API with a .htaccess file and I'm facing an issue.
For example, what I'd like to achieve is:
User reaches http://localhost/teammngr/user/photos/secrethash and is automatically redirected to http://localhost/teammngr/user/photos.php?sid=secrethash.
User reaches http://localhost/teammngr/user/config/secrethash and is automatically redirected to http://localhost/teammngr/user/config.php?sid=secrethash.
User reaches http://localhost/teammngr/team/members/secrethash and is automatically redirected to http://localhost/teammngr/team/members.php?sid=secrethash.
If the user wants to reach these files directly, it is supposed to be possible.
Moreover, the url http://localhost/teammngr/ will be under a subdomain like http://team.mywebsite.com/.
So far, I've made the following .htaccess file, but it keeps throwing a 500 error on my server.
To be clear, this file is not in the root directory but in a sub dir.
Options +FollowSymlinks
RewriteEngine On
RewriteBase /teammngr
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^user/([a-z_]+)/([A-Za-z0-9_-]+)$ /user/$1.php?sid=$2 [L]
RewriteRule ^team/([a-z_]+)/([A-Za-z0-9_-]+)$ /team/$1.php?sid=$2 [L]
Where did I make a mistake ?
Thanks for your precious help.
RewriteCond is only applicable to very next RewriteRule. To avoid repeated RewriteCond before every rule you can have a separate rule to ignore requests for files and directories. Also remove / from your target URIs.
Options +FollowSymlinks -MultiViews
RewriteEngine On
RewriteBase /teammngr/
# ignore requests for files and directories from rules below
RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^ - [L]
RewriteRule ^user/(\w+)/([\w-]+)/?$ user/$1.php?sid=$2 [L,QSA]
RewriteRule ^team/(\w+)/([\w-]+)/?$ team/$1.php?sid=$2 [L,QSA]
you can try this:
Options +FollowSymlinks
RewriteEngine On
RewriteBase /teammngr
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^user/([a-z_]+)/([A-Za-z0-9_-]+)$ user/$1.php?sid=$2 [L]
RewriteRule ^team/([a-z_]+)/([A-Za-z0-9_-]+)$ team/$1.php?sid=$2 [L]

.htaccess mod_rewrite in main directory and subdirectory

I've a main directory with a .htaccess file that looks like this
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^ index.php [L]
The one in the subdirectory looks identical to this one. What I want the .htaccess files to do is redirect to the index file in each directory.
So if you visit main/example you get redirected to the index.php file in the main directory but if you visit main/subdirectory you get redirected to the index.php file in the subdirectory.
I've searched for an answer but haven't found anything that works.
Thankful for any help!
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)/ /$1/index.php [L]
This configuration do the following:
if we have this files
/index.php
/foo/index.php
/var/index.php
/var/boo/index.php
and you request the follow file
/foo/hello
you will go to:
/foo/index.php
and, if you request
/var/boo/world
you will go to:
/var/boo/index.php
Try
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond $1 -d
RewriteRule ^(.+)/ $1/index.php [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^ index.php [L]
You can use a simple solution like this one, no need to put .htaccess files everywhere
on your main directory you put one .htaccess file, you keep your code
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^ index.php [L]
and you add those two lines
Options All -Indexes
ErrorDocument 403 http://domain.com/index.php
these two lines prevent the user to access the folder, and returns the statut 403 and then you redirect your 403 statut to the adress of your index.php of your main directory

Categories