How to restrict directory access - .htaccess

I have folder directory http://test.com/app/cl/login.php. If anyone tries to access app folder, it can. Please let me know how to stop them accessing it and redirect the url to http://test.com/ if anyone tries to access app folder directly. I tried to do it through htaccess but it blocks the whole website
The options I tried
RewriteEngine On
RewriteRule ^app/ - [L,R=404]
RewriteEngine on
RewriteCond %{HTTP_HOST} ^test\.com\/app [NC,OR]
RewriteRule ^(.*)$ http:\/\/test\.com\/$1 [L,R=301,NC]
Redirect 301 /app test.com

You can use the following rules :
RewriteEngine On
#1) redirect "/app/"
RewriteRule ^app/?$ http://example.com/ [L,R]
#2) redirect "/app/cl/"
RewriteRule ^app/cl/?$ http://example.com/ [L,R]
You can alternatively accomplish this using the RedirectMatch directive :
RedirectMatch ^/app/?$ http://example.com/
RedirectMatch ^/app/cl/?$ http://example.com/

Related

Old Domain 301 Redirect to New Domain with HTAccess Wildcard

I have a old domain lets call it
example.com
I want to do a 301 redirect of all its page and its homepage to
newsite.com
So for example
example.com/category/page.html
Should 301 redirect to
newsite.com/category/page.html
And also
example.com should redirect to newsite.com
Be it with www or without www
I tried the following
RewriteEngine on
RewriteCond %{HTTP_HOST} ^example.com [NC,OR]
RewriteCond %{HTTP_HOST} ^www.example.com [NC]
RewriteRule ^(.*)$ http://www.newsite.tv/$1 [L,R=301,NC]
My folder structure is
index.html
.htaccess
When I go in the site, be it with wildcard or not, it only load the index.html and the .htaccess redirect is not working.
I did enable modrewrite
Can anyone guide me to set up the right .htaccess for this case.
Thanks!
You can also use RedirectMatch directive .
Try this in Olddomain/.htaccess :
RedirectMatch ^/(.*)$ http://newsite.com/$1

Cant redirect folder exluding one subfolder - htaccess

I am having problems writing some redirect rules. I want all the pages inside the "/es" folder to be redirected to my homepage but excluding the pages in /es/empresa that need to redirect to mydomain.com/nosotros/. I have written this code but it is not working:
RewriteEngine On
RewriteCond %{REQUEST_URI} !^/es/empresa/($|/)
RewriteRule / http://www.example.com/ [R=301,L]
You can use these 2 redirect rules:
RewriteEngine On
RewriteRule ^es/empresa(/.*)?$ /nosotros$1 [R=301,NC,L]
RewriteRule ^es(/.*)?$ / [R=301,NC,L]

htaccess, www redirect fake subdirectory

Common problem, but too complicated for me.
Here are the requirements I am trying to meet:
The root URL http://example.com should be redirected to http://www.example.com
All URLs like http://example.com/c/1234567890 should be redirected to http://www.example.com/c/1234567890 (notice the "c" fake subdirectory)
When entered http://example.com/index.php one should be redirected to http://www.example.com (no trailing slashes)
On top of that I'm trying but failing to secure a subfolder "xy" from direct access exept from php files in root and javascript files.
I searched a lot and tried a lot of rewrite conditions and rules, but htaccess+regex is just from another planet for me. :/
Sorry if this is a duplicate…
These would be in the htaccess file in your document root. Turn on the rewrite engine
RewriteEngine On
For 1 and 2: redirect to "www" if root request or request for /c/(something)
RewriteCond %{HTTP_HOST} ^example\.com$ [NC]
RewriteRule ^(c/.+)?$ http://www.example.com%{REQUEST_URI} [L,R=301]
For 3: redirect direct requests for /index.php to just /
RewriteCond %{THE_REQUEST} ^[A-Z]{3,9}\ /index\.php
RewriteRule ^ / [L,R=301]
For 4: forbid direct access to anything in the /xy/ directory.
RewriteCond %{HTTP_REFERER} !http://(www\.)?example\.com/ [NC]
RewriteRule ^xy/ - [L,F]

Redirect specific page with query using htaccess

for example
mydomain.com/jobresultpage?what=&where=Arkansas
redirect to
yourdomain.com/jobresultpage?what=&where=Arkansas
but other page like
mydomain.com/about.html
mydomain.com/contact.html
not redirect..
Try adding the rules below to the .htaccess file located in the root directory of mydomain.com
RewriteEngine On
RewriteBase /
RewriteCond %{HTTP_HOST} mydomain\.com$ [NC]
#redirect any request for jobresult to yourdomain
RewriteRule ^jobresultpage$ http://yourdomain.com%{REQUEST_URI} [NC,L,R=301]

.htaccess subdomain redirect

My server is somehow configured to accept any string as a existing subdomian. This subdomains doesn't redirect, they show content instead. For example:
bla.example.com // shows the content of http://example.com
blabla.example.com // show the content of http://example.com
lorem.example.com/events/ // shows the content of http://example.com/events
So you can use "stackoverflow.example.com" and it will work.
I want to create an .htaccess rule to overwrite this behavior. I want to redirect http://*.example.com/* to http://www.example.com/*
Everything I have done so far is redirect http://example.com to http://www.example.com (wich is another behavior I want to keep)
RewriteCond %{HTTP_HOST} ^example\.com$
RewriteRule (.*) http://www.example.com/$1 [R=301,L]
Thank you for your help!
Try adding the following to your htaccess file in the root folder of your domain
RewriteEngine on
RewriteBase /
#if its not www.example.com
RewriteCond %{HTTP_HOST} !^www\.example\.com$ [NC]
#redirect it to www.example.com
RewriteRule .* http://www.example.com%{REQUEST_URI} [R=301,L]

Resources