wildcard .htaccess and reject some address on subdomain - regex

I want all file or directory existed under up.example.com subdomain rejected and show 404 error.
Also I want all another request point to index.php.
I write this code:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^up\.(.+)$ [NC]
RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^(.*)$ - [R=404,L,NS]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . index.php [L,QSA]
When I test this code for http://up.example.com/test.jpg
If test.jpg file existed on root server, show me this error:
Not Found
The requested URL /test.jpg was not found on this server.
Apache Server at up.example.com Port 80
It's ok!
But when test.jpg not existed, give me this error:
Not Found
The requested URL /index.php was not found on this server.
Apache Server at up.example.com Port 80
Instead open index.php (index.php exist on root)
Why? and how can I fix it?

Actually the problem is that you write everything to index.php and that becomes a valid file in the request. When mod_rewrite runs next time first rule sends it to 404.
To overcome this error have your rules like this:
RewriteEngine On
# if it is valid file or directory except for index.php
# then send 404 to browser
RewriteCond %{HTTP_HOST} ^up\. [NC]
RewriteCond %{THE_REQUEST} \s/+index\.php[?\s] [NC]
RewriteRule ^index\.php$ - [R=404,L,NC]
RewriteCond %{HTTP_HOST} ^up\. [NC]
RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule !^index\.php$ - [R=404,L,NC]
# send all non-file/directories to index.php
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . index.php [L]

Try this one
RewriteEngine On
RewriteCond %{HTTP_HOST} ^up\.(.+)$ [NC] # remove if it is not related to subdomain
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . index.php [L,QSA]
RewriteCond %{HTTP_HOST} ^up\.(.+)$ [NC]
RewriteCond %{REQUEST_URI} !^/index\.php
RewriteRule ^(.*)$ - [R=404,L,NS]

Related

htaccess File Rewriting API Routes

I am trying to redirect all incoming traffic to my site to index.php and put the remainder of the url in a path variable. I also have a index.html in my root directory and if the user accesses it directly I want them rerouted to the index.php script.
RewriteEngine On
RewriteBase /
RewriteCond $1 !^index\.html
RewriteCond $1 !^index\.php
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^index\.html?$ / [NC,R,L]
RewriteRule ^(.*)$ index.php?path=$1 [L,QSA]
The rerouting seems to be working ok, but all my /api/index.php?route=xx&mode=xx seem to be getting rerouted too. Is there a way to exclude the api directory from the rewrite condition?
Could you please try following. This is checking if REQUEST_URI is not starting from /app/index.php then reroute everything to index.php along with passing parameters to it, couldn't test it as of now, will test it few mins or so.
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !^/api/index\.php/?$ [NC]
RewriteRule ^(.*)$ index.php?path=$1 [QSA,L]
RewriteCond %{REQUEST_FILENAME} -f
RewriteCond %{REQUEST_URI} ^/api/index(\.html)/?$ [NC]
RewriteRule ^(.*)$ index.php [NC,L]

htaccess rewrite rule with single parameter

When user enters url like
http://example.com/app/abcd123/
I want to show hime page from
http://example.com/app/index.php?param=abcd123
Without changing URL in browser.
I put .htaccess file inside app folder with code
RewriteEngine on
RewriteCond %{REQUEST_URI} ^/index.php [NC]
RewriteCond %{QUERY_STRING} ^param=(.*)
RewriteRule (.*) http://example.com/app/%1? [R=301,L]
You can use this code in /app/.htaccess:
RewriteEngine on
RewriteBase /app/
# external redirect from actual URL to pretty one
RewriteCond %{THE_REQUEST} /index\.php\?param=([^\s&]+) [NC]
RewriteRule ^ %1? [R=302,L,NE]
# internal forward from pretty URL to actual one
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.+?)/?$ index.php?param=$1 [L,QSA]
Try this .htaccess code. It should help you.
Make sure the rewrite base should be the form the root to your present directory.
RewriteBase /app/
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-s
RewriteRule ^(.*)$ index.php?param=$1 [QSA,NC,L]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^(.*)$ index.php [QSA,NC,L]
RewriteCond %{REQUEST_FILENAME} -s
RewriteRule ^(.*)$ index.php [QSA,NC,L]
In this, if the user enters like http://example.com/app/qwerty the call will be processed like http://example.com/app/index.php?params=qwerty
This should be working, I tested it. Let me know if you face any troubles.
RewriteEngine On
RewriteBase /app
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php?param=$1 [L]
Check it there: http://htaccess.mwl.be/ or other online htaccess test service

.htaccess make subdirectory root for domain

