Htaccess file extension in rewrite causing 404 - php

I am having a problem with one of my rewrite rules. I am in the process of developing an application where I want to control file access via a script. My plan is to simply pass a rewrite rule through to my script with parameters to retrieve the appropriate file(s).
However, I am having some issues when trying to pass a file name with an extension as part of a rewrite rule. Perhaps someone can point out what i'm doing wrong.
My rewrite rule:
RewriteEngine On
RewriteBase /
RewriteRule ^files/(.+)/(.*)$ /index.php/files/$1/$2 [NC,L]
What works: http://localhost/files/12345/index
What is causing 404 errors to be thrown: http://localhost/files/12345/index.css
I've been battling with this for quite some time now, it almost acts like it's trying to load the css file, not finding it, and never running the rerwite rule. I am using IntelliJ's PHP Built-in Web Server for my host environment, not sure if that has anything to do with it or not.
UPDATE:
If I go to http://localhost/index.php/files/12345/index.css directly the page works as expected, it does not return a 404. This rule is the only rule in my .htaccess file.
If I echo the $_SERVER["PATH_INFO"]; from the index.php I get /files/12345/index.css back as the value

You need to skip existing files and directories from this rewrite:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^(files/[^/]+/.*)$ /index.php/$1 [NC,L]

Found that the problem was simply that PHP Built-In Web Server doesn't honor .htaccess the same as apache does. Tossed this to a full blown apache server, and everything worked like a charm.

Related

htaccess friendly URL rewrite rule

I have recently change my hosting and i need a htaccess rewrite rule for my files. I tried many examples but no one really works for my case. I have never been really good in htaccess and on my older hosting i didn't really need anything it just worked but here is not. Basically i want that my PHP files are without extensions and treated like a directory. So for example i have a URLs like these:
www.domain.com/file1/{id}/{nick}
So for example:
www.domain.com/myfile1/104/username
www.domain.com/myotherfile/455/nick
File1 in this case is a PHP file and {id} and {nick} are changable. I have this structure on my entire site for many other PHP files etc. So if possible i want one universal rule for all files. I tried with htaccess to remove php extenstion etc but all I got is 404 error. Also URL in browser should stay friendly without PHP extension. So in my case if i rewrite my URL manually in:
www.domain.com/file1.php/{id}/{nick} it worked but i don't want to change all the links etc on my website. So all i want is to hide PHP extension and treat PHP files as directory.
Thanks
You can use this single and generic front controller rule in site root .htaccess:
AcceptPathInfo On
DirectoryIndex index.php
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^([\w-]+)/(.*)$ $1.php/$2 [L]

Rewrite Mode and PHP using .htaccess not working (Error 404)

Here is my .htaccess:
RewriteEngine on
RewriteRule !\.(js|ico|gif|ini|jpg|png|css|html|swf|flv|xml)$ index.php
I've been using this file for about a year now and frankly, if you ask me I'd say it means: If you're trying to access something NOT ended with js, ico, gif... flv, xml THEN redirect to index.php
Now I'm trying to deploy an application at a new server (centOS WITH mod_rewrite enabled) and I get the Error 404 when trying to access stuffs that usually works (at a different web server, for instance).
Since I know too little about it, I'd like some input on what should I be looking for to find the problem. Thanks in advance.
You should perform that kind of match in RewriteCond
RewriteEngine on
RewriteRule ^index\.php$ - [L]
RewriteCond %{REQUEST_URI} !\.(js|ico|gif|ini|jpg|png|css|html|swf|flv|xml)$
RewriteRule .* index.php
Note: the first rule is needed so that the rules won't loop (exclude index.php itself from rewriting).

Trying to figure out htaccess issues

