Write an .htaccess file without %{REQUEST_FILENAME} - .htaccess

So I have a ridiculous problem that the hosting company cannot seem to take care of by itself. I have a website on a windows server that has ISAPI Rewrite 3 running to allow me to use .htaccess files for wordpress. But I noticed that the
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
statements are not working. They are just ignored. Is this a configuration issue? In the mean time I have the site up and running with this clever line that I stole from somewhere
RewriteCond %{REQUEST_URI} (/[^.]*|\.(html?|php))$ [NC]
The problem is that using this line I cannot access an actual directory that exists. Ex. /wp-admin/ so if I want to make changes in the admin area I have to enable that line that restricts by IP make the changes and then turn off that line. Utterly ridiculous.
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_URI} (/[^.]*|\.(html?|php))$ [NC]
#disallow or allow just my IP address
#RewriteCond %{REMOTE_HOST} !1.2.3.4
RewriteRule .* /index.php [L]
Any solutions so that I can still access directories that exist without using the %{REQUEST_FILENAME}?

Well .. I have Helicon ISAPI_Rewrite v3 running on IIS 6 (and had it on IIS 7 2 years ago) .. and these instructions actually work fine:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
It may not work that well if part of the URL is virtual folder / application .. but it works fine with real files and folders. Try these instead (should work exactly the same if no virtual stuff involved):
RewriteCond %{DOCUMENT_ROOT}%{REQUEST_URI} !-f
RewriteCond %{DOCUMENT_ROOT}%{REQUEST_URI} !-d

Related

Simple mod_rewrite rules broken after new c9 version

