.htaccess file sub-folder redirect - apache

I know this has been posted in other places, but I have not been able to make it work properly. I am just trying to get a simple redirect from my root / to /html folder and add a .html extension so the url isn’t www.websites.com/html/file.html but rather www.website.com/file. Does anyone have any idea how to do this type of rewrite?
This is about all I have working properly.
RewriteOptions inherit
RewriteEngine on
<FilesMatch "\.(ico|pdf|flv|jpg|jpeg|png|gif|js|css|swf)#$">
Header set Cache-Control "max-age=604800, public"
</FilesMatch>
RewriteBase /html/

This probably is what you are looking for:
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond /html%{REQUEST_URI}.html -f
RequestRule ^ /html%{REQUEST_URI}.html [END]
For this to work the rewriting module needs to be loaded into the http server. Best is to implement such rules in the actual host configuration of the http server. If you do not have access to that (cheap hosting provider), then you can use a distributed configuration file (".htaccess"), but that needs to be enabled too.

Related

Apache FilesMatch directive to match requested domain

I'm trying to setup the FilesMatch directive in such a way that when the user requests a resource that matches the domain name, only then will the resource be served.
For example:
example.com/example.com.html = good
example.com/other.com.html = not allowed, serves error.html instead
I'm currently considering configuring Apache like so:
<FilesMatch regexp+domain_name+regexp>
allow the resource...
</FilesMatch>
But to achieve this effect I would need to have the domain of the request as a variable somehow.
Any advice appreciated.
This option doesn't only check if the file exists, but also if it matches your desired pattern:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f [OR]
RewriteCond %{REQUEST_FILENAME} ^%{SERVER_NAME}\.html$
RewriteRule ^ - [F]
There are more ways to do it, but rewriting gives you the most and probably also the most powerful options. It does require mod_rewrite to be activated though.

enabling RewriteEngine in apache (uniserver) causes 403 on all files

What I'm trying to do is to enable a 'dry' version of url rewriting in Apache only to check if it is working and actually do not rewrite anything yet.
The simplest .htaccess that I came up with is:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
But this still doesn't work. All files, all paths I'm trying to request, no matter if they exist or not return 403 Forbidden
What I need is any example htaccess that has redirection inside that must work, that is allow access to all existing files and folders and do it's job only when the file requested doesn't exit.
The problem was that I've not added these lines:
Options +SymLinksIfOwnerMatch
or
Options +FollowSymlinks

Apache rewrite rule to redirect all request to subdirectory containing another .htaccess and rewrite rules

I have public and private projects on my webserver. I put everything what is public into the webserver root, and I have a private folder there which I can only reach from local network (set by .htaccess in there).
I want to simply put every private projects in the private folder and handle the requests automatically, but want the URLs look like they are served from webroot.
For example if there is private/project1 I want to use the URL http://example.com/project1 to serve that folder and don't want to change the URL.
This simple rewrite:
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ private/$1
works, but when I have a private/project2 with another .htaccess:
Options +FollowSymLinks
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /project2/
<Files .*>
Order Deny,Allow
Deny From All
</Files>
# Allow asset folders through
RewriteRule ^(assets/.+) - [L]
# Protect files from being viewed
RewriteRule ^(uploads.+) - [F,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L]
</IfModule>
Options -Indexes
then the static content will appear, but the links are broken.
What should I modify to work ?
Also if I have a private/project3 and browse to http://example.com/project3/ there is no problem, but when I browse to http://example.com/project3 (without the trailing /) the URL will be visible as http://example.com/private/project3/ in the browser. Why ? How can I avoid that ?
All you need in your case is mod_alias.
Then serve your private project like:
Alias /project3 /apache/htdocs/private/project3
And with .htaccess you will control access rights.
If you want to control it without restarting server, you can try to achieve this with following config, that can be placed in .htaccess file:
RewriteRule ^/(project1)$ /private/$1/index.html
RewriteRule ^/(project1/)(.*)$ /private/$1$2
index.html - any index file for your project.
This way public part of URL's will be completly accessible, beside path's you are using for the private projects.
You also can add RewriteCond to check IP and enable rewriting only for your local network.
Actually, looking over your question it looks like this is an issue with mod_dir interferring with the path pipeline. Specifically, DirectorySlash which is by default turned on, will 301 redirect the browser when it thinks the browser is requesting a directory and is missing the trailing slash. You can try turning DirectorySlash Off but there's a security warning associated with it:
Turning off the trailing slash redirect may result in an information disclosure. Consider a situation where mod_autoindex is active (Options +Indexes) and DirectoryIndex is set to a valid resource (say, index.html) and there's no other special handler defined for that URL. In this case a request with a trailing slash would show the index.html file. But a request without trailing slash would list the directory contents.
That may or may not be applicable to your setup. You can also try modifying your rewrite rule to account for a trailing slash:
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)/$ private/$1/
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*[^/])$ private/$1/
But I've had mixed results trying to get mod_rewrite and mod_dir to always play nicely with each other.
Wouldn't virtual domains be enough?
You could use a domain for private projects and another for public projects.
http://httpd.apache.org/docs/2.2/vhosts/examples.html

Apache ignoring file extensions

I use the following .htaccess code to make my URLs cleaner:
RewriteEngine on
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?$1 [L]
It basically check if the requested URL points to a file or a directory and if it doesn't, it formats it in a particular way.
The problem is that my production server seems to ignore file extensions when it checks if the requested URL points to a file. For example, it would consider the URL /contact pointing to a file named contact.jpg if a file with that name existed on the root of the server.
What causes Apache to behave like that and what can I do to control it - make it strict about file extensions?
I believe it's because of MultiViews option.
Try Options -MultiViews in the .htaccess

How would I go about creating a mod_rewrite that redirects to launch.php?i=/the/url/that/they/want?

So if the user types mydomain.com/dashboard, the document the server actually sends them is /launch.php?i=/dashboard.
The one caveat is that I would like to leave requests for
/flags
/people
/posters
/css
/icons
/images
/libraries
/patterns
alone, and they should request the actual folder.
How would I create such a mod_rewrite?
This is the .htaccess file for the CakePHP Framework.
Please replace the index.php and ?url= to fit your needs.
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ index.php?url=$1 [QSA,L]
</IfModule>
The "!-d" tells Apache to follow existing folders and "!-f" to follow existing files.
Everything else is channelled through index.php
As suggested in a comment, you have to be aware that if it's not working it could be because mod_rewrite is not enabled and you'll not get an error stating that fact, you'll probably only have a HTTP 404.