Why am I getting a 404 error with this routing? - .htaccess

In my web root (var/www) I have the following in my .htaccess file:
RewriteCond %{REQUEST_URI} ^system.*
RewriteCond $1 !^(index\.php|vges|images|robots\.txt)
RewriteCond %{REQUEST_URI} ^application.*
RewriteRule ^(.*)$ /test/index.php?/$1 [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?/$1 [L]
I then have a subdirectory (/var/www/test) with a codeigniter project. It also has a .htaccess file with the following:
RewriteEngine On
RewriteCond $1 !^(index\.php|quiz|vges|buddy|css|fonts|img|images|js|robots\.txt)
RewriteRule ^(.*)$ /index.php/$1 [L]
When I visit localhost I can view the test directory and when I go to the test directory I'm displayed with the default controller view. But when I visit localhost/test/controller I get a 404 not found error:
The requested URL /index.php/events was not found on this server.
Edit: I found the solution by changing the last line /var/www/vges.htaccess to:
RewriteRule ^(.*)$ test/index.php/$1 [L]
Although I suspect if I was to upload this test project to a server, where it is not a subdirectory then I may get an error. What's the best solution to this? What should
I change in my .htaccess file so
I can have subdirectories on my local machine
When I upload them as individual projects (such as the test project) to a server I don't have to modify the .htaccess file.
Thanks.

First, .htaccess files apply to the directory they're in, and every child directory. Your CodeIgniter-specific rules should be in the var/www/test/ CI directory -- usually wherever CI's index.php file is.
Second, your root .htaccess is kind of weird. RewriteCond conditions accumulate until there a RewriteRule rule fires, then they are reset. Your two %{REQUEST_URI} conditions conflict with each other, since the URI can't start with both. I'm not really sure what this .htaccess rule is doing, but if your URLs starting at the root have no bearing on your CI application, I don't think it's necessary to have it there in the first place. I can't say for sure without knowing your directory structure and how you want your website to function.
kjetilh is right - unless your environments share exactly the same settings, it's unlikely an .htaccess will be universally functional. Your best bet is to start them as simple as possible, and modify as necessary. A good starting .htaccess for CI 2.1.x is something like:
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond $1 !^(favicon\.ico|robots\.txt)
RewriteRule ^(.*)$ index.php/$1 [L]
You can add your assets directories or any paths that you don't want redirected in the third RewriteCond (remember to properly escape regular expression characters such as periods). A RewriteBase rule definitely comes in handy if CI is in a subdirectory. You also don't need the system and application folder references with 2.1.x, since those folders have their own .htaccess files blocking access to them.

Related

Rewrite folders with .htaccess

For a website, I want to set up a redirection using .htaccess.
My folder structure is something like
/
/folderA/
/folderB/
/index/
where folderA and B and index contain subfolders and files. Now, I want to rewrite all requests for the root / and for all not existing folders and files to index. It should be masked. It seems to me like an easy task but I cannot get it to work. So far, I tried
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule (.*) /index/$1 [L]
The redirection somehow works but it does not work when I call the root http://example.org/ directly.
Is the root also seen as valid directory or excluded in the RewriteCond checks? Any ideas how to realize that?
Yes, the root is also a directory. You will need to add another rule to rewrite the root only. For example:
RewriteRule ^$ /index/ [L]
And since the root is a directory, you might as well exclude this from the first rule. ie. Change (.*) to (.+).
HOWEVER, your existing rule will result in a rewrite-loop (500 error) if the URL you are rewriting to in /index/... does not exist either*1. eg. If you request /foo, it rewrites to /index/foo and if /index/foo does not exist then it rewrites to /index/index/foo to /index/index/index/foo etc.
You will need to add an additional condition (or use a negative lookahead) to prevent requests to /index/... itself being rewritten.
(*1 Unless you have another .htaccess file in the /index subdirectory that contains mod_rewrite directives and you have not enabled mod_rewrite inheritance.)
For example:
RewriteEngine On
RewriteCond %{REQUEST_URI} !^/index/
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule (.+) /index/$1 [L]
RewriteRule ^$ /index/ [L]

Modify .htaccess for codeigniter URLS

I am trying to remove the index.php from the url in codeigniter. Rewrite mod scripts are all over the place but I can't find the .htaccess file!! I tried looking in the root directory and everywhere else, but no luck.
From what I read it should be in application folder and when I go there i find the .htaccess file and all it has is deny from all. This is not the same content every one else is sharing online before modification.
Please advise.
Actually you don't find it; you create it along side with your index.php file with this contents:
RewriteEngine on
RewriteCond $1 !^(index\.php|images|robots\.txt)
RewriteRule ^(.*)$ /index.php/$1 [L]
In the above example, any HTTP request other than those for index.php,
images, and robots.txt is treated as a request for your index.php
file.
Reference.
The solution is as follows ( for future references)
Create the .htaccess file in the root of the codeigniter application (where you got system, application, etc folders).
Paste this code in it:
RewriteEngine on
RewriteCond $1 !^(index.php|resources|robots.txt)
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L,QSA]
Open config.php and change index = "index.php" to "". Things should work now.

