.htaccess redirect sub directory access attempts back to root - apache

I wish to protect folders such as /css and /js from listing directory contents to snoopers. Perhaps I could do this via PHP, but I need to use .htaccess.
Desired:
Anyone browsing to http://example.com/css should not see a listing of files, but is immediately redirected back to http://example.com.
I have tried several variations on the following, but cannot seem to get it right:
.htaccess inside each private folder:
RewriteEngine On
Redirect 301 /. http://example.com
or, in the webroot:
RewriteEngine On
RewriteBase /
RewriteRule /(.*)$ /$1 [L,NC,R]
Note: I already have an .htaccess file in the webroot, which any additional mod_rewrite commands would need to accomodate:
RewriteEngine On
RewriteBase /
RewriteRule ^dev/(.*)$ /$1 [L,NC,R]
RewriteRule ^/(.*)$ /dev/$1 [L,NC]

You can create /css/.htacces with this line:
ErrorDocument 403 http://example.com/
Options -Indexes
Do same in /js/.htaccess file.

Related

.htaccess Redirect all html files to index.php

I have strange problem with .htaccess file.
The file is placed inside directory www.mydomain.com/t/ and looks like that:
Options +FollowSymlinks
RewriteEngine on
RewriteRule ^([^/\.]+).html$ ../index.php [R=301]
I would like to redirect all .html files to my main page www.mydomain.com
Redirection works but I am redirected to www.mydomain.com/server/file/path/t/index.php not to www.mydomain.com (or www.mydomain.com/index.php)
Where did I make a mistake?
Thank you in advance for help.
Add this rule
RewriteRule ^(.*)$ /index.php?$1 [L]

htaccess 301 redirection of urls w/ parameters to url w/o parameters

I would like to do a simple 301 direct on some old URLs that have parameters to them.
e.g.
/dir/page1/?id=4590
/dir/page1/?id=45
/dir2/page5/?id=4590
/dir2/page9/?id=45
/page90/?id=45etc
...
I want a 301 redirect of the above urls to the corresponding urls w/o parameters:
/dir/page1
/dir2/page5
/dir2/page9
/page90
My experience with .htaccess is limited. What would be the simplest and the most correct way to enable the above redirects to actually work.
Enable mod_rewrite and .htaccess through httpd.conf and then put this code in your .htaccess under DOCUMENT_ROOT directory:
Options +FollowSymLinks -MultiViews
# Turn mod_rewrite on
RewriteEngine On
RewriteBase /
RewriteCond %{QUERY_STRING} ^id= [NC]
RewriteRule ^ %{REQUEST_URI}? [R=301,L]

htaccess rewriterule to send a file to another file in the same directory

I have a directory that contains an index.php and an index.html file, both being published from a CMS. There's a specific IP address that will attempt to access index.html, but should instead be shown index.php in the same directory. All other traffic should act as normal.
I've been working with some variations of this code:
RewriteEngine On
RewriteCond %{REMOTE_HOST} ^123\.456\.789\.10
RewriteRule ^index\.(htm|html?)$ index.php [NC,R=301,L]
This does do the redirect, but of course it goes to the root of the site rather than staying in the same directory. It's somewhat unclear what the directory path will be in all cases, so I'd like to tell Apache to stay in the same directory it's in.
Is this possible?
Thanks,
Jonathan
If you want to have this rule for IP: 123.456.789.10 then you shouldn't have this IP with
negation like: RewriteCond %{REMOTE_HOST} !
Try this code:
Options +FollowSymLinks -MultiViews
# Turn mod_rewrite on
RewriteEngine On
RewriteBase /subdir/
RewriteCond %{REMOTE_HOST} ^123\.456\.789\.10$
RewriteRule ^index\.html?$ index.php [NC,R=302,L]

Rewrite requests to a directory (w/o a trailing slash) to index.php in .htaccess

I've seen similar requests for this but was not able to find a hard and fast answer that worked. Essentially I want to internally rewrite requests to a directory (where the requested URL does NOT contain a trailing slash) to the index.php contained in that directory:
http://example.com/foo => http://example.com/foo/index.php
Right now if a request is made to http://example.com/foo apache will do an externally visible 301 redirect to http://example.com/foo/, which in turn will result in the index.php being rendered, but I want to avoid this external redirect and extra request.
Enable mod_rewrite and .htaccess through httpd.conf and then put this code in your .htaccess under DOCUMENT_ROOT directory:
DirectorySlash On
Options +FollowSymLinks -MultiViews
# Turn mod_rewrite on
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule (?!^.+?/$)^(.+)$ /$1/index.php [L]

How to use mod_rewrite in a subdomain's .htaccess?

I have a domain (let the name be) mydomain.com.
I added a subdomain, sub.mydomain.com. (It's an other site, the two sites don't have anything to do with each other)
I have mapped the subdomain (with godaddy.com's tool) to /sub directory. It works well with static file paths, ex. http://sub.mydomain.com/js/script.js.php.
However, I would like to be able to use the rewrite module to get http://sub.mydomain.com/js/script.js
My .htaccess files:
.htaccess in the root directory:
RewriteEngine On
RewriteCond %{HTTP_HOST} ^sub.mydomain.com$
RewriteRule ^.*$ http://mydomain.com/sub%{REQUEST_URI} [L,P]
.htaccess in the /sub directory:
RewriteEngine on
RewriteRule ^js/script\.js$ js/script.js.php [L]
(I tried adding RewriteBase /sub/, I didn't succeed).
It seems as if Apache didn't notice the /sub's .htaccess
How can I get the rewrite work on the subdomain's paths?
SOLVED!
After a long, exhausting debugging and googling I found an Apache setting that made the trouble:
I added Options -MultiViews and voilá, it works!
/////////////////////////////////////////////
Now my configuration:
-no .htaccess in the root.
-.htaccess in the root/sub:
DirectoryIndex site/index.php
Options -MultiViews
RewriteEngine on
RewriteRule ^site/js/script.js$ /site/js/script.js.php [L]