Hide file extension with htaccess - php

I'm not the best with mod rewrite so if anybody can help me out here that would be great.
I'm using a markdown processor script and it's using rewrite to grab any files that end with a markdown file type. However, I'd like this script to grab any files within a folder, rather than any files that end with the markdown file type.
Here's the htaccess:
# display Markdown as HTML by default
RewriteEngine on
RewriteRule .+\.(markdown|mdown|md|mkd)$ /static/includes/markdown/render.php
RewriteRule .+\.(markdown|mdown|md|mkd)\-text$ /static/includes/markdown/render.php [L]
Is there a way to grab all files within a folder called (let's say) "folder" and eliminate the file type on the end?
So maybe have a URL like
website.com/home
that actually is
website.com/home.md
and is processed with the markdown script?
Hope this makes sense.

The re-write module and it's .htaccess files actually work on a per folder basis. Usually one would have a main .htaccess file in the web root of a site/server. However you can add numerous .htaccess files throughout your site's folder structure giving each individual folder specific rules.
All you would have to do is add another .htaccess file to your markdown folder and enable it to parse URL's without file extensions, forwarding it to a script which will be able to detect what original file was requested -
RewriteEngine on
RewriteRule ^(.*)$ /static/includes/markdown/render.php?file=$1 [L,QSA]
Basically what is happening here is that any file requested within this folder will be passed through your render.php file.
Now in your render.php file, you would have a $_GET parameter of file containing the original URL. For a url of http://example.com/markdown/foo, your render.php would have foo in the file parameter -
/static/includes/markdown/render.php?file=foo
If you set the correct headers in render.php it will be able to print out any format of file, hiding it's extension in a "fake" URL.

Related

strange issue with .htaccess redirect breaks if source path is the same name as a existing php file

I've noticed a strange situation that occurs when I try to create a mod-rewrite entry where the source path is also the same name as an existing php file in the same folder. See example
RewriteRule ^users/([^/]*)/([^/]*)$ redirect.php?page=user&name=$1&id=$2 [L]
The issue is that I'm calling the directory "/users/" as the source path in the rule and if I also have a file in the root folder with the same name "users.php" then the rule above ends up pointing to that file instead of the page I want to redirect rule to point to (which is redirect.php in this case).
Any suggestions?
That's because of Apache content negotiation.
To disable it, put this line in your htaccess (for example, before RewriteEngine on line)
Options -MultiViews

.htaccess dynamic variables from php?

I'm trying to find a way to dynamically change the root folder of a site. I currently set the root folder with the following:
RewriteCond %{DOCUMENT_ROOT}/foo/bar%{REQUEST_URI} -f
RewriteRule ^(.*)$ %{DOCUMENT_ROOT}/foo/bar%{REQUEST_URI} [QSA,L]
So if a user visits http://example.com it's pulling from /foo/bar. However I would like to be able to change /foo/bar to any other random directory of my choosing with PHP.
This is done is because the root folder of the site can be changed quite regularly for various reasons.
I could just write a PHP script that will replace /foo/bar in the .htaccess file but that's not a wise decision to give PHP write access to .htaccess. Ideally I would like to have a simple .txt file that can be included to pull in /foo/bar and then PHP can write that .txt file.
Is this possible, or is there any other way to get this done? Essentially the thing that matters is that PHP can safely change the root folder without a Apache restart.
You might want to look into RewriteMap, but actually, I'd make a symlink, and point it to the proper place.

Replace mod_autoindex with PHP indexer in all directories without adding a index.php to all of them

I like the ability to automatically index a folder, so that I can serve a large number of files, without adding links to a page all the time. However I dislike the httpAuth login box that comes with using .htaccess to secure a directory. Plus there are more features I wanted on my indexes. So I have written a PHP script to generate indexes the way I want, so that I can control everything with PHP, store users in SQL, add extra links to my file editor, and log in with a nice looking web form.
The problem is any new directory needs an index.php file that includes the script, or I just get apache indexes. Which means copying a one line index.php file to every directory. I could generate it using PHP, but if I am working with FTP to manage files that will not solve all the problems. Is there any way to configure apache to display my index script in any directory that does not have an index files of its own? Such that it acts just like mod_autoindex? But with my custom script.
Put the index.php in your root, then use redirect
RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} -d #if directory exists
RewriteRule . /index.php [L]
Then inside index.php use $_SERVER['REQUEST_URI'] to figure out which folder was requested, then use PHP to display what you wanna display, e.g. using opendir, readdir, etc (don't forget to handle the '.' and '..' unix files)

How to tell code igniter to ignore /lib/css path

How can I tell CodeIgniter to ignore the /lib/css folder and load my stylesheets instead of trying to load things via my controller?
I'm just going to take it straight from the user guide:
By default, the index.php file will be
included in your URLs:
example.com/index.php/news/article/my_article
You can easily remove this file by
using a .htaccess file with some
simple rules. Here is an example of
such a file, using the "negative"
method in which everything is
redirected except the specified items:
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.
With .htaccess: You decide which files and folders can be accessed directly, like css or images.
Without .htaccess: All references to the application are routed through index.php so there is no possibility of conflict
In the example above, two files and one directory (images) are allowed direct access. Any other requests are routed through Codeigniter, so if you HAVE a controller called css and want to access it - you can do so by removing it from this list. If you have a directory named css, you would add it to this list of exceptions and be able to access it, and all files within it, directly.
Another note: do not try to use ../../relative/paths with CI, use full URLs or /absolute/paths. Use the helpers that exist, like base_url(), and link_tag() as seen in Phphelp's example
Your question is not exactly clear. If this is what you want, you can do the following.
echo link_tag('css/mystyles.css');
Gives:
<link href="http://site.com/css/mystyles.css" rel="stylesheet" type="text/css" />
In place of mystyles.css, you can give your style sheet.

How to access a .php file with Kohana (ignore routing)

let's say we have simple file
<?php
echo "hello world"
I'd like to place the file in /modules/myModul/(somewhere).
Where do I have to save the file and how do i call it via the browser url?
As far as I know, Kohana's default .htaccess already includes the check whether the URL corresponds to a real file
RewriteCond %{REQUEST_FILENAME} !-f
So if you save this php file somewhere and access it by the URL you would expect to find it at, the request will not be sent to Kohana at all.
If you placed the file in /modules/myModul/example.php, then the URL would be http://www.example.com/modules/myModul/example.php
By default every file in application|modules|system will be hidden. You should use webroot directory (were the index.php and .htaccess placed) or another directory (for example, create public folder for your non-framework scripts). So, it will be http://example.com/script.php or http://example.com/public/script.php.

Categories