I am trying to use htaccess to make a subdirectory the root for an additional domain. I currently have:
RewriteEngine on
RewriteCond %{HTTP_HOST} ^(www.)?thedrive.co$
RewriteCond %{REQUEST_URI} !^/test/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /test/$1
RewriteCond %{HTTP_HOST} ^(www.)?thedrive.co$
RewriteRule ^(/)?$ test/index.php [L]
It works great and hides the 'test' directory in the URL. But, when a file in a subdirectory is requested it shows the 'test' directory. For example, instead of thedrive.co/somefolder it shows thedrive.co/test/somefolder.
How can I get it to not display that?
Insert a 301 rule right at the top below RewriteEngine on line:
RewriteEngine on
RewriteCond %{HTTP_HOST} ^(www\.)?radioreformation\.co$ [NC]
RewriteCond %{THE_REQUEST} \s/+radioreformation.com/(\S*) [NC]
RewriteRule ^ /%1 [R=301,L]
RewriteCond %{HTTP_HOST} ^(www.)?radioreformation.co$
RewriteCond %{REQUEST_URI} !^/radioreformation.com/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /radioreformation.com/$1 [L]
RewriteCond %{HTTP_HOST} ^(www.)?radioreformation.co$
RewriteRule ^(/)?$ radioreformation.com/index.php [L]
This will redirect thedrive.co/test/somefolder to thedrive.co/somefolder

htaccess rewrite - prevent multiple redirects

The following regex works perfectly except for under the condition when the user goes to examplesite1.com/site1. When this happens the page fails because of multiple redirects in which it continues to add the request=site1 query to the URL. I thought that adding RewriteCond %{ENV:REDIRECT_STATUS} ^$ would fix this issue however that seemed to not be the case.
This setup is for a multi-domain website. There are three folders site1, site2, and shared. There are also rewrite rules for site2 and shared that are not shown as they do not effect this issue.
# if the resource exists in site1 return it to the user
RewriteCond %{HTTP_HOST} examplesite1.com$
RewriteCond %{DOCUMENT_ROOT}/site1/%{REQUEST_URI} -f
RewriteRule ^(.*)$ /site1/$1 [QSA,L]
# no file found, send URI to CMS
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ /index.php?request=$1 [QSA,L]
complete .htaccess file:
AddDefaultCharset UTF-8
# Disallow browsing file directories
Options -Indexes
RewriteEngine on
RewriteBase /
#remove the trailing slash
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_URI} (.*)/$
RewriteRule ^(.+)/$ /$1 [R=301,L]
RewriteCond %{HTTP_HOST} examplesite1.com$
RewriteCond %{DOCUMENT_ROOT}/site1/%{REQUEST_URI} -f
RewriteCond %{ENV:REDIRECT_STATUS} ^$
RewriteRule ^(.*)$ /site1/$1 [QSA,L]
RewriteCond %{HTTP_HOST} examplesite2.com$
RewriteCond %{DOCUMENT_ROOT}/site2/%{REQUEST_URI} -f
RewriteCond %{ENV:REDIRECT_STATUS} ^$
RewriteRule ^(.*)$ /site2/$1 [QSA,L]
RewriteCond %{DOCUMENT_ROOT}/shared/%{REQUEST_URI} -f
RewriteRule ^(.*)$ /shared/$1 [QSA,L]
# resources not to be public are sent to CMS
RewriteCond %{REQUEST_FILENAME} (\.inc\.|\.tpl$)
RewriteRule ^(.*)$ /index.php?request=$1 [QSA,L]
# no resource found, send URI to CMS
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{ENV:REDIRECT_STATUS} ^$
RewriteRule ^(.*)$ /index.php?request=$1 [QSA,L]
Try adding an extra condition:
RewriteCond %{HTTP_HOST} examplesite1.com$
RewriteCond %{REQUEST_URI} !^/site1/
RewriteCond %{DOCUMENT_ROOT}/site1/%{REQUEST_URI} -f [OR]
RewriteCond %{DOCUMENT_ROOT}/site1/%{REQUEST_URI} -d
RewriteRule ^(.*)$ /site1/$1 [QSA,L]
What looks like may be happening here is that you need to prevent a redirect from happening after one of your other rules get applied. Try adding this right below RewriteBase /:
RewriteCond %{ENV:REDIRECT_STATUS} 200
RewriteRule ^ - [L]

prevent access to a subfolder in a domain after a redirect

Assuming i have the following "mydomain.com" that has is pointed to the folder "public_html".
I wanted to redirect all mydomain.com to mydomain.com/prod/public folder so i edited the .htaccess file as follows:
RewriteEngine on
RewriteCond %{HTTP_HOST} ^(www.)?mydomain.com$
RewriteCond %{REQUEST_URI} !^/prod/public/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /prod/public/$1
RewriteCond %{HTTP_HOST} ^(www.)?mydomain.com$
RewriteRule ^(/)?$ prod/public/index.php [L]
the changes work as expected, YAY!!!
how do i prevent access to the site via the following request:
http://mydomain.com/prod/public/
or
http://mydomain.com/prod/public/index.php
Place this rule as first rule in your prod/public/.htaccess file:
RewriteEngine on
# direct access forbidden
RewriteCond %{THE_REQUEST} ^[A-Z]{3,}\s/+prod/public(/index\.php|/?)?[\s?] [NC]
RewriteRule ^ - [F]