.htaccess redirect if file exists in subfolder - apache

I have the following challenge. Assuming the following file is requested from the server:
/pages/subfolder/mypage.php
Is there an option using mod-rewrite to do the following:
Look into a specific folder f. e. /USERMOD if the requested file (and structure) exists
If Yes, execute this file with all parameters
If Not, execute the requested file.
The background would be that the rewrite looks if there is a replacement under /USERMOD having the same folder structure. If an replacement exists use this one otherwise us the original file.
Actually (but doesn't work) I have is:
RewriteCond %{REQUEST_URI} !^/USERMOD/ [NC]
RewriteCond %{DOCUMENT_ROOT}/USERMOD/$1 -f [NC]
RewriteRule ^(.*)$ /USERMOD/$1 [L]
Thanks for any hint and help!
EDIT: The full .htaccess
RewriteEngine On
RewriteCond %{REQUEST_URI} !^/USERMOD/ [NC]
RewriteCond %{DOCUMENT_ROOT}/USERMOD/$1 -f [NC]
RewriteRule ^(.*)$ /USERMOD/$1 [L]
RewriteCond %{REQUEST_FILENAME} ^(.*)\.(php|css|js|gif|jpg|jpeg|png)$ [NC]
RewriteRule ^(.+) - [L]
RewriteCond %{REQUEST_URI} (.*)?/admin/(.*)
RewriteRule ^(.+) - [L]
RewriteCond %{REQUEST_URI} (.*)?/images/(.*)
RewriteRule ^(.+) - [L]
RewriteCond %{REQUEST_URI} (.*)?/templates/(.*)
RewriteRule ^(.+) - [L]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^(.+) - [L]
RewriteCond %{REQUEST_FILENAME} -f
RewriteRule ^(.+) - [L]
RewriteCond %{REQUEST_FILENAME} -l
RewriteRule ^(.+) - [L]
##boosted CONTENT
RewriteRule (.*/)?info/([A-Za-z0-9_-]+)\.html.* shop_content.php?gm_boosted_content=$2&%{QUERY_STRING} [PT,L]
##boosted PRODUCTS
RewriteRule (.*/)?([A-Za-z0-9_-]+)\.html product_info.php?gm_boosted_product=$2&%{QUERY_STRING} [PT,L]
##boosted CATEGORIES
RewriteRule (.*/)?([A-Za-z0-9_-]+)/?.* index.php?gm_boosted_category=$2&%{QUERY_STRING} [L]

Have your initial rules like this:
RewriteEngine On
RewriteRule ^USERMOD/ - [NC,L]
RewriteCond %{DOCUMENT_ROOT}/USERMOD/$1 -f [NC]
RewriteRule ^(.*)$ /USERMOD/$1 [L]
# rest of your rules come here

Related

.htaccesss: allow to load resource files

I am trying to do the following.
denied directly access to example.domain.com
allow access from to example.domain.com from another.domain.com
allow to load resource files (css, js) when the the user comes form another.domain.com to example.domain.com
Points 1 and 2 i solved like this:
RewriteCond %{HTTP_REFERER} !http(s)?:\/\/(www\.)?another\.domain\.com\/$ [NC] RewriteRule ^.*$ - [F]
But i can not to load resource files. I get the error message 403.
I tried with this code, but it's not working.
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond $1 ^(index\.html|robots\.txt)
RewriteRule ^.*$ ^.*$ [L]
RewriteCond %{HTTP_REFERER} !http(s)?:\/\/(www\.)?another\.domain\.com\/$ [NC]
RewriteRule ^.*$ - [F]
I am a quite new with htaccess, can someone help me to solve this problem?
With your shown samples, please try following .htaccess rules file. Make sure to clear your browser cache before testing your URLs.
RewriteEngine ON
RewriteCond %{ENV:REDIRECT_STATUS} ^$
RewriteCond %{HTTP_HOST} ^(www\.)?example\.domain\.com$ [NC]
RewriteRule ^ - [F,L]
RewriteCond %{HTTP_HOST} ^(www\.)?another\.domain\.com$ [NC]
RewriteRule ^(.*)/?$ http://example.domain.com/%1 [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} (?:index\.html|robots\.txt)/?$ [NC]
RewriteRule ^ - [F,L]
i have found a solution for me:
RewriteCond %{REQUEST_FILENAME} ^.+(\.js|\.css|\.json|\.jpg|\.png|\.woff2|\.woff|\.ttf)$
RewriteRule ^.*$ - [L]
RewriteCond %{HTTP_REFERER} !http(s)?:\/\/(www\.)?another\.domain\.com\/$ [NC
RewriteRule ^.*$ - [F]

rewrite {query}.m3u8 to m3u8.php?v={query}

Template:
{query}.m3u8
Example:
m3u8.php?v=AAAA-000
This is what I tried:
RewriteCond %{REQUEST_FILENAME}.m3u8 -f
RewriteRule ^(.*)$ m3u8.php?v=$1
Could you please try following.
RewriteEngine ON
RewriteCond %{REQUEST_URI} ^/([^.]*)\.m3u8/?$ [NC]
RewriteRule ^(.*)$ /m3u8.php?v=%1 [NC,L]
OR if you want to check if requested filename in browser is present in your system then add extra condition like:
RewriteEngine ON
RewriteCond %{REQUEST_FILENAME} -f
RewriteCond %{REQUEST_URI} ^/([^.]*)\.m3u8/?$ [NC]
RewriteRule ^(.*)$ /m3u8.php?v=%1 [NC,L]

Reading file from different directory with htaccess

I want something like this:
If a request comes to domain.com/photos/1.jpg it should read file from new_photos/1.jpg directory. But url should still look same. I tried with this line to do this:
RewriteRule ^photos/(.*)$ new_photos/?$1
It doesn't work. How can I fix it?
Edit:
My full htaccess file:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^www\.(.+)$ [NC]
RewriteRule ^(.*)$ http://%1/$1 [R=301,L]
RewriteCond %{REQUEST_URI} ^/system.*
RewriteRule ^(.*)$ index.php/?$1 [QSA,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.+)$ index.php/?$1 [QSA,L]
RewriteCond %{HTTP_HOST} ^app.domain.com$
RewriteRule ^robots\.txt$ robots-subdomain.txt
If the file is in /new_photos dir ,You can use this :
RewriteRule ^photos/(.*)$ new_photos/$1 [NC,L]

url rewrite for subfolder

I'm trying to do rewrite only for a subdirectory, but I have a problem with RewriteCond.
All my files are in /abc.
Here is my htaccess (/abc/.htaccess) file:
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} -f
RewriteCond %{REQUEST_URI} ^/static/ [NC,OR]
RewriteCond %{REQUEST_URI} ^/files/ [NC]
RewriteRule . - [L]
RewriteRule (.*) index.php?_url=$1 [QSA,L]
So I want to rewrite every url to index.php except the existing files in /static and /files. But this code is rewriting /static/a.jpg too, since the request was /abc/static/a.jpg. I tried setting RewriteBase to /abc/, but nothing changed.
Any idea how can I solve this?
Have your .htaccess like this in /abc/.htaccess:
RewriteEngine on
RewriteBase /abc/
RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_URI} ^/(static|files)(/|$) [NC]
RewriteRule . - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule (.*) index.php?_url=$1 [QSA,L]

