Htaccess - Disallow going backwards? - .htaccess

is there a way to disallow to go backwards to another directory?
E.g.: The URL is http://www.example.com/subdirectory/
If there is a HTML file which links to the the URL "/file.txt" it should not leave the subdirectory. The slash has to be ignored. Are there some Rewrite Rules in htaccess to do this? The subdirectory should be handled like a root directory.

There is a similar question here. I believe it is what you are looking for:
.htaccess. deny root, allow specific subfolder. Possible?

Related

htaccess rewrite not working beyond first directory

I have had to change the name of a page on my wordpress site. In the url structure the page has many sub pages which are illustrated as follows:
www.mysite.com/folder1/plus whatever else may come here
Basically I want to do a wildcard redirect where any URL matching the folder1 part of the URL gets caught and redirected to the following:
www.mysite.com/newfolder1/folder2/
The following redirect works for the above situation
RewriteRule ^folder1/?(.*) http://www.mysite.com/newfolder1/$1 [R=301,L]
But fails and give a 404 for the following
www.mysite.com/folder1/folder2/folder3
It will not redirect beyond the folder2 depth. Why is this?
Following up on the comments:
The problem sounds like the .htaccess file (or the rules you're talking about) is in the wrong location.
As far as my understanding goes, Apache reads all the .htaccess files (and other server configs that do redirects) from the "top" down - i.e., .htaccess files in subfolders are processed after .htaccess files in the root folder.

Forcing www for part of my site

I'm trying to set up .htaccess to force www for one folder of my site, but not the rest of it. Does anyone know how to do this?
Use the solution from here:
.htaccess - how to force "www." in a generic way?
but put the .htaccess in the directory you want to force the www subdomain in. .htaccess files are only evaluated if they're in a directory that is in the directory where the file you're serving resides or any of its parent directories, so any other directories would be unaffected.
You should, however, probably not do that to begin with. Forcing the "www" subdomain is annoying enough as it is - and domains look sexier without that ;) - but only doing it for some directories is really going to confuse people.

mod_rewrite: rewriting existing directory with htaccess in it

how can I tell mod_rewrite that ALL existing directories should be rewritten?
I have an subdirectory with a .htaccess-File which rewrites too. When I go to domian.tld/sub_with_htaccess, Apache only reads the htaccess in "sub_with_htaccess" but do not read the .htaccess in / which is more important for me.
BUT: When I have a subdomain (sub.domain.tld) whose document root points on /var/www/domain-tld/sub_with_htaccess, I want that only the .htaccess in "sub_with_htaccess" decides what to do.
Any ideas? :)
You can try adding the RewriteOption directive in the htaccess file in the sub_with_htaccess directory:
RewriteOption Inherit
This will make it so rules from parent directories get appended after the rules in this htaccess file. If you are using apache 2.4, you can also use the InheritBefore option which will place the parent htaccess' mod_rewrite rules before the rules in this htaccess file.

Multiple htaccess files with different rewrite rules

I have 2 .htaccess files - one in root directory, another in subdirectory:
accordingly docroot/ and /subdirectory/docroot (this one works like a separate account).
The problem is that there are different rewrite rules in both files. Basicly, the problem is that the .htaccess in subdirectory doesn't work and/or is ignored.
What I am trying to achieve is to have one set of rewrite rules for docroot and other set of rewrite rules for subdirectory/docroot
edit:
the .htaccess file in subdirectory/docroot basicly strips index.php from url, and it actually works, but when i go to subdirectory/user it redirects to subdirectory/user/login (instead of subdirectory/index.php/user/login -- this parts is ok) but the website shows the root page (not subdirectory, but domain root)
Any ideas?
I just had to set RewriteBase to that particular subdirectory to make things work.

can i use robots.txt while handling my site with htaccess

I am using htaccess in my site, such that all the request to my site will be redirected to index page in my root directory. No other file in my site can be accessed because my htaccess will restrict it. My doubt is, when I use robots.txt file, will the search engines be able to reach the robots.txt file in my domain?. Or must i modify my htaccess file to allow the search engines to read the robots.txt file. If so help me in finding that specific code for htaccess.
I suppose you're using some sort of rewriting. You can exclude files from beeing processed by mod_rewrite with the following rule:
RewriteCond %{REQUEST_FILENAME} -f
RewriteRule ^(.*) $1 [L]
In this case all files that do exist in the webroot and are called directly will not be processed any further. Replace _%{REQUEST_FILENAME} with robots.txt and you should be fine.
Best wishes,
Fabian
How about trying if you can yourself access the robots.txt via a web browser? If you can, then the search engines can, an vice versa (unless you're using some IP- or browser-specific redirections or such). That is, http://yoursite.com/robots.txt

Resources