Hiding index.php from ALL subdirectories using .htaccess

So I'm building an ecommerce website using a file structure suggested to me, it looks like this:
http://www.sample.com/index.php
http://www.sample.com/department/index.php
http://www.sample.com/department/men/index.php
Basically I want to get rid of all the index.php filenames in the url, so it looks like this:
http://www.sample.com
http://www.sample.com/department
http://www.sample.com/department/men
I've achieved this for my homepage with adding this code in .htaccess:
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /index.php?/$1 [L]
But, is there any way to apply this rule to every subdirectory in my site with just editing the root .htaccess file? I'm guessing it can be done by adding this rule in .htaccess files in every directory but I'd rather just have one.
Also, is this even a good way to structure a website? I am not particularly fond of it as there are so many index.php files but I'm not sure of a better alternative...
Try putting these rules above the rules that you have in your htaccess file in the document root:
RewriteRule ^index.php$ / [L,R=301]
RewriteRule ^(.+)/index.php$ /$1/ [L,R=301]

Possible .htaccess errors, 1and1.com not rewriting

The following .htaccess file works perfectly on my local server.
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} \.cssc
RewriteRule . style.php [L]
RewriteRule ^admin\.php$ - [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . admin.php [L]
I am doing some work for a client and he is using 1and1.com. I do not know anything about his account or what package he has.
All files are rewritten to admin.php (unless they actually exist). The problem is, I am getting a 404.
I know it's reading the .htaccess file because:
I can put garbage in the file and get a 500 error.
If I do a general rewrite (all pages go to admin.php), it works.
Also, it seems that 1and1 does it's own rewriting. If I go to: http://somewhere/afile, it will include afile.js even though I am not requesting the .js.It is super strange.
Does anyone have any experience with this? Or any insight?
To add some information..
1and1 seem to have some sort of default rewriting already in place that can interfere with your own rules - for example:
RewriteRule ^product$ /product.php [L]
It gave a 404 for this, and also for any other rewrite rule that matched a pre-existing .php file in my root folder.
2 things that 'fixed' it:
Change rewrite text to no longer match filename:
RewriteRule ^productz$ /product.php [L]
Or, change filename:
RewriteRule ^product$ /product_.php [L]
Both work - both very annoying.
The answer was simply "Do not nest .htaccess files". I had one in a sub folder.

Excluding a script from the general UrlRewrite rules

I have following rewrite rules for a website:
RewriteEngine On
# Stop reading config files
RewriteCond %{REQUEST_FILENAME} .*/web.config$ [NC,OR]
RewriteCond %{REQUEST_FILENAME} .*/\.htaccess$ [NC]
RewriteRule ^(.+)$ - [F]
# Rewrite to url
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !^(/bilder_losning/|/bilder/|/gfx/|/js/|/css/|/doc/).*
RewriteRule ^(.+)$ index.cfm?smartLinkKey=%{REQUEST_URI} [L]
Now I have to exclude a script including its eventually querystrings from the above rules, so that I can access and execute it on the normal way, at the moment the whole url is being ignored and forwarded to the index page.
I need to have access to the script shoplink.cfm in the root which takes variables tduid and url (shoplink.cfm?tduid=1&url=)
I have tried to resolve it using this:
# maybe?:
RewriteRule !(^/shoplink.cfm [QSA]
but to be honest, I have not much of a clue of urlrewriting and have no idea what I am supposed to write. I just know that above will generate a nice 500 error.
I have been looking around a lot on stackoverflow and other websites on the same subject, but all I see is people trying to exclude directories, not files. In the worst case I could add the script to a seperate directory and exclude the directory from the rewriterules, but rather not since the script should really remain in the root.
Just also tried:
RewriteRule ^/shoplink.cfm$ $0 [L]
but that didn't do anything either.
Anyone who can help me out on this subject?
Thanks in advance.
Steven Esser
ColdFusion programmer
Please try to put the following line at the top of your config (after RewriteEngine on):
RewriteRule ^shoplink.cfm$ - [L]

Resources