RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule .* ./index.php
I think it means if the url doesn't match a file or directory on the server go to /index.php?
Can anybody confirm?
Yes is the short answer. But usually ./index.php has an [L,QSA] suffix/qualifier, so the query part of a URI request is also passed on to it.
What this is doing:
RewriteCond %{REQUEST_FILENAME} !-d
If it can't find a directory with the name
RewriteCond %{REQUEST_FILENAME} !-f
If it can't find a file with the name
RewriteRule .* ./index.php
Load the index.php next to the .htaccess file.
To answer Anthony's question regarding what would happen if it where removed1 2,
It would try and access the directory or file. If it fails, instead of defaulting to the index.php it would simply give the default web servers 404 error.
Right now, it allows the application to either show the default page, or handle the request through it in some fashion.
Related
OK so I've found every rendition of this but not this specifically. So my urls are in this format
www.inspection.com/users/?action=register
I want to convert it to
www.inspection.com/users.php?action=register
Here's what I've got so far in my htaccess
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^[A-Za-z0-9]+/\?action=[A-Za-z0-9]+$ $1.php?action=$2 [L,QSA]
But error logs say RewriteRule: cannot compile regular expression
Main Goal: After the domain name is the reference to the file I want to call. So append php on the end of that string but keep the query parameters
With your shown samples, attempts please try following .htaccess rules file. Please make sure to clear your browser cache before testing your URLs. Also make sure that your .htaccess rules file and .php file are residing on same folder.
RewriteEngine ON
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^([^/?]*)/?\?action=(.+)$ $1.php?action=$2 [NC,L,QSA]
One more thing, you have not created a capturing group on left side of RewriteRule hence you can't use them on the right side of it.
I've got an install of concrete5 living in my site root - the problem is that the C5 file structure is pretty varied, and it's getting mixed in with my subdirs and subdomains. I can't tell what's my code and what's C5, I hate it.
My solution is to move everything to a /_concrete/ folder - but since my domain has to point to the root, I can't use the files here.
Enter .htaccess: I need to write a script to redirect any instance of www.domain.com (ignore subdomains) to www.domain.com/_concrete/ - but not a transparent 301, just an alias. To clarify: www.domain.com/page/ and www.domain.com/_concrete/page/ should display the same thing. www.domain.com/page/ should NOT change it's URL to www.domain.com/_concrete/page/.
I've adapted this from a similar script I use elsewhere:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ /_concrete/$1 [L,QSA]
But this only causes a server error. I hope I'm close - can anyone point me in the right direction?
You're telling the server to check if the value is not a filename, but you're not having it ignore the _concrete directory itself (which would also match your script and create a loop).
This might get you closer to what you're trying to do.
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !^_concrete
RewriteRule ^(.*)$ _concrete/$1 [L,QSA]
Additionally you might want to have it ignore directories that exist as well. At which point _concrete would no longer be matched because it exists.
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ _concrete/$1 [L,QSA]
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]
The way my site is currently structured, the actual site is in its own separate folder. Here's what I mean:
/projects
/files
/pictures
/tools
/school
/~webroot
.htaccess
This makes the file-system much easier to manage and navigate. An easy way to utilize this, without having everyone navigate to http://domain.com/~webroot/, and still allow them to access files and such like http://domain.com/projects/, is to use the htaccess I wrote below to check for files in both the real root, and ~webroot directories.
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /~webroot/$1 [NC,QSA]
RewriteRule ^/?$ /~webroot/index.php [L,QSA]
However, if the file doesn't exist anywhere (root or ~webroot), an HTTP 500 error is thrown instead of an HTTP 404. In order to display my 404 error page, I have to instead use these lines:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{DOCUMENT_ROOT}/~webroot/$0 !-F
RewriteRule ^(.*)$ /404.shtml [B,L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !^/~webroot
RewriteRule ^(.*)$ /~webroot/$1 [NC,QSA]
RewriteRule ^/?$ /~webroot/index.php [L,QSA]
This is all quite messy, and it's only a trick and doesn't actually throw an HTTP 404, which keeps 404s from being documented using my statistics application. Is there a way to: Check if file exists in root, if not check if file exists in ~webroot, if not throw real HTTP 404 error?
I do also have all my ErrorDocument's defined properly.
I know this was a while ago, but wanted to chime in. For a rewrite rule to hit a 404, I always use this:
RewriteRule ^(.*)$ - [R=404,L]
I'm not sure if it's correct, but it works for me. I'm pretty sure it should always be with "L" to prevent further rewriting.
BTW, what is the "B" flag for? I only know the ones listed on the apache mod_rewrite page, so I'm lost on it's meaning.
http://httpd.apache.org/docs/2.0/mod/mod_rewrite.html
EDIT: ah, my problem is I look at older version docs. Thank you, Swivelgames. I probably would have never even known I was looking at the older docs if you hadn't pointed that out (need to update my bookmarks).
http://httpd.apache.org/docs/2.3/rewrite/flags.html#flag_b
If you can make your 404 page a PHP file, you can add this before any output to get real 404's:
<?php header("HTTP/1.1 404 Not Found"); ?>
This will cause PHP to trigger a 'real' 404 (from the browsers point of view) which gets passed back to the browser. If your statistics package is internal to your server this approach may or may not work, I'm not sure.
The only other solution I can think of is to redirect to a non-existent file:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !/non-existent-404-generator
RewriteCond %{DOCUMENT_ROOT}/~webroot/$0 !-F
RewriteRule ^(.*)$ /non-existent-404-generator [B,L]
A fairly basic htaccess question, but I don't use it much, so i have no idea.
I want to check if the requested file exists. If it does, forward to one page, and if not forward to another page and pass the requested path as GET.
Thanks in advance.
RewriteEngine On
# check if requested file or directory exists
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
# if not, pass it to index.php
RewriteRule ^(.*) index.php?page=$1 [QSA]
As Gumbo suggested, you can repeat the condition without the ! if you also want to rewrite the URL when a file exists. Maybe you want to 'protect' your real files and folders with this method.
Use the -f expression in a RewriteCond condition to test if the given path points to an existing regular file:
RewriteEngin on
# file exists
RewriteCond %{REQUEST_FILENAME} -f
RewriteRule ^ one-page [L]
# file does not exist
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ another-page [L]
The initial requested URI should then be available in an environment variable.