RewriteCondition in .htaccess except for one folder? - apache

I would like to do the following:
I have a htaccess file that makes every request go trough index.php. If I am not mistaken this is called bootstrapping? (I haven't done this before).
I would like to make a subdirectory in the root directory of the site that will serve as a "test" site because I cannot add a subdomain. I need a htaccess rewritecond that will redirect any request under the teszt folder to the index.php in the same folder.
So if I enter example.com/[anything] I get data sent to the index.php in the root and if I enter example.com/teszt/[anything], the data needs to be sent to teszt/index.php
This is my htaccess file:
IndexIgnore *
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-l
RewriteRule ^(.+)$ index.php?url=$1 [QSA,L]

You can use these rules in htaccess in your root folder :
RewriteEngine On
#rewrite /subfolder/foobar to /subfolder/index.php
ReweriteRule !subfolder/index\.php /subfolder/index.php [L]
#rewrite /foobar to /index.php
RewriteRule !index\.php /index.php [L]
The rules above also rewrites your existent files to index.php . If you do not want to rewrite your files , just add a condition to the rule RewriteCond %{REQUEST_FILENAME} !-f .

Related

Rewriting to subdirectory and then to React's index.html using root .htaccess

I am trying to create .htaccess file that will be located in the root webserver directory. In the root, there will be also directories named backend/ and frontend/ + few more (their names are variable).
Current tree may look like this:
/
.htaccess
frontend/
static/
index.html
manifest.json
backend/
files/
index.php
other/
Since the front-end (React) and back-end (PHP) is done by others and is deployed using CI, I don't have any control over the files and folders in that two directories.
What I am trying to accomplish with the .htaccess file is to:
rewrite (not redirect) everything that goes to /** and does not exist itself to /frontend/**
everything in /frontend/** that does not exists should return /frontend/index.html
Here are some examples:
/ becomes /frontend/ (#1)
/index.html becomes /frontend/index.html (#1)
/manifest.json becomes /frontend/manifest.json (#1)
/static/script.js becomes /frontend/static/script.js (#1)
/module becomes /frontend/index.html (#1 and #2)
/backend/** will not be rewritten as the directory exists
/other/** will not be rewritten as the directory exists
My current file looks like this:
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteBase /
# Rewrite root directory to frontend
RewriteCond %{REQUEST_URI} ^/$
RewriteRule ^(.*)$ /frontend/index.html [NC,L,QSA]
# Rewrite non-existent files and folders to frontend
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !^/frontend/
RewriteRule ^(.*)$ /frontend/$1 [NC,L,QSA]
</IfModule>
which mainly fails to rewrite /module to /frontend/index.html.
How should I continue?
Have it like this:
RewriteEngine on
RewriteBase /
# skip backend/ and other/ directories
RewriteRule ^(?:backend|other)/ - [L,NC]
# forward these known paths to /frontend/
RewriteRule ^(index\.html)$ /frontend/$1 [L,NC]
# rewrite an alphanumeric URI that is non-file/dir to /frontend/index.html
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^\w+/?$ /frontend/index.html [L]
# Rewrite non-existent files and folders to frontend
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !^/frontend/ [NC]
RewriteRule ^(.*)$ /frontend/$1 [L]

Apache ModRewrite rules for frontend controller AND `301 Permanently Moved`

What I'm doing right now
I'm developing a backend for a customer.
Previously he mixed up the website data with files to download for friends and other purposes.
Example:
/public
/somedir
somesubdirfile
anotherfile1
anotherfile2
foobar.html
index.html
Now I implemented the common rewrite rules to proxy all requests to the new index.php of the website.
.htaccess
<IfModule mod_rewrite.c>
RewriteEngine "On"
RewriteCond "%{REQUEST_FILENAME}" ".htaccess"
RewriteCond "%{REQUEST_FILENAME}" ".htpasswd"
RewriteCond "%{REQUEST_FILENAME}" "!-s"
RewriteCond "%{REQUEST_FILENAME}" "!-l"
RewriteCond "%{REQUEST_FILENAME}" "!-d"
RewriteRule "^.*$" "public/index.php" [NC,L]
</IfModule>
Fine, works.
Now I moved all the clutter into a separate folder.
/resources
/somedir
somesubdirfile
anotherfile1
anotherfile2
foobar.html
/public
index.php
What I need to accompolish
... is to change the rewrite rules.
If the request doesn't match a specific file or symlink in /public it must be tested if it matches a specific file or symlink in /resources. If there's a match a 301 Moved Permanently must be sent and the request redirected to /resources. If there's no match a redirect to the public/index.php must be done.
But I really stuck in understanding how to write proper rewrite rules in a complexity like I'm requesting.
I need some help here, please.
You may try these rules in your site root .htaccess:
RewriteEngine On
# if it exists in /resources then redirect
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{DOCUMENT_ROOT}/resources/$0 -f
RewriteRule .+ /resources/$0 [L,NE,R=301]
# else route to public/index.php
RewriteCond %{REQUEST_URI} \.(htaccess|htpasswd) [NC]
RewriteCond %{REQUEST_FILENAME} !-s
RewriteCond %{REQUEST_FILENAME} !-l
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^ public/index.php [L]

Apache, rewrite/redirect all to index.php - is not empty REQUESTED_FILENAME

I want to forward everything to index.php except if no file (REQUESTED_FILENAME) was specified or with other words URI is empty or take one /.
I've tried this conditions to rewrite/redirect all to index.php in the .htaccess - file.
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ /index.php/$1 [QSA]
If I call http://my.dom/test.php, the content of the test.php is displayed (!-f).
Next try: Check, if requested file not index.php and then redirect to index.php
RewriteCond %{REQUEST_FILENAME} !index.php
RewriteRule ^(.*)$ /index.php/$1 [QSA,R=301]
Ok this works. But i would like no redirect by empty requested file.
Next try: Redirect if requested file is not empty and requested file is not index.php
RewriteCond %{REQUEST_FILENAME} !^$
RewriteCond %{REQUEST_FILENAME} !index.php
RewriteRule ^(.*)$ /index.php/$1 [QSA,R=301]
If I call http://my.dom/test.php it works fine, redirect to index.php/test.php :D.
If I call http://my.dom or http://my.dom/ its also redirect to index.php.
But i would like no redirect by empty requested file.
Update (1)
I've now this in my .htaccess.
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} -f
RewriteRule ^(.*)$ / [QSA,R=301]
#this redirect all files to root
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.+)$ /index.php/$1 [QSA,L]
#this rewrite on server side to index.php
This doesn't work. http://my.dom/test.php was redirect to http://my.dom/. But the second rule makes an internal (550) error (every time redirect). I've put a new condition to the first section
RewriteCond %{REQUEST_FILENAME} -f
RewriteCond %{REQUEST_URI} !^/.*$
RewriteRule ^(.*)$ / [QSA,R=301]
#this redirect all files to root
By this: http://my.dom/test.php does not redirect.
Please tell me where my mistake is. And maybe a few solutions.
Regards
Its because / is not a file, / points to your root directory. You cant match against your root dir using Request filename variable. To exclude your homepage from the rule, just change your regex pattern to (.+) .
Or you can use the following rule
RewriteEngine on
RewriteRule ((?!index\.php).+) /index.php/$1 [L]

Redirecting all requests to index.php except /files subdirectory

I have very little understanding of .htaccess files so far. I want to redirect all requests to one index.php file in my root folder, except if the url links to a file that exists and is in the files folder. (e.g. example.com/files/picture.png) I want to achieve this with only htaccess file in the root directory and if necessary one in the files directory. Moreover, i don'd want to show a permission denied message.
Right now i have this:
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule . index.php [L]
But it does not redirect one to the index.php file if the file the url refers to exists. Any help? I sought on the internet but could not find anything.
Something like this:
RewriteEngine on
RewriteCond %{REQUEST_URI} /img/.+(gif|png|jpg)$
RewriteRule .* - [L]
RewriteCond %{REQUEST_URI} /css/.+(css|png)$
RewriteRule .* - [L]
RewriteCond %{REQUEST_URI} /js/.+css$
RewriteRule .* - [L]
RewriteCond %{REQUEST_URI} /js/.+(js|htm)$
RewriteRule .* - [L]
RewriteCond $1 !^(index\.php|robots\.txt)
RewriteRule ^(.*)$ /index.php/$1 [L]
All requests will go through the index.php file except for the css javascript and image files. This .htaccess file i used for the codeigniter framework, so it can be a little bit different for your situation.

Redirect to existing file/folder if it exists in specific dir or go to index.php

I have a folder App\ressources that contains all the images, javascript library of the website.
What i'm trying to do is to redirect to the existing image/js or go to the index.php file that will handle the request and get the current module/controller/action.
So i made this :
RewriteEngine On
RewriteCond App/ressources/$1 -d [OR]
RewriteCond App/ressources/$1 -f
RewriteRule ^(.+)$ App/ressources/$1 [L]
RewriteRule ^(.*)$ index.php [QSA]
It shoud not be difficult but "debuging" is not possible.
I've asked for same question but i didn't get an answer.
Here is my workaround:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule (.*) App/ressources/$1 [DPI]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule App/ressources/(.*) $1 [DPI]
RewriteCond %{REQUEST_FILENAME} !-f # missing line, see comments
RewriteRule (.*) index.php [L,QSA,DPI]
Basically we redirect everything (what doesn't exists as file) to App/ressources and if it still doesn't exist we redirect it back to / root dir. Then we redirect it to index.php.
If file not exists redirect to App/resources/
If file still not exists redirect back to root /
If file still not exists redirect to index.php