How can I disable RewriteRule for one subcategory?

I have a .htaccess in my root of website that looks like this:
RewriteEngine On
RewriteCond %{HTTP_HOST} !^www\.mydomain\.pl [NC]
RewriteCond %{HTTP_HOST} ^(?:www\.)?([a-z0-9_-]+)\.mydomain\.pl [NC]
RewriteRule ^/?$ /index.php?run=places/%1 [L,QSA]
RewriteCond %{REQUEST_URI} !^/index.php$
RewriteCond %{REQUEST_URI} !^/images/
RewriteCond %{REQUEST_URI} !^/upload/
RewriteCond %{REQUEST_URI} !^/javascript/
RewriteRule ^(.*)$ /index.php?runit=$1 [L,QSA]
But when I type:
mydomain.pl/guests
I would like to go normally to actual folder guests. I understand that I need to somehow disable this rule for guests subfolder, but how do I do this?
EDIT:
I've included whole .htaccess file
I've put a htaccess in subfolder and added RewriteEngine Off and it works.
Add one of these
RewriteEngine On
#If the request starts with guests, then pass it through
RewriteCond %{REQUEST_URI} ^guests
RewriteRule ^ - [L,QSA]
#Or, if the request contains an existing filename or directory, pass it through
RewriteCond %{REQUEST_FILENAME} -d [OR]
RewriteCond %{REQUEST_FILENAME} -f
RewriteRule ^ - [L,QSA]