.htaccess block all php files with the exepction of some files - .htaccess

Im trying to make a .htaccess file that blocks every folders and file with the exepction of the folder public, the file index.php, the file ajax.php, img, css and js. It should also redirect you to www.domain.com/index.php no matter what you type in the url. Almost everything is working but the ajax.php file is still blocked so my javascript files cant access it. Any suggestions?
My index.php is placed in the folder public, all other php files is placed in a folder named php, js, css and so on but they are not inside public. its like this /root/public and /root/php and so on. The .htaccess file is placed in /root/
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ public/index.php?_url=$1 [QSA,L]
RewriteCond %{REQUEST_URI} !^/(?:public/|index\.php$|ajax\.php$|.+\.(?:jpe?g|gif|png|ico|tiff|css|js)$) [NC]
RewriteRule . - [F]

Have it this way:
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_URI} !^/(?:public/|(?:index|ajax)\.php$|.+\.(?:jpe?g|gif|png|ico|tiff|css|js)$) [NC]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.+)$ public/index.php?_url=$1 [QSA,L]

Related

Redirection works but cannot load .css

Hello.
I have a site protected by a .htaccess which redirects everything to the /pb directory except the /cron directory which is directly accessible (later it will be protected).
My htaccess works (it redirects as I want) BUT I cannot load the .css .js files that I call directly into my index page.
Do I have to put a new .htaccess in my /pb directory to allow access to .css or can I do it in my root htaccess ? I do not understand.
My .htaccess file (the https lines can't be removed "hostinger")
RewriteEngine on
RewriteCond %{HTTPS} off
RewriteCond %{HTTP_HOST} (www\.)?XXXXXXXXX.net
RewriteRule (.*) https://%{HTTP_HOST}%{REQUEST_URI} [L,R=301]
RewriteRule ^(cron) - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^pb/ - [L]
RewriteCond %{DOCUMENT_ROOT}/pb/$1 -f
RewriteRule (.+) pb/$1 [L]
RewriteRule (.*) pb/index.php?path=$1 [L,QSA]
order deny,allow
deny from all
allow from XX.XXX.XXX.XX
Thanks for your help
First thing first, I have fixed your htacces rules file, comments have been made inside Rules file. Have your htaccess rules file like as follows style, make sure its placed in same folder where pb folder is there. Also please do clear your browser cache before testing your URLs.
Options -Indexes -MultiViews
RewriteEngine on
##Apply https rules here.
RewriteCond %{HTTPS} off
RewriteCond %{HTTP_HOST} ^(?:www\.)?XXXXXXXXX.net [NC]
RewriteRule ^ https://%{HTTP_HOST}%{REQUEST_URI} [NE,L,R=301]
##Handling base URI of site.
RewriteRule ^/?$ pb/ [R=301,L]
##Blocking pb uri pages non existing ones.
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^pb/ - [NC,L]
##making all uris which are files to pb folder.
RewriteCond %{DOCUMENT_ROOT}/pb/$1 -f
RewriteRule ^(.+)/?$ pb/$1 [L]
##Making non existing uris to index.php
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)/?$ pb/index.php?path=$1 [L,QSA]
order deny,allow
deny from all
allow from XX.XXX.XXX.XX
JS/CS rewrite/redirect:
You may need to use base tag to fix your js and other relative resources. If you are linking js files using a relative path then the file will obviously get a 404 because its looking for URL path. for example if the URL path is /file/ instead of file.html then your relative resources are loading from /file/ which is not a directory but rewritten html file. To fix this make your links absolute or use base tag. In the header of your webpage add this <base href="/"> so that your relative links can load from the correct location.

Htaccess rewrite url only in one folder

I useing this code in htaccess:
RewriteRule ^([^/]*)/([^/]*)$ /etel.php?q=$1&n=$2 [L]
This control this
domain.com/etel.php?q=501&n=Beef
to this:
domain.com/501/Beef
But if I open the [partner] folder without index.php the htacces open the etel.php. How can I limit that rewrite rule to the parent folder locked out other folder files?
You may use this rule with these 2 RewriteCond to exclude existing files and directories:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)/([^/]+)/?$ /etel.php?q=$1&n=$2 [L,QSA]

Mod-rewrite for css, js, html and directories in Apache/PHP

I am using mod_rewrite in .htaccess. I can't figure out how to get the following done.
All css pages should go to /minify/?f=/styles.css
All js pages should go to /minify/?f=/js.js
All .txt, .gif, .jpg, .png files should open as they are
All other pages (eg: /page, /dir/page, /dir1/dir2/page) should go to /page.php
All other files in directories should open as it is (eg: /admin/login.php)
Edit:
This is the code that I came up with. It's not elegant, and it fails sometimes. For eg: I can't get /admin/login.php to work. When I open that URL, it's going to page.php.
RewriteEngine on
RewriteCond %{REQUEST_URI} !(\.css|\.js|\.png|\.jpg|\.gif|\.txt)$
RewriteRule .* page.php
RewriteRule ^.*?\.css$ /min/?f=/styles.css [L]
RewriteRule ^.*?\.js$ /min/?f=/js.js [L]
Try the following:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} !\.(css|js|png|txt|gif|jpe?g)$
RewriteRule ^ /page.php [L]
RewriteRule \.css$ /minify/?f=/styles.css [NE,L]
RewriteRule \.(js)$ /minify/?f=/$1.$1 [NE,L]

Codeigniter index.php breaking image paths

When I don't edit my .htaccess file, my image path reads something like: http://this.website.com/codeigniter/inc/images/logo.jpg.
However, as soon as I add this code into the .htaccess file:
RewriteEngine On
RewriteCond $1 !^(index\.php)
RewriteRule ^(.+)$ index.php?$1 [L]
(to remove the index.php from the URL), I get 404 error, even with the exact same path.
How do I remove index.php and still have access to my images?
RewriteEngine On
RewriteCond $1 !^index\.php [NC]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.+)$ index.php?$1 [L]
The two new RewriteConditions would exclude existing directories and files (and so images) from redirection.

.htaccess clean URL and relative paths

I have the following in my .htaccess which is :
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)$ /index.php?type=cat&id=$1 [QSA,L]
RewriteRule ^page/([^/]+)$ /index.php?type=page&name=$1
This seems to work just fine but relative paths to pictures and css files inside of index.php become broken in the second case (Page). did not work. In second case, all images are pointing to page/images/ instead of image/
Other than hardcoding the actual path to images, is there any other way to fix this?
images, css, js folders are located in the root. This is how the root looks like
.htaccess
index.php
images/
css/
js/
RewriteCond directives only apply to the rule directly following them.Try the follwing
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/]+)$ /index.php?type=cat&id=$1 [QSA,L]
RewriteCond %{REQUEST_URI} !\.(css|js|jpe?g|gif|png)$ [NC]
RewriteRule ^page/([^/]+)$ /index.php?type=page&name=$1 [L]
#rewrite requests for page/images to images
RewriteCond %{REQUEST_URI} ^/page(/images/.+)$ [NC]
RewriteRule . %1 [L]
EDIT.
Modified to rewrite page/images to images

Resources