Apache and backslashes in mod_rewrite - php

I want to process all incoming requests through a single script (index.php in web-root).
So, the following is what currently happens: http://localhost/foo/bar/baz
Is routed by Apache (through .htaccess) to: http://localhost/index.php?url=foo/bar/baz
This works well, however, in Firefox I am able to do this: http://localhost/foo\ -> notice the backslash.
And Apache, instead of doing: /index.php?url=foo\
Emits a generic error page saying:
Object not found! The requested URL
was not found on this server. If you
entered the URL manually please check
your spelling and try again.
If you think this is a server error,
please contact the webmaster.
Error 404 localhost Apache/2.2.14
(Win32) DAV/2 mod_ssl/2.2.14
OpenSSL/0.9.8l mod_autoindex_color
PHP/5.3.1 mod_apreq2-20090110/2.7.1
mod_perl/2.0.4 Perl/v5.10.1
Directly going to: http://localhost/index.php?url=foo\ works without issues, however.
All the sites that I've seen on the internet seem to be able to handle backslashes gracefully (e.g., http://stackoverflow.com/tags/php\\\\\).
I consider this behavior a bug and I want to force Apache to forward backslashes correctly.
Here's my .htaccess file in its entirety:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(.*)$ index.php?url=$1 [L]
How can I make this work properly?
Edit: I just tried this on my webhost and this is handled properly by them. Makes me think it's some setting in httpd.conf.

Probably need AcceptPathInfo off to not get those tolerated as forward slashes.

I was just having this issue on WampServer on Windows. This page had the solution - the AllowEncodedSlashes option that RobertPitt mentions above is the correct one, but it must go in the http.conf file, inside a VirtualHost block, eg:
<VirtualHost *:80>
AllowEncodedSlashes On
</VirtualHost>
It doesn't work in a .htaccess file.

Related

My .htaccess not working on my ubuntu server

I've working on my web application under ubuntu server. I would like to override or hide my index.php. I'm trying to access html GET method Pls. help, how I can not identify my error. Thanks!
<.htaccess code>
RewriteEngine On
RewriteRule ^([^/]+)/? index.php?url=$1 [L,QSA]
From this - http://192.168.2.60/api/index.php?url=auth
To this - http://192.168.2.60/api/auth
<Error-msg:>
Not Found
The requested URL /api/auth was not found on this server.
Apache/2.4.18 (Ubuntu) Server at 192.168.2.60 Port 80
I've search for multiple possible answer to work on. This post help me to work my .htaccess. I just need to enable the override at /var/www/. Here is the link it might help you guys!
Enabling .htaccess file to rewrite path

Apache mod_rewrite can't pass variable

I'm having a hard time figuring out why my rewrite rules work perfectly online (the site is up and running), but fail when I'm trying to run a local copy on my mac. I'm not using MAMP o LAMP and I have installed apache and php manually. Apache is 2.2.26 and php is 5.4.20.
There are more rules in my .htaccess file, but the ones I'm concerned about are these two:
RewriteRule ^category/([a-zA-Z0-9_-]+)/([a-zA-Z0-9_-]+)/?$ category.php?slug=$1&page=$2 [L]
RewriteRule ^category/([a-zA-Z0-9_-]+)/?$ category.php?slug=$1 [L]
Basically the variables are not being passed to the php file. Besides this, the rule below works perfectly fine! Even on my local setup I can't see any difference except the fact that I have two rules beginning with "^category/"
RewriteRule ^project/([a-zA-Z0-9_-]+)/?$ project.php?slug=$1 [L]
Any clue why this works online (ubuntu) but fails locally?
I'm going to guess that you have multiviews turned on. This causes mod_negotiation to fuzzy match requests to file-path resources. So when it sees /category/something and sees the file category.php, it sends the request right to the php file (in this case, ala PATHINFO) thus completely bypassing mod_rewrite.
Try turning multiviews off:
Options -Multiviews

Setting BaseURL for Codeigniter

