Apache DirectorySlash seems not to be working - apache

I am facing problems with a site that I am trying to deploy. The site is working fine on my development host, but fails on deployment. Both hosts have a very similar configuration, both are Ubuntu Linux distros (dev: 11.10, deploy: 10.04), both use apache2, etc.
Both sites have mod_dir enabled, but the deployment one seems not to add the trailing slash to directory names, while the development one does. So, when I enter this URL (removed http to avoid silly stackoverflow antispam filtering):
devel.mydomain.com/admin
The development host redirects it to:
devel.mydomain.com/admin/
In deployment http://mydomain.com/admin is not redirected to mydomain.com/admin/ for unknown reasons, and I end with a 404 error. Of course if I enter mydomain.com/admin/, adding the trailing slash by hand, it works as expected. But I rather like to redirect also mydomain.com/admin to mydomain.com/admin/
The question is WHY in devel mod_dir seems to be doing the redirection and in deployment it does not.
I have done a grep within configuration files to see if the DirectorySlash directive was being disabled somewhere and found nothing. It is neither enabled explicitly on devel, so I think it should be on by default. Anyway I add this to my .htaccess file on deployment host:
DirectorySlash on
But it did not work neither.
Any hints?

The /admin folder is password protected. I did not think that this was relevant, but in fact it is.
Also I have these rules on my .htaccess file:
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-l
RewriteRule .* index.php [L]
</IfModule>
The problem is related to this - see here for a good explanation.
Basically, when Apache generates a 401 error to ask the browser for a password, it also generates another request that does not use any folder, directory, or symlink, so it is redirected to index.php.
The workaround is to add this to the .htaccess file on site root folder:
ErrorDocument 401 /error/null.html
ErrorDocument 403 /error/null.html
and create that file, for example like this:
touch ./error/null.html
These are not best practices but they'll do for now while I look for a better solution.

Related

.htaccess RewriteCond %{REQUEST_FILENAME}

I have a folder in my web root called docs and I need to ensure that files in this directory cannot be accessed by non authenticated users.
I have a file in this directory called index.php that verifies whether the user is logged in and serves up the requested file accordingly.
So in order to catch requests for files, in docs I have created the following .htaccess file
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !index.php
RewriteRule ^(.*)$ index.php?requested_file=$1 [QSA,L]
</IfModule>
This works if I navigate to /docs/index.php then index.php is called. However if I navigate to /docs/cat.jpg then I am sent directly to cat.jpg. But I shouldn't be able to access this file...
Can anyone advise what I am doing wrong here please?
Well I tried your exact code on my Apache based stack and it works fine
So There is nothing wrong with your apache instructions
May be you are using php or IDE's inbuilt live servers. For that to work you need apache based stack like Lampp, Wampp, Xampp or Mampp.

mod_rewrite remapping folder

I've currently updated our site, and the image folder name has changed from /img/ to /images/.
I'm still getting 404 errors in my apache error log from bots etc trying to access the old /img folder.
I'm trying to write a mod_rewrite rule to redirect any attempts to access /img/ to refer to /images/.
This is what I've got so far:
RewriteRule ^img/?(.*)$ images/$1 [R=301,L]
However, whenever I access http://mysite.com/img I still get my 404 page (instead of a forbidden page which I should receive for accessing /images).
Is this correct? I do have another rule forcing use of ssl if that matters.
Many thanks
This rule should be place on your root folder:
Options +FollowSymLinks -MultiViews
RewriteEngine On
RewriteBase /
RewriteCond %{DOCUMENT_ROOT}/images/$1 -d [OR]
RewriteCond %{DOCUMENT_ROOT}/images/$1 -f
RewriteRule ^img/(.*) /images/$1 [R=302,NC,L]
This rule will redirect only existent files or folders existent on http://domain.com/images.
Keep in mind that you may have been cached from previous attempts since you're using R=301, so to make sure its working try using a different browser.
Note that I am using R=302, to avoid this caching, once you confirm it is working, change it to R=301.

Apache Rewrite: favicon in %{HTTP_HOST} directory

My software supports multiple domains pointed at the exact same directory, I use shared hosting. I need to have each domain's favicon load from directories with their respective host names. Here is a visual...
http://www.example1.com/favicon.ico
public_html/www.example1.com/favicon.ico
\
http://www.example2.com/favicon.ico
public_html/www.example2.com/favicon.ico
\
http://www.example3.com/favicon.ico
public_html/www.example3.com/favicon.ico
I've tried some rewrites along the lines like this without any success...
RewriteEngine on
RewriteRule ^favicon\.ico$ %{HTTP_HOST}/favicon\.ico
Things to keep in mind...
1.) I use shared hosting so remember that the answer I need should be short and simple.
2.) I will only accept a DYNAMIC answer, I will only use the %{HTTP_HOST} variable and NOT a static domain name as I will not be manually editing my .htaccess file every single time I add a domain name.
3.) I may end up putting a .htaccess file in those sub-directories though I do not at the moment, an exception for the favicon would be greatly appreciated though is not necessary for me to accept the answer.
4.) I'll be more than happy to make any clarifications.
Use this code:
Options +FollowSymLinks -MultiViews
# Turn mod_rewrite on
RewriteEngine On
RewriteRule ^(favicon\.ico)$ %{HTTP_HOST}/$1 [L,NC]
I have been struggling with this issue too but I finally fixed it using the following rule:
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteRule ^/favicon\.ico$ /sites/all/themes/mytheme/favicon.ico
</IfModule>
I stuffed this into a virtual host declaration. You can do this for each of your virtual hosts, all you need to do is point the second part to the correct icon!
This solves all of my favicon problems, even for Firefox :)
(Tested on FF25, Safari 6.1, IE8 and IE10)
This one worked better in my case
RewriteCond $0 !=images/favicon.ico
RewriteRule ^([^/]+/)*favicon\.ico$ /images/favicon.ico [L,NC]
To support all possible browsers and platforms, in addition to favicon.ico file, need to have files such as android-chrome-192x192.png, apple-touch-icon.png, favicon-32x32.png, etc...
Here is rewrite rule to support them all:
RewriteRule ^(favicon.*\.(ico|png)|apple-touch-icon.*\.png|android-chrome.*\.png|mstile.*\.png|safari-pinned-tab.*\.svg)$ /favicons/%{HTTP_HOST}/$1 [L,NC]
This will serve favicons including Apple Touch, Android Chrome, Windows and other favicons from /favicons/<DOMAIN_NAME> folder.
I had a problem with favicon in files on subdomain
I was struggling with redirect for favicon in htaccess for only one subdomain for a long time.
My case was that all domain take favicon from public/ directory. One subdomain (let's call it 'subdomain_a') is configured to take it from another directory and it works.
Problem appeared when a file was opened on subdomain_a. The favicon in file view (f.e. pdf-viewer) was taken from public/ directory, not from configuration of subdomain_a.
Here is my solution:
# Redirect for favicon
RewriteCond %{HTTP_HOST} ^www.subdomain_a.com
RewriteCond %{REQUEST_URI} ^/favicon.ico$
RewriteRule ^(.*)$ /path/to/favicon/for/new/domain/$1 [R=301,L]

Why is my .htaccess file redirecting to full server path instead of relative path?

I've never had a problem with cakePHP before, but something's odd about this server and is causing the redirects in the .htaccess files to behave oddly.
CakePHP uses mod_rewrite in .htaccess files to redirect requests to its own webroot folder. The problem is that the redirects are listing the wrong path and causing a 404 error. My CakePHP application, which is stored in the listings directory, has a .htaccess file as follows:
<IfModule mod_rewrite.c>
RewriteEngine on
RewriteRule ^$ app/webroot/ [R=301,L]
RewriteRule (.*) app/webroot/$1 [R=301,L]
</IfModule>
(*note that the R=301 causes an external redirect so we can see what is going on from our end. It should really omit this flag and do the redirect internally, transparent to end-users)
This is supposed to redirect any request from http://hostname.com/~username/listings/ to http://hostname.com/~username/listings/app/webroot/
However, rather than simply adding “app/webroot/” to the end as it is supposed to, it is adding the full server path ( /home/username/public_html/listings/app/webroot/ ) resulting in the final URL http://hostname.com/home/username/public_html/listings/app/webroot/ which is obviously incorrect and triggers a 404 error.
The hosting is on a shared hosting account, so that limits what I can do with the settings. I've never seen this happen before, and I'm thinking it's something wrong from the hosting side of things, but if anyone has some helpful suggestions then I can put them to the hosting company as well.
The solution to your question can be found towards the bottom of this page in the cakephp book:
For many hosting services (GoDaddy, 1and1), your web server is actually being served from a user directory that already uses mod_rewrite. If you are installing CakePHP into a user directory (http://example.com/~username/cakephp/), or any other URL structure that already utilizes mod_rewrite, you'll need to add RewriteBase statements to the .htaccess files CakePHP uses (/.htaccess, /app/.htaccess, /app/webroot/.htaccess).
I've deployed CakePHP from my profile's public_html folder as well. I had to change 3 the same .htaccess files mentioned above. Just add RewriteBase /~username/ to the .htaccess files just after RewriteEngine on!
Try removing .htaccess from main file... It worked for me
It was quite simple (using uolhost shared host):
Edit both .htaccess files:
/webroot/.htaccess
/.htaccess
Add the following line:
RewriteBase /
Here is the whole /webroot/.htaccess file:
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [L]

mod-rewrite question: /test/method is rewritten to test.svg/method

I noticed an odd (to me) mod_rewrite thing happening. Fixing it is not important to me so much as figuring out what's going on. Basically, I have an svg file called test.svg in my document root, as well as an index.php. My expectation, based on my .htaccess file is that visiting http://localhost/test.svg would get me the .svg file (and it does), while visiting http://localhost/test/action would be rewritten to index.php/test/action. Instead, the latter is apparently rewritten to test.svg/action, as I receive the message
The requested URL /test.svg/action was not found on this server.
Here is my .htaccess file:
# Turn on URL rewriting
RewriteEngine On
# Protect application and system files from being viewed
# RewriteRule ^(application|modules|system) - [F,L]
# Allow any files or directories that exist to be displayed directly
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
# Rewrite all other URLs to index.php/URL
RewriteRule .* index.php/$0 [PT,L]
I am using a Apache 2.2.12 on Ubuntu (installed via apt-get). I think my setup is fairly standard, but I'm not sure exactly what directives or config files would be relevant. I am by no means a sysadmin of any kind, I just use this server to test and develop things locally.
As I said, fixing this issue would be trivial, I just am often confounded by mod_rewrite and would like to understand what's going on here.
Apache's HTTP content negotiation feature is automatically translating from "/test" to "/test.svg". See http://httpd.apache.org/docs/2.0/content-negotiation.html#multiviews
You can disable content-negotiation in .htaccess with the directive:
Options -MultiViews
You can get more information about what mod_rewrite is doing by adding these directives to your Apache configuration (they won't work in .htaccess):
RewriteLog /path/to/rewrite.log
RewriteLogLevel 3
The RewriteLogLevel can be any number from 0 (disabled) to 9 (extremely verbose). 3 should give you enough to see what's going on, but don't use that on a production server.