I'm having some issues figuring out how to use an htaccess file. I've got apache/php installed on an ubuntu system and mod_rewrite is turned on (php_info() states that it's in the list of loaded modules). The web server works, displays html and php files, so I'm happy with that.
What I'm trying to figure out now is how to use an htaccess file properly. I created a directory, /data, with an index.php file in it. All I want it to do at the moment is just display the $_REQUEST variable so I can see if things are working the way I assume they should.
Example: If I type in the following URL: localhost/data/info1/ I want the htaccess file to access localhost/data/index.php?request=info1
However, no matter what I enter in to the htaccess file, I keep getting 404 errors, and I'd like to understand why.
Here's my htaccess file:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule data/(.*)$ data/index.php?request=$1 [L]
</IfModule>
I've made no changes to the config file, to activate mod_rewrite, I used the ubuntu a2enmod command so ubuntu did it for me. After that, I restarted apache.
What I can't figure out is why this doesn't work. My assumption is that there's still some sort of configuration I need to do on the server end, but I honestly don't know what. Is there any advice anyone can offer me?
Here's the fix:
RewriteRule ^data/(.*)$ data/index.php?request=$1 [L]
(You were missing a ^)
EDIT:
In the OP, you have another leading / in the URL example, in this case it'd be:
RewriteRule ^data/(.*)/$ data/index.php?request=$1 [L]

Some header passing variable?

Im trying to pass some variables from the URL to the PHP script. Now I know that www.site.com/index.php?link=HELLO would require $_GET['link'] to get the variable "link". I was hoping there are other ways to do this without the variable.
For instance I want a link structure like this: www.site.com/HELLO. In this example I know that I have to create a Directory called Hello place an index file and it should work but I don't want to create a directory and Im hoping there's a way to "catch" that extra part after the domain. I'm thinking of creating a custom HTTP 404 Page that will somehow get the variable of the not found page, but I don't know how to get the HTTP 404 error parameters. Is there another simpler way to get a variable without the use of the extra ?link= part? I just want it to be a structure like this www.site.com/HELLO.
What you want is URL rewriting. You don't mention what kind of web server you're using, so I'll assume it's Apache.
If you have mod_rewrite enabled on your web server, this can easily be accomplished by creating a .htaccess file in your document root with the following
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?request=$1
This will make all requests - that match the regular expression [a-zA-Z0-9]+ - get forwarded to index.php. For instance, if you try to access domain.com/hello, PHP would interpret this as trying to access index.php?request=hello.
You can read more about this in the Apache HTTP Server manual about mod_rewrite.
In which case, you normally use .htaccess to alter the URL in some form. For instance:
RewriteEngine On #Enable Rewrite
RewriteCond %{REQUEST_FILENAME} !-f #If requested is not an existing file
RewriteCond %{REQUEST_FILENAME} !-d #If requested is not an existing directory
RewriteRule ^(.*)$ /index.php?link=$1 [L] #Preform Rewrite
Which will make www.site.com/hello to www.site.com/index.php?link=hello. This change is invisible to the user (he will still see www.site.com/hello as an address). Be advised that it may cause trouble if you try using relative paths with CSS/JavaScript files.
You need to use URL rewriting to do this. If you're using Apache: http://httpd.apache.org/docs/current/mod/mod_rewrite.html
you can do that with a mod-rewrite structure.
Here's a pretty good tutorial on how to set it up with php/apache.
http://www.sitepoint.com/guide-url-rewriting/

Setup clean URLs like CodeIgniter

I like the way CodeIgniter cleans URLs for sites, however, I have a site that is too involved to start over using the CI framework (at least for now), and I don't need the depth CI provides, I only have one level deep.
Is there an easy way to do this easily using straight PHP?
index.php?id=2454
index.php/2454/
NOTE: I need a straight PHP solution because the server is Windows and .htaccess is not setup to work.
If you use mod_rewrite in apache you can allow your application to dispatch requests as you want.
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} -s [OR]
RewriteCond %{REQUEST_FILENAME} -l [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^.*$ - [NC,L]
RewriteRule ^.*$ index.php [NC,L]
Say your server is located # http://coolguy.com and a users accesses http://coolguy.com/mycleanurl/
With the above rewrite rule in your .htaccess or apache configuration you can intercept which url is being accessed via $_SERVER['REDIRECT_URL'] and send it off to the specific code point you want.
The "RewriteCond" directives i have in there are used to ignore this rewrite rule if there exists a file directly at the location the user has specified, this is handy for static assets like CSS and images where you dont want to have to dispatch these requests yourself.
Check out $_SERVER['PATH_INFO'] - it returns anything trailing the script filename but preceding the query.
For example, in the URL:
http://www.domain.com/index.php/var1/var2
$_SERVER['PATH_INFO'] would contain /var1/var2
You could then write a function in your __construct() or init(), etc, to parse the path (e.g. explode("/", $_SERVER['PATH_INFO'])) and use the resulting array as variables.
Are you using Apache? If so, look into files called .htaccess. They rewrite the URL when they're stored in your directory. So if you put an .htaccess file in your web root with
# .htaccess
RewriteEngine on
RewriteCond %{REQUEST_URI} !^/www/
RewriteRule ^(.*)$ /www/$1 [L]
then when a person goes to the URL yourdomain.com/page1, it will actually retrieve the resource yourdomain.com/www/page1 on your server, but their browser won't see the www.
So, for very simple URL rewriting you can use this to hide the ?var=val&var2=val2 crap in the URL
Information on the rules is here
If your using PHP on windows and want to do URL Rewriting you have two choices:
Url Rewrite (which allows you to use rewrite rules in the web.config) http://www.iis.net/download/urlrewrite - Open the site in IIS Manager and select "Url Rewriting", then add your rules.
Buy Helicon-APE or similar (which allows you to use a native .htaccess file) http://www.helicontech.com/ape/ - we use this on our shared windows hosting servers with great success

Categories