I set my baseurl to:
http://localhost/codeigniter/
in the application/config/config.php file as asked by the installation instructions.
I was following this tutorial. However, when I type in the url: http://localhost/codeigniter/index.php/pages/view , I get the the following error:
Object not found!
The requested URL was not found on this server. If you entered the URL
manually please check your spelling and try again.
If you think this is a server error, please contact the webmaster.
Error 404
localhost 7/27/2012 11:14:39 PM Apache/2.2.12 (Win32) DAV/2
mod_ssl/2.2.12 OpenSSL/0.9.8k mod_autoindex_color PHP/5.3.0
mod_perl/2.0.4 Perl/v5.10.0
I found the following contents in .htaccess file in the path of E:\WEB D\xampp\htdocs\CodeIgniter_2.1.2\application:
Deny from all
I am totally new to the CodeIgniter framework. Can someone help me out?
Ok, well for anyone wondering the final solution to this was that the directories were not properly named. When creating a local server it is important to put the contents of the files in the correct folder.
So if you have http://localhost/codeigniter then the directory should be setup as \path\to\webdirectory\root\codeigniter or in this case: E:\WEBD\xampp\htdocs\codeigniter
try edit your .htaccess file became like this below, and put .htaccess file in your root application folder
RewriteEngine on
RewriteCond $1 !^(index\.php|resources|robots\.txt)
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L,QSA]
Make sure that your php errors are visible.
I had this issue as well when trying to set the base_url for Code Igniter when I was working off localhost. I had a look in the htaccess file, checked that mod_rewrite was switched on, tried altering the base_url but all failed.
The solution was to make sure that the environment was set to development in the main index.php file and to ensure that $config['log_threshold'] = 4; is set in config.php.
After that my php errors showed up. I'd forgotten to switch the development environment for the new website build.

Getting an error when I visit http://localhost

I am trying to access localhost on my xampp via the Chrome browser but I get this error message:
Object not found!
The requested URL was not found on this server. If you entered the
URL manually please check your spelling and try again.
If you think this is a server error, please contact the webmaster.
Error 404
Apache and MySQL are turned on so I don't understand.
You can access "localhost" just fine.
The "404: file not found" error indicates the server can't find the requested file once you've connected to localhost.
ADDENDUM:
Maybe try to create a regular html file called test.html in one of
your vhosts that is not working. Then try to visit that url. That
should give you some clue as to what may be the problem. – Gohn67
This is good advice. Try it. Specifically:
1) Create the following five files:
C:/xampp/htdocs/dummy-host.localhost/test.html
C:/xampp/htdocs/my/test.html
C:/xampp/htdocs/launcher/public_html/test.html
C:/xampp/htdocs/website/httpdocs/test.html
C:/xampp/htdocs/my/test.html
2) Make sure each of these directories exist, and each has a "test.html" with the words "TESTING 1...2...3" in it
3) Try each of these five URLs in your browser:
http://localhost/test.html
http://itutormaths.web/test.html
http://itutormaths.mod/test.html
http://itutormaths.hub/test.html
http://my.itm/test.html
4) Report back the exact error you get from each browser URL
Navigate here:
http://localhost/
If that works, everything is okay.
Paste wordpress to xampp/htdocs, and you should not get any error.
I had a similar issue and this is how I resolved it:
Open the httpd:conf file on the Apache module in XAMPP (click the Congif button)
Scroll through the file - after the first set of comments (#) there is a line of code which says 'Listen 80'. Change this to 81 (or whatever other port you want to use).
Open web browser and explicitly define the port in the web address:
127.0.0.1:81
The may or may work for you; I initially had the issue that Apache wouldn't start at all, which is why I changed the port number (couldn't be bothered to figure out which service was using port 80 - if you want to you can use the command netstats -a or netstats -b in command prompt to figure it out)
Hope this helps
I just encountered this and solve this by creating .htaccess file.
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /yourfoldername/
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L]
</IfModule>

Something rewrites rules for php when Apache mod_rewrite module is disabled

I run the following url:
http://localsite/index.php/foo/bar
index.php is executed and outputs variable:
$_SERVER[SCRIPT_FILENAME] = E:/path/to/file/index.php
mod_rewrite in Apache is disabled.
Who rewrites the rule?Or what happens?
How index.php is found? Why apache decided to run it?
My configuration:
Windows Vista, Apache Apache/2.2.14 (Win32) PHP/5.3.1 (with php module).
(Indeed the problem is the rule is actually rewritten before mod_rewrite - this is when mod_rewrite is enabled. This causes that RewriteCond %{REQUEST_FILENAME} !-f is always false, because /foo/bar is trimmed before RewriteCond).
This is caused by the AcceptPathInfo Apache directive.
It treats everything up to
http://localsite/index.php
as the resource, and puts
/foo/bar
into the $_SERVER["PATH_INFO"] variable.
It's sometimes used as a poor man's URL rewriter when mod_rewrite isn't available - with the downside that in a normal configuration, there has to be a .php somewhere in the URL.
Your options are to turn this off, or to use a different URL - depending on your situation.
Maybe you should use $_SERVER[REQUEST_URI] instead, afaik, SCRIPT_FILENAME tells you which file php was started with.

Categories