I'm working on a project hosted on the Cloud9 IDE. I had a simple set of mod_rewrite rules set up, but they no longer work after c9's new version rollout. It took me forever to iron out these rules (I'm a novice at best at this) I'm confused as to why these rules no longer work (AFAIK, the new c9 version should not have affected mod_rewrite rules).
Here are the rules (located in the root .htaccess)
RewriteEngine on
Options FollowSymLinks
RewriteBase /
RewriteCond %{REQUEST_URI} ^/css/.*$ [OR]
RewriteCond %{REQUEST_URI} ^/img/.*$ [OR]
RewriteCond %{REQUEST_URI} ^/js/.*$
RewriteCond Astralis/resources%{REQUEST_URI} -f
RewriteRule ^(.+)$ Astralis/resources/$1 [L,QSA]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [L]
The goal is pretty straightforward... All requests going to /css/... /img/... or /js/... should serve up the associated file within Astralis/resources (after checking that the file exists). Otherwise, redirect the rest of the traffic to index.php.
The problem I am experiencing is that all requests to resources (css, img, js) are returning 404's. If I move the /css, /img, and /js folders from within Astralis/resources back to the root directory, all the resources load properly. This problem started happening after c9's new version, with no changes to the .htaccess file, the codebase, or directory structure.
Any clue as to what is going on? How do I debug this kind of stuff? Any general tips/tricks for writing mod_rewrite rules would also be appreciated. Thanks.
Since Apache needs full path of the file in order to return true using -f you will need to use %{DOCUMENT_ROOT}/ before your file path.
Have it this way:
RewriteEngine on
Options FollowSymLinks
RewriteBase /
RewriteCond %{REQUEST_URI} ^/(css|img|js)/ [NC]
RewriteCond %{DOCUMENT_ROOT}/Astralis/resources%{REQUEST_URI} -f
RewriteRule ^(.+)$ Astralis/resources/$1 [L,QSA]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [L]

Multisites with one host using htaccess. Need to create an images folder rewrite

I have one hosting account that I am running using to run multiple sites, masterhost.com. Under masterhost.com I have site1.com, site2.com, etc... and am using this piece of code in the masterhost directory to reroute the domain request from the masterhost directory to the site1 directory.
RewriteCond %{HTTP_HOST} ^(www.)?site1.com$
RewriteCond %{REQUEST_URI} !^/site1/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /site1/$1/
RewriteCond %{HTTP_HOST} ^(www.)?site1.com$
RewriteRule ^(/)?$ site1/index.php
However Joomla on site1.com is having some issues with with this, and is routing the default images path back to masterhost.com/images.
Is there a rewrite I could use that would recognize any request http://www.site1.com/images and write it to http://www.masterhost.com/site1/images?
I believe the problem is these two lines:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
Basically, if the image exists on mainsite.com, these rules will prevent the request from being redirected to site1. So, for example, if you have mainsite.com/img/logo.png and site1.com/img/logo.png, site1.com would show the logo for mainsite.com.
Also, with this rule:
RewriteRule ^(.*)$ /site1/$1/
I don't believe the trailing slash should be there.
Finally, you didn't ask about it, but the last rule probably also doesn't work? If it does, removing the !-f and !-d checks above will break it. This would be because if a user requests site1.com/, the first rule redirects that to /site1/, which will never match the rule ^(/)?$. So, that rule needs to be changed to:
RewriteRule ^site1(/)?$ site1/index.php
You may or may not need a slash between ^ and site1 in that rule, depending on your RewriteBase directive.

.htaccess. Redirect requests to ~username folder

Situation:
I'm moving a website from a production environment to a test environment.
The test environment url is similar to http://192.168.1.100/~username/
There are thousands of files which use the following within the html
<img src='/images/image.jpg' />
Since the request is going to root http://192.168.1.100/ the files are 404.
Rather than finding and replacing all of html I'd assume that there is an easy way to fix it with mod_rewrite via .htaccess.
I've tried using the following
RewriteCond %{REQUEST_URI} !^/~username/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /~username/$1
But did not work as expected.
Thanks in advance.
UPDATE
The development environment resides within cpanel/whm. So when the username is removed from the requested url, it now belongs to the root users. So, my question now: How do I update the .htaccess file for the root user to mod_rewrite back to the ~username?
If you remove
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
it appears to work as expected, because any request to the right url will not be rewritten.
you might want to add [L] as a flag to signify it's the last rewrite rule, like so:
RewriteCond %{REQUEST_URI} !^/~username/
RewriteRule ^(.*)$ /~username/$1 [L]

Do I need to call for .htaccess?

I have no experience with .htaccess, but I got a tip that it's very useful so I wanted to try this.
I now have a file called .htaccess, in my root folder.
The files contains this;
RewriteBase /
RewriteEngine on
RewriteCond %{HTTP_HOST} ^kellyvuijst\.nl [nc]
RewriteRule (.*) http://www.kellyvuijst.nl/$1 [R=301,L]
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}\.html -f
RewriteRule ^([^/]+)/$ $1.html
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !(\.[a-zA-Z0-9]{1,5}|/)$
RewriteRule (.*)$ /$1/ [R=301,L]
What I'm trying to do here is create a 'www.mysite.com/portfolio/' instead of 'mysite.com/portfolio.html' I used some tutorials on this and I think it's correct, but I'm not sure.
So now I have this file, and what now? The tutorials all show what to put in the file but not what to do with it? Do I need to call for it in every .html page I have? And how do I call for it?
A .htaccess file is automatically invoked by the server.
You have just to put this into your file :
RewriteBase /
RewriteEngine on
RewriteRule www.mysite.com/portfolio/ /mysite.com/portfolio.html [L]
Hmm, you're using a lot of rules here to achieve just that.
Anyway, no you don't have to include that file. If you're hosting your site on a server with Apache it'll be included automatically. Can you also run PHP files or is your site just HTML? That's always an easy sign if you're also using Apache (not 100%, but often the go together).
If so, you could try just using these rules first:
RewriteEngine on
RewriteCond %{HTTP_HOST} !^www\.(.+)\.(.+)$ [nc]
RewriteRule ^(.*)$ http://www.%1.%2/$1 [R=301,L]
If that always adds www to your address, even if you type in the URL without www at least you can be certain that it works.
Then, to make the .html disappear you can add this rule:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule $(.*)/$ /$1.html [L]
This should make every url that ends with a slash (like portfolio/) use a .html file instead (portfolio.html), but only if /portfolio/ isn't an actual directory on your website.
(I removed your url from the rules because this way it should also work if you use it on another website, or if you change your url. It should still do what you want)
Made sure the server is configured to allow htaccess files to override host options. So in your vhost/server config, you need:
AllowOverride All

merging two folder with mod-rewrite in .htaccess

I'm currently developing a drupal based website but will have to keep a legacy system and many static pages/folders alive. It would be great if i could keep the old rubble in a separate folder than the new site. This would mean I've to merge both folders on the fly. I thought that this folder structure would be great.
/htdocs/legacy (symlink to old web root)
/htdocs/index.php
/htdocs/.htaccess
/htdocs/other drupal files and folders
/htdocs/...
This would mean that if i.e. mydomain.com/xyz.php is accessed the server should try to serve it in the following order.
if file or folder in /htdocs/ server this
if file or folder in /legacy/ serve this but do not rewrite the browsers location bar.
else rewrite pass it as querystring to the
I came up with the following rewrite rules. Which however don't work. I can either serve files in legacy or via drupal.
# 1. server from .htaccess folder
RewriteCond %{REQUEST_FILENAME} -f [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^(.*)$ - [NC,L]
# 2. serve from legacy
RewriteCond /full-path-to-legacy-folder/%{REQUEST_URI} -f [OR]
RewriteCond /full-path-to-legacy-folder/%{REQUEST_URI} -d
RewriteRule ^(.*)$ /legacy/$1 [NC,L]
# 3. else
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_URI} !=/favicon.ico
RewriteRule ^(.*)$ index.php?q=$1 [L,QSA]
Has anyone of you suggestions?
Many thanks!
Since you're just rewriting the URL, and there are other operations that need to be done with the file (like mod_php), try changing your [L] options to [PT] to allow regular processing to continue on the request.

Resources