Can we use RewriteRule for internal paths? - apache

I've always seen RewriteRule used for public URL paths, but can we also map the URLs to internal paths?
For example, to redirect all links to my_page.php, is this allowed? :
RewriteRule .* /home/yccaucom/public_html/my_page.php [last,noescape]

You cannot redirect to a folder or file that is not within the domain root.
Given that your root folder is:
/home/yccaucom/public_html/
You can only redirect to what is within public_html, for example public_html/css or public_html/some_folder, you can also make a symbolic link to an internal folder or file and it should work as well.
Given your rule, it would look like this:
RewriteCond %{REQUEST_URI} !^/my_page.php$ [NC]
RewriteRule ^ /my_page.php [L,NE]
You want the condition to avoid it from falling into a infinite loop.
Or you could check for existent files/folders instead with this 2 conditions:
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
Basically it says if file or folder does not exist redirect.
If you try to use your rule as it is, it will try to redirect to:
/home/yccaucom/public_html/home/yccaucom/public_html/my_page.php
However it will most likely not fail since you don't have any verification to stop the loop so it will give you a fail message saying too many redirects or so.
On a deeper look at the rule, this is what happens:
10.0.0.1 - - [24/Aug/2013:05:25:24 --0300] [somedomain.com/sid#7fe8521040e0][rid#7fe85288af18/initial] (3) [perdir /home/account/public_html/] strip per-dir prefix: /home/account/public_html/asdasdas2 -> asdasdas2
10.0.0.1 - - [24/Aug/2013:05:25:24 --0300] [somedomain.com/sid#7fe8521040e0][rid#7fe85288af18/initial] (3) [perdir /home/account/public_html/] applying pattern '.*' to uri 'asdasdas2'
10.0.0.1 - - [24/Aug/2013:05:25:24 --0300] [somedomain.com/sid#7fe8521040e0][rid#7fe85288af18/initial] (2) [perdir /home/account/public_html/] rewrite 'asdasdas2' -> '/home/account/public_html/edit.php'
10.0.0.1 - - [24/Aug/2013:05:25:24 --0300] [somedomain.com/sid#7fe8521040e0][rid#7fe85288af18/initial] (2) [perdir /home/account/public_html/] trying to replace prefix /home/account/public_html/ with /
10.0.0.1 - - [24/Aug/2013:05:25:24 --0300] [somedomain.com/sid#7fe8521040e0][rid#7fe85288af18/initial] (5) strip matching prefix: /home/account/public_html/edit.php -> edit.php
10.0.0.1 - - [24/Aug/2013:05:25:24 --0300] [somedomain.com/sid#7fe8521040e0][rid#7fe85288af18/initial] (4) add subst prefix: edit.php -> /edit.php
10.0.0.1 - - [24/Aug/2013:05:25:24 --0300] [somedomain.com/sid#7fe8521040e0][rid#7fe85288af18/initial] (1) [perdir /home/account/public_html/] internal redirect with /edit.php [INTERNAL REDIRECT]
10.0.0.1 - - [24/Aug/2013:05:25:24 --0300] [somedomain.com/sid#7fe8521040e0][rid#7fe852858798/initial/redir#1] (3) [perdir /home/account/public_html/] strip per-dir prefix: /home/account/public_html/edit.php -> edit.php
10.0.0.1 - - [24/Aug/2013:05:25:24 --0300] [somedomain.com/sid#7fe8521040e0][rid#7fe852858798/initial/redir#1] (3) [perdir /home/account/public_html/] applying pattern '.*' to uri 'edit.php'
10.0.0.1 - - [24/Aug/2013:05:25:24 --0300] [somedomain.com/sid#7fe8521040e0][rid#7fe852858798/initial/redir#1] (2) [perdir /home/account/public_html/] rewrite 'edit.php' -> '/home/account/public_html/edit.php'
10.0.0.1 - - [24/Aug/2013:05:25:24 --0300] [somedomain.com/sid#7fe8521040e0][rid#7fe852858798/initial/redir#1] (1) [perdir /home/account/public_html/] initial URL equal rewritten URL: /home/account/public_html/edit.php [IGNORING REWRITE]

Related

internal mod_rewrite leads to infinite redirects

I have a server on localhost:8080 and I've piped requests to it via a tunneling software called ngrok so now I want to setup my mod_rewrite policy to map and serve any request from mysubdomain.ngrok.com to localhost:8080 without redirecting the browser.
# Turn on rewrites
RewriteEngine on
###
## Rules for "mysubdomain.ngrok.com"
###
RewriteCond %{HTTP_HOST} ^mysubdomain.ngrok.com$
RewriteRule .? %{REQUEST_URI} [L]
###
## Rules for "localhost:8080"
###
RewriteCond %{HTTP_HOST} ^localhost:8080$
RewriteRule . - [E=REWRITEBASE:/prestashop/]
RewriteRule ^api/?(.*)$ %{ENV:REWRITEBASE}webservice/dispatcher.php?url=$1 [QSA,L]
But I end up in infinite redirect loop! What's the best way to avoid this and accomplish my goal?
[/initial] [perdir /Users/root/dev/bitnami-ps1/apps/prestashop/htdocs/] add path info postfix: /Users/root/dev/bitnami-ps1/apps/prestashop/htdocs/api -> /Users/root/dev/bitnami-ps1/apps/prestashop/htdocs/api/products
[/initial] [perdir /Users/root/dev/bitnami-ps1/apps/prestashop/htdocs/] strip per-dir prefix: /Users/root/dev/bitnami-ps1/apps/prestashop/htdocs/api/products -> api/products
[/initial] [perdir /Users/root/dev/bitnami-ps1/apps/prestashop/htdocs/] applying pattern '.?' to uri 'api/products'
[/initial] [perdir /Users/root/dev/bitnami-ps1/apps/prestashop/htdocs/] RewriteCond: input='mysubdomain.ngrok.com' pattern='^mysubdomain.ngrok.com$' => matched
[/initial] [perdir /Users/root/dev/bitnami-ps1/apps/prestashop/htdocs/] rewrite 'api/products' -> '/prestashop/api/products'
[/initial] [perdir /Users/root/dev/bitnami-ps1/apps/prestashop/htdocs/] internal redirect with /prestashop/api/products [INTERNAL REDIRECT]
[/initial/redir#1] [perdir /Users/root/dev/bitnami-ps1/apps/prestashop/htdocs/] add path info postfix: /Users/root/dev/bitnami-ps1/apps/prestashop/htdocs/api -> /Users/root/dev/bitnami-ps1/apps/prestashop/htdocs/api/products
[/initial/redir#1] [perdir /Users/root/dev/bitnami-ps1/apps/prestashop/htdocs/] strip per-dir prefix: /Users/root/dev/bitnami-ps1/apps/prestashop/htdocs/api/products -> api/products
[/initial/redir#1] [perdir /Users/root/dev/bitnami-ps1/apps/prestashop/htdocs/] applying pattern '.?' to uri 'api/products'
[/initial/redir#1] [perdir /Users/root/dev/bitnami-ps1/apps/prestashop/htdocs/] RewriteCond: input='mysubdomain.ngrok.com' pattern='^mysubdomain.ngrok.com$' => matched
[/initial/redir#1] [perdir /Users/root/dev/bitnami-ps1/apps/prestashop/htdocs/] rewrite 'api/products' -> '/prestashop/api/products'
[/initial/redir#1] [perdir /Users/root/dev/bitnami-ps1/apps/prestashop/htdocs/] internal redirect with /prestashop/api/products [INTERNAL REDIRECT]
...
[/initial/redir#10] [perdir /Users/root/dev/bitnami-ps1/apps/prestashop/htdocs/] add path info postfix: /Users/root/dev/bitnami-ps1/apps/prestashop/htdocs/api -> /Users/root/dev/bitnami-ps1/apps/prestashop/htdocs/api/products
[/initial/redir#10] [perdir /Users/root/dev/bitnami-ps1/apps/prestashop/htdocs/] strip per-dir prefix: /Users/root/dev/bitnami-ps1/apps/prestashop/htdocs/api/products -> api/products
[/initial/redir#10] [perdir /Users/root/dev/bitnami-ps1/apps/prestashop/htdocs/] applying pattern '.?' to uri 'api/products'
[/initial/redir#10] [perdir /Users/root/dev/bitnami-ps1/apps/prestashop/htdocs/] RewriteCond: input='mysubdomain.ngrok.com' pattern='^mysubdomain.ngrok.com$' => matched
[/initial/redir#10] [perdir /Users/root/dev/bitnami-ps1/apps/prestashop/htdocs/] rewrite 'api/products' -> '/prestashop/api/products'
[/initial/redir#10] [perdir /Users/root/dev/bitnami-ps1/apps/prestashop/htdocs/] internal redirect with /prestashop/api/products [INTERNAL REDIRECT]
Looking at logs and rules I can see that this rule is causing infinite looping:
RewriteCond %{HTTP_HOST} ^mysubdomain.ngrok.com$
RewriteRule .? %{REQUEST_URI} [L]
As it is unconditionally writing a URI to itself for domain=mysubdomain.ngrok.com
Just comment it out and looping will be fixed.

rewrite utf8 characters in htaccess

I am trying to rewrite é to e, using the htaccess file.
All the following rules have all failed.
RewriteRule ^(.*)é(.*) $1e$2 [R,L]
RewriteRule ^(.*)\xc3\xa9(.*) $1e$2 [R,L]
RewriteRule ^(.*)%C3%A9(.*) $1e$2 [R,L]
I have the following debug output, however am unable to figure out what it says.
197.178.121.8 - - [24/Feb/2014:00:29:35 --0800] [www.speaksheets.com/sid#7ff60b7a0318][rid#7ff60d1edf58/initial] (2) init rewrite engine with requested uri /translation/how-to-say-bon-app\xc3\xa9tit-in-french-bon-appetit/
197.178.121.8 - - [24/Feb/2014:00:29:35 --0800] [www.speaksheets.com/sid#7ff60b7a0318][rid#7ff60d1edf58/initial] (3) applying pattern '^(.*)' to uri '/translation/how-to-say-bon-app\xc3\xa9tit-in-french-bon-appetit/'
197.178.121.8 - - [24/Feb/2014:00:29:35 --0800] [www.speaksheets.com/sid#7ff60b7a0318][rid#7ff60d1edf58/initial] (3) applying pattern '^(.*)' to uri '/translation/how-to-say-bon-app\xc3\xa9tit-in-french-bon-appetit/'
197.178.121.8 - - [24/Feb/2014:00:29:35 --0800] [www.speaksheets.com/sid#7ff60b7a0318][rid#7ff60d1edf58/initial] (1) pass through /translation/how-to-say-bon-app\xc3\xa9tit-in-french-bon-appetit/
197.178.121.8 - - [24/Feb/2014:00:29:35 --0800] [www.speaksheets.com/sid#7ff60b7a0318][rid#7ff60d1edf58/initial] (3) [perdir /home/speaksheets.com/public_html/] add path info postfix: /home/speaksheets.com/public_html/translation -> /home/speaksheets.com/public_html/translation/how-to-say-bon-app\xc3\xa9tit-in-french-bon-appetit/
197.178.121.8 - - [24/Feb/2014:00:29:35 --0800] [www.speaksheets.com/sid#7ff60b7a0318][rid#7ff60d1edf58/initial] (3) [perdir /home/speaksheets.com/public_html/] strip per-dir prefix: /home/speaksheets.com/public_html/translation/how-to-say-bon-app\xc3\xa9tit-in-french-bon-appetit/ -> translation/how-to-say-bon-app\xc3\xa9tit-in-french-bon-appetit/
197.178.121.8 - - [24/Feb/2014:00:29:35 --0800] [www.speaksheets.com/sid#7ff60b7a0318][rid#7ff60d1edf58/initial] (3) [perdir /home/speaksheets.com/public_html/] applying pattern '^index\.php$' to uri 'translation/how-to-say-bon-app\xc3\xa9tit-in-french-bon-appetit/'
197.178.121.8 - - [24/Feb/2014:00:29:35 --0800] [www.speaksheets.com/sid#7ff60b7a0318][rid#7ff60d1edf58/initial] (3) [perdir /home/speaksheets.com/public_html/] add path info postfix: /home/speaksheets.com/public_html/translation -> /home/speaksheets.com/public_html/translation/how-to-say-bon-app\xc3\xa9tit-in-french-bon-appetit/
197.178.121.8 - - [24/Feb/2014:00:29:35 --0800] [www.speaksheets.com/sid#7ff60b7a0318][rid#7ff60d1edf58/initial] (3) [perdir /home/speaksheets.com/public_html/] strip per-dir prefix: /home/speaksheets.com/public_html/translation/how-to-say-bon-app\xc3\xa9tit-in-french-bon-appetit/ -> translation/how-to-say-bon-app\xc3\xa9tit-in-french-bon-appetit/
197.178.121.8 - - [24/Feb/2014:00:29:35 --0800] [www.speaksheets.com/sid#7ff60b7a0318][rid#7ff60d1edf58/initial] (3) [perdir /home/speaksheets.com/public_html/] applying pattern '.' to uri 'translation/how-to-say-bon-app\xc3\xa9tit-in-french-bon-appetit/'
197.178.121.8 - - [24/Feb/2014:00:29:35 --0800] [www.speaksheets.com/sid#7ff60b7a0318][rid#7ff60d1edf58/initial] (2) [perdir /home/speaksheets.com/public_html/] rewrite 'translation/how-to-say-bon-app\xc3\xa9tit-in-french-bon-appetit/' -> '/index.php'
The question is, what is wrong with this rule, RewriteRule ^(.*)\xc3\xa9(.*) $1e$2 [R,L]?
Your rule:
RewriteRule ^(.*)\xc3\xa9(.*) $1e$2 [R,L]
Is fine and should work (worked in my testing also).
But from rewrite log it appears some rule in internally rewriting it to index.php first.
Just make sure this above rule is very first rule in your .htaccess.
URLs cannot contain non-ASCII characters. All non-ASCII characters must be escaped to percent escapes like %C3%A9. ^(.*)é(.*) should never match a valid URL. \xYY is not defined as a byte escape sequence in Apache as far as I'm aware, so ^(.*)\xc3\xa9(.*) does not mean what you think it means either. The only valid rule is ^(.*)%C3%A9(.*), which contains the URL escaped version of a UTF-8 representation of "é".

mod_rewrite END flag not working in httpd 2.4

The END flag for mod_rewrite in Apache 2.4 does not appear to be working correctly. I have a directory named test with a file test.html. I have placed an .htaccess file in the directory with the following content
RewriteEngine on
RewriteRule ^test$ test.html [NC,QSA,END]
RewriteRule ^$ test.html [NC,QSA,END]
Here is the rewrite log for the request http://localhost.dev/test/
[...] 10.0.2.2 - - [localhost.dev/sid#2532c00][rid#2a56d60/initial] [perdir /var/www/vhosts/test/] strip per-dir prefix: /var/www/vhosts/test/ ->
[...] 10.0.2.2 - - [localhost.dev/sid#2532c00][rid#2a56d60/initial] [perdir /var/www/vhosts/test/] applying pattern '^test$' to uri ''
[...] 10.0.2.2 - - [localhost.dev/sid#2532c00][rid#2a56d60/initial] [perdir /var/www/vhosts/test/] strip per-dir prefix: /var/www/vhosts/test/ ->
[...] 10.0.2.2 - - [localhost.dev/sid#2532c00][rid#2a56d60/initial] [perdir /var/www/vhosts/test/] applying pattern '^$' to uri ''
[...] 10.0.2.2 - - [localhost.dev/sid#2532c00][rid#2a56d60/initial] [perdir /var/www/vhosts/test/] rewrite '' -> 'test.html'
[...] 10.0.2.2 - - [localhost.dev/sid#2532c00][rid#2a56d60/initial] [perdir /var/www/vhosts/test/] add per-dir prefix: test.html -> /var/www/vhosts/test/test.html
[...] 10.0.2.2 - - [localhost.dev/sid#2532c00][rid#2a56d60/initial] [perdir /var/www/vhosts/test/] strip document_root prefix: /var/www/vhosts/test/test.html -> /test/test.html
[...] 10.0.2.2 - - [localhost.dev/sid#2532c00][rid#2a56d60/initial] [perdir /var/www/vhosts/test/] internal redirect with /test/test.html [INTERNAL REDIRECT]
[...] 10.0.2.2 - - [localhost.dev/sid#2532c00][rid#2a44060/subreq] [perdir /var/www/vhosts/test/] strip per-dir prefix: /var/www/vhosts/test/index.php -> index.php
[...] 10.0.2.2 - - [localhost.dev/sid#2532c00][rid#2a44060/subreq] [perdir /var/www/vhosts/test/] applying pattern '^test$' to uri 'index.php'
[...] 10.0.2.2 - - [localhost.dev/sid#2532c00][rid#2a44060/subreq] [perdir /var/www/vhosts/test/] strip per-dir prefix: /var/www/vhosts/test/index.php -> index.php
[...] 10.0.2.2 - - [localhost.dev/sid#2532c00][rid#2a44060/subreq] [perdir /var/www/vhosts/test/] applying pattern '^$' to uri 'index.php'
[...] 10.0.2.2 - - [localhost.dev/sid#2532c00][rid#2a44060/subreq] [perdir /var/www/vhosts/test/] pass through /var/www/vhosts/test/index.php
[...] 10.0.2.2 - - [localhost.dev/sid#2532c00][rid#2a44060/subreq] [perdir /var/www/vhosts/test/] strip per-dir prefix: /var/www/vhosts/test/index.html -> index.html
[...] 10.0.2.2 - - [localhost.dev/sid#2532c00][rid#2a44060/subreq] [perdir /var/www/vhosts/test/] applying pattern '^test$' to uri 'index.html'
[...] 10.0.2.2 - - [localhost.dev/sid#2532c00][rid#2a44060/subreq] [perdir /var/www/vhosts/test/] strip per-dir prefix: /var/www/vhosts/test/index.html -> index.html
[...] 10.0.2.2 - - [localhost.dev/sid#2532c00][rid#2a44060/subreq] [perdir /var/www/vhosts/test/] applying pattern '^$' to uri 'index.html'
[...] 10.0.2.2 - - [localhost.dev/sid#2532c00][rid#2a44060/subreq] [perdir /var/www/vhosts/test/] pass through /var/www/vhosts/test/index.html
Why is mod_rewrite making sub requests for directory index files index.html and index.php, when the second rule matches the request?
Request for http://localhost.dev/test/test works without any sub request, as expected
[...] 10.0.2.2 - - [localhost.dev/sid#2532c00][rid#2a58b20/initial] [perdir /var/www/vhosts/test/] strip per-dir prefix: /var/www/vhosts/test/test -> test
[...] 10.0.2.2 - - [localhost.dev/sid#2532c00][rid#2a58b20/initial] [perdir /var/www/vhosts/test/] applying pattern '^test$' to uri 'test'
[...] 10.0.2.2 - - [localhost.dev/sid#2532c00][rid#2a58b20/initial] [perdir /var/www/vhosts/test/] rewrite 'test' -> 'test.html'
[...] 10.0.2.2 - - [localhost.dev/sid#2532c00][rid#2a58b20/initial] [perdir /var/www/vhosts/test/] add per-dir prefix: test.html -> /var/www/vhosts/test/test.html
[...] 10.0.2.2 - - [localhost.dev/sid#2532c00][rid#2a58b20/initial] [perdir /var/www/vhosts/test/] strip document_root prefix: /var/www/vhosts/test/test.html -> /test/test.html
[...] 10.0.2.2 - - [localhost.dev/sid#2532c00][rid#2a58b20/initial] [perdir /var/www/vhosts/test/] internal redirect with /test/test.html [INTERNAL REDIRECT]
I have tried using L and END flags, independently and together.
It appears mod_dir does not respect the END flag set by mod_rewrite. For anyone else who is experiencing this, I was able to work around the problem with the following
RewriteEngine on
RewriteRule ^$ /test/home.php [NC,QSA,END,E=IS_DIR_INDEX:1]
FallbackResource /test/fallback.php
<If "env('IS_DIR_INDEX') == 1">
SetHandler default_handler
</If>
I hit the same problem on Apache 2.4.7 on Ubuntu and reported a bug to Apache which was confirmed and is fixed in 2.4.9, see here. For Ubuntu, there is an updated PPA here for easy upgrade. If upgrading is not possible or you don't want to compile the sources, you can use Joyce's workaround above, or the one I posted here which uses only RewriteCond and RewriteRule.

Apache Mod Rewrite does not rewrite file name

i have the following problem:
On the download dialog on each browser it displays me only the dot extension filename
like .divx
But i want the full name eg
http://example.com/14558/.divx rewrites to 14558.divx and not onyly .divx
I want see on every browser dialog the number and the extension.
like 14558.divx
How i can solve this problem ??
RewriteRule ^([0-9]+)/.divx$ $1.divx
RewriteRule ^([0-9]+)/.mkv$ $1.mkv
RewriteRule ^([0-9]+)/.avi$ $1.avi
Current Configuration:
RewriteRule ^([0-9]+)/\.(divx|mkv|avi)$ /$1.$2
Rewrite Log:
127.0.0.1 - - [19/Dec/2011:13:07:02 +0100] [video.local/sid#4518f8][rid#ae5c7c8/initial] (3) [perdir F:/media/video_files/] add path info postfix: F:/media/video_files/29 -> F:/media/video_files/29/.mkv
127.0.0.1 - - [19/Dec/2011:13:07:02 +0100] [video.local/sid#4518f8][rid#ae5c7c8/initial] (3) [perdir F:/media/video_files/] strip per-dir prefix: F:/media/video_files/29/.mkv -> 29/.mkv
127.0.0.1 - - [19/Dec/2011:13:07:02 +0100] [video.local/sid#4518f8][rid#ae5c7c8/initial] (3) [perdir F:/media/video_files/] applying pattern '^([0-9]+)/\.(divx|mkv|avi)$' to uri '29/.mkv'
127.0.0.1 - - [19/Dec/2011:13:07:02 +0100] [video.local/sid#4518f8][rid#ae5c7c8/initial] (2) [perdir F:/media/video_files/] rewrite '29/.mkv' -> '/29.mkv'
127.0.0.1 - - [19/Dec/2011:13:07:02 +0100] [video.local/sid#4518f8][rid#ae5c7c8/initial] (1) [perdir F:/media/video_files/] internal redirect with /29.mkv [INTERNAL REDIRECT]
127.0.0.1 - - [19/Dec/2011:13:07:02 +0100] [video.local/sid#4518f8][rid#ae588f8/initial/redir#1] (3) [perdir F:/media/video_files/] strip per-dir prefix: F:/media/video_files/29.mkv -> 29.mkv
127.0.0.1 - - [19/Dec/2011:13:07:02 +0100] [video.local/sid#4518f8][rid#ae588f8/initial/redir#1] (3) [perdir F:/media/video_files/] applying pattern '^([0-9]+)/\.(divx|mkv|avi)$' to uri '29.mkv'
127.0.0.1 - - [19/Dec/2011:13:07:02 +0100] [video.local/sid#4518f8][rid#ae588f8/initial/redir#1] (1) [perdir F:/media/video_files/] pass through F:/media/video_files/29.mkv
Your rewriterules seem ok... err well maybe it's the backslash. By the way you could optimize in a nicer way (not tested):
RewriteRule ^([0-9]+)/\.(divx|mkv|avi)$ /$1.$2
But the only way to solve your problem (= show the whole filename) is to make a full redirect to a URL that has the full filename.
So in your case redirect would mean these rules:
RewriteRule ^([0-9]+)/\.(divx|mkv|avi)$ $1.$2 [QSA,R=301,L]
And that implies that the URLs like "14558.divx" and "1526.mkv" have to be valid and return the file.
Two hints:
Please try to use the RewriteLog directive: it helps you to track down such problems:
# Trace:
# (!) file gets big quickly, remove in prod environments:
RewriteLog "/web/logs/mywebsite.rewrite.log"
RewriteLogLevel 9
RewriteEngine On
My favorite tool to check for regexp:
http://www.quanetic.com/Regex (don't forget to choose ereg(POSIX) instead of preg(PCRE)!)

Rewrite Url with apache2

I'm experimenting with CodeIgniter PHP framework, this framework works like:
http://localhost:7777/~dhalsim/ci/index.php/blog
So, I tried to remove index.php part from there. So far I do these:
make $config['index_page'] = "index.php"; to $config['index_page'] = "";
make $config['uri_protocol'] = "REQUEST_URI"; from $config['uri_protocol'] = "AUTO";
enable apache mod_rewrite by "a2enmod rewrite"
put a .htaccess file to /ci directory:
RewriteEngine on
RewriteBase /
RewriteCond %{REQUEST_URI} ^system.*
RewriteRule ^(.*)$ /index.php/$1 [L]
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?/$1 [L]
And of course restart apache server
Here is my apache logs with these configurations:
127.0.0.1 - - [17/Jul/2009:02:21:41 +0300] [localhost/sid#7f48e8ad2968][rid#7f48e8e634c8/initial] (3) [perdir /home/dhalsim/public_html/ci/] strip per-dir prefix: /home/dhalsim/public_html/ci/blog -> blog
127.0.0.1 - - [17/Jul/2009:02:21:41 +0300] [localhost/sid#7f48e8ad2968][rid#7f48e8e634c8/initial] (3) [perdir /home/dhalsim/public_html/ci/] applying pattern '^(.*)$' to uri 'blog'
127.0.0.1 - - [17/Jul/2009:02:21:41 +0300] [localhost/sid#7f48e8ad2968][rid#7f48e8e634c8/initial] (4) [perdir /home/dhalsim/public_html/ci/] RewriteCond: input='/~dhalsim/ci/blog' pattern='^system.*' => not-matched
127.0.0.1 - - [17/Jul/2009:02:21:41 +0300] [localhost/sid#7f48e8ad2968][rid#7f48e8e634c8/initial] (3) [perdir /home/dhalsim/public_html/ci/] strip per-dir prefix: /home/dhalsim/public_html/ci/blog -> blog
127.0.0.1 - - [17/Jul/2009:02:21:41 +0300] [localhost/sid#7f48e8ad2968][rid#7f48e8e634c8/initial] (3) [perdir /home/dhalsim/public_html/ci/] applying pattern '^(.*)$' to uri 'blog'
127.0.0.1 - - [17/Jul/2009:02:21:41 +0300] [localhost/sid#7f48e8ad2968][rid#7f48e8e634c8/initial] (4) [perdir /home/dhalsim/public_html/ci/] RewriteCond: input='/home/dhalsim/public_html/ci/blog' pattern='!-f' => matched
127.0.0.1 - - [17/Jul/2009:02:21:41 +0300] [localhost/sid#7f48e8ad2968][rid#7f48e8e634c8/initial] (4) [perdir /home/dhalsim/public_html/ci/] RewriteCond: input='/home/dhalsim/public_html/ci/blog' pattern='!-d' => matched
127.0.0.1 - - [17/Jul/2009:02:21:41 +0300] [localhost/sid#7f48e8ad2968][rid#7f48e8e634c8/initial] (2) [perdir /home/dhalsim/public_html/ci/] rewrite 'blog' -> 'index.php?/blog'
127.0.0.1 - - [17/Jul/2009:02:21:41 +0300] [localhost/sid#7f48e8ad2968][rid#7f48e8e634c8/initial] (3) split uri=index.php?/blog -> uri=index.php, args=/blog
127.0.0.1 - - [17/Jul/2009:02:21:41 +0300] [localhost/sid#7f48e8ad2968][rid#7f48e8e634c8/initial] (3) [perdir /home/dhalsim/public_html/ci/] add per-dir prefix: index.php -> /home/dhalsim/public_html/ci/index.php
127.0.0.1 - - [17/Jul/2009:02:21:41 +0300] [localhost/sid#7f48e8ad2968][rid#7f48e8e634c8/initial] (2) [perdir /home/dhalsim/public_html/ci/] trying to replace prefix /home/dhalsim/public_html/ci/ with /
127.0.0.1 - - [17/Jul/2009:02:21:41 +0300] [localhost/sid#7f48e8ad2968][rid#7f48e8e634c8/initial] (5) strip matching prefix: /home/dhalsim/public_html/ci/index.php -> index.php
127.0.0.1 - - [17/Jul/2009:02:21:41 +0300] [localhost/sid#7f48e8ad2968][rid#7f48e8e634c8/initial] (4) add subst prefix: index.php -> /index.php
127.0.0.1 - - [17/Jul/2009:02:21:41 +0300] [localhost/sid#7f48e8ad2968][rid#7f48e8e634c8/initial] (1) [perdir /home/dhalsim/public_html/ci/] internal redirect with /index.php [INTERNAL REDIRECT]
Here is the result in Firefox:
404 Not Found:
The requested URL /index.php was not found on this server.
So, what should I do (or where am I wrong) to get work these URLs?
http://localhost:7777/~dhalsim/ci/blog/ instead of http://localhost:7777/~dhalsim/ci/index.php/blog/
Here is how i resolve it:
RewriteEngine on
RewriteCond $1 !^(index\.php|images|flash|css|js|robots\.txt|downloads)
RewriteRule ^(.*)$ /index.php/$1 [L]
Then in the config.php file
/*
|--------------------------------------------------------------------------
| Base Site URL
|--------------------------------------------------------------------------
|
| URL to your CodeIgniter root. Typically this will be your base URL,
| WITH a trailing slash:
|
| http://example.com/
|
*/
$config['base_url'] = "http://localhost:7777/~dhalsim/ci/";
/*
|--------------------------------------------------------------------------
| Index File
|--------------------------------------------------------------------------
|
| Typically this will be your index.php file, unless you've renamed it to
| something else. If you are using mod_rewrite to remove the page set this
| variable so that it is blank.
|
*/
$config['index_page'] = "";
UPDATE
In the routes.php file:
//Point to default controller
$route['default_controller'] = "site/home"; //Set to your controller
// Point to the right controller
$routing_array = array(
'blog' => 'site/blog',
); // set to your blog controler
Use the following Rewrite Rule:
RewriteEngine On
RewriteCond %{REQUEST_FILENAME} -s [OR]
RewriteCond %{REQUEST_FILENAME} -l [OR]
RewriteCond %{REQUEST_FILENAME} -d
RewriteRule ^.*$ - [NC,L]
RewriteRule ^(.*)$ /index.php/$1 [NC,L]
In order for this to work you should setup your CodeIgniter app on a different port or use /etc/hosts to give it a simple Vhost (127.0.0.1 ci and http://ci:7777/ with Apache set to use the ci vhost.). Then you can set the base url to / and not worry about it. Otherwise you're going to have to deal with confusing directory issues and mod_rewrite.