Exclude certain directory from RewriteEngine on a Server running TYPO3 - .htaccess

I want to develop a new website for a customer who is currently running a TYPO3 website on his server.
I'd like to exclude a subdirectory from the TYPO3 RewriteEngine (realurl?) so I have normal access to that subdirectory.
www.domain.com/dev
for example.
I have access to that site but as soon as I put a .htaccess file in that certain directory to protect it with a htpasswd. I get a TYPO3 Error:
Reason: Segment "dev" was not a keyword for a postVarSet as expected!

In .htaccess find this
# Stop rewrite processing, if we are in the typo3/ directory or any other known directory
# NOTE: Add your additional local storages here
RewriteRule (?:typo3/|fileadmin/|typo3conf/|typo3temp/|uploads/|favicon\.ico) - [L]
and add your directory there
# Stop rewrite processing, if we are in the typo3/ directory or any other known directory
# NOTE: Add your additional local storages here
RewriteRule (?:typo3/|fileadmin/|typo3conf/|typo3temp/|uploads/|favicon\.ico|dev/) - [L]

I found the solution and it was anything but straightforward.
I needed to add this line to my .htaccess in the protected subdirectory:
ErrorDocument 401 "Not authorized"
Apparently there is a conflict between URL rewriting and Basic Auths in cases just like mine. The Server writes a "401 Unauthorized" Header and looks for an existing error document based on a pre-defined path which then gets rewritten.

Related

.htaccess rewrite with subdirectory

I want to rewrite (not redirect) all url's of my site to a sub-directory of my site. For example:
http://example.com/example
would load the following:
http://example.com/public/example
Though, requesting http://example.com/public should not load the contents from public/public but from /.
Answers I've found on SO either do the above with redirect (which I don't want) or doesn't account for the special case above.
EDIT: further clarification:
I want every request on my site to go load under the public folder, but without being visible to the visitor. So requesting http://example.com/index.php will load the file from http://example.com/public/index.php. The url in the browser remains unchanged for the user.
Try the following rule :
RewriteEngine on
RewriteRule ^((?!public).+)$ /public/$1 [NC,L]
This will rewrite all requests from root to /public dir.

Rewrite rule to redirect to file and not folder with same name

I am working on a new website and I want the following url /newsite/products to redirect to /newsite/products/product.php.
My .htaccess file is as follows:
RewriteEngine On
RewriteRule ^newsite/products$ /newsite/products/product.php
My problem is that there is already a folder called products within newsite and when requesting newsite/products it tries to open that folder rather than go to the php file.
I get:
Forbidden
You don't have permission to access /newsite/products/ on this server.
Is there any way to make this work?
EDIT: Requested url is: http://example.com/newsite/products and location of .htaccess is in root.
You need to turn off the directory slash to rewrite your file :
Try :
DirectorySlash off
RewriteEngine On
RewriteRule ^newsite/products$ /newsite/products/product.php [NC,L]
Don't add a trailing slash in rule pattern
/newsite/products
will rewrite to
/newsite/products/product.php
and
/newsite/products/
will open your "products" directory
Note- this is not a safe solution, if you remove the rule and DirectorySlash is off, Your index file will be ignored ,all directories will start listing files and folders.

.htaccess force 404 and prevent directory listing

Is it possible to force a 404 error if someone tries to access any directory in my root directory (/var/www) provided the directory name contains .git?
I.e if someone tried to access http://mysite.com/site.git, it would show a 404 page?
Furthermore, how could I prevent directory listing in the same file?
I am using Apache/2.2.22 (Ubuntu).
Thanks,
Max.
Using mod_rewrite, you can put these rules in the htaccess file in your document root:
RewriteEngine On
RewriteRule \.git$ - [R=404,L]
Or, you can replace the brackets with a [F,L] to return a 403 Forbidden instead.

How to set the default website page through htaccess?

I am currently setting up a website from a client on his hosting account. The website address and for some reason doesn't default to .php files (that is: index.php). If I put index.php and there is no index.html file I receive the following error:
If you feel you have reached this page in error, please contact the
web site owner: someemail#example.com If you are the web site owner,
it is possible you have reached this page because: The IP address has
changed. There has been a server misconfiguration. The site may have
been moved to a different server. If you are the owner of this website
and were not expecting to see this page, please contact your hosting
provider.
His hosting is a shared hosting on cpanel.
Use the DirectoryIndex directive in your .htaccess file
DirectoryIndex index.php
The problem was caused by my web-browser cache, as soon as I have cleared my cache it worked!
Try
DirectoryIndex index.php
if that doesn't work you might have to work around it by providing index.html like
<html><frameset rows="*"><frame src="index.php"></frameset></html>
(ofcourse more elaborate)
You can set your default page as the directory index using .htaccess file in 2 ways.
First method
DirectoryIndex yourfile.php
Above code will set 'yourfile.php' as the directory index. But this will applies to all sub folders as well and may occur '404' error for some related urls.
Second method
Use this code to set your handler file for the root directory
RewriteEngine on
RewriteRule ^$ /yourfile.php [L]
If you want to set a handler file for the sub folder, use this one
RewriteEngine on
RewriteRule ^subfolder/$ /yourfile.php [L]
enter image description here
im getting this error...
enter image description here
i found this line in index.php
how to remove this defualt page and make my website live

simple .htaccess rewrite URL to different directory on same server

Ok, I'm clueless here...
I need to rewrite a directory structure and all sub-directories within it to a directory within the same server, but a root that is before the directory.
For example:
http://www.mydomain.com/Themes/default/css/folder
and all directories called upon after folder. Such as folder/sub_folder or folder/afolder/anotherfolder, it needs to include ALL sub-directories within the folder directory.
should be redirected to this:
http://www.mydomain.com
How do I do this via a .htaccess file within the folder path http://www.mydomain.com/Themes/default/css/folder?
Please someone help.
Thanks guys :)
The files within the directory structure still need to be accessible for that structure when called via PHP, but I don't want people being able to browse to http://www.mydomain.com/Themes/default/css/folder and be shown all subdirectories within that folderpath and/or all files. Same thing for all sub-directories that follow that folder path.
I'd like to be able to place the .htaccess file within the http://www.mydomain.com/Themes/default/css/folder directory on the server, but don't know exactly what code to use for this.
ALSO, even more challenging... The domain name can change, so I'd rather not use the domain name within the .htaccess file, instead perhaps use .. or . to go up a directory or a different method of grabbing the domain name within the .htaccess file.
Create a .htaccess file in /Themes/default/css/folder and place these lines there (it requires mod_rewrite):
Options +FollowSymLinks -MultiViews
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^.*$ http://%{HTTP_HOST}/ [R=301,L]
It will redirect (301 Permanent Redirect) all requests to a folder to a homepage. If file is requested, it will allow it.
If you want to have it working for folders as well as files then remove the RewriteCond line -- it will redirect ALL requests (even for non-existing URLs) to a homepage.
If you will see "500 Internal Server Error" after creating such file, then it is your server configuration: mod_rewrite may not be enabled or it's directives (RewriteRule, RewriteCond, RewriteEngine) are not allowed to be placed in .htaccess. In any case -- check Apache's error log for exact error message (it will give you the exact reason).
http://www.besthostratings.com/articles/prevent-directory-listing.html
IndexIgnore *

Resources