Welcome Guest, Not a member yet? Register   Sign In
Using .htaccess to strip index.php (not just internally redirect)
#1

[eluser]Scott Rabin[/eluser]
Hi all;

I recently discovered CodeIgniter and would like to start using it for one of my friend's webpages. It was easy to redirect pages to ./index.php/rest/of/path for requests of ./rest/of/path, but my question is how to force redirect ./index.php/rest/of/path to appear as ./rest/of/path and then get <i>internally</i> redirected? This is my current .htaccess file:

Code:
RewriteEngine On
RewriteBase /abc

#RewriteCond %{REQUEST_URI} ^index\.php
RewriteRule ^index\.php(/)?(.*)$ ./$2 [R]

#RewriteCond %{REQUEST_FILENAME} !-f
#RewriteCond %{REQUEST_FILENAME} !-d
#RewriteCond $1 !^(index\.php|images|css|js|robots\.txt|favicon\.ico)
#RewriteRule ^(.*)$ ./index.php/$1 [L]

I can't seem to get the first RewriteRule to only trigger when the preceding RewriteCond is uncommented; I've tried a number of variations and can't seem to get it to work.

The reason that first RewriteCond is there is to prevent an infinite loop when the second RewriteRule is uncommented. For whatever reason, the [L] flag seems to be ignored here - with or without that flag, it seems to hit an infinite loop.

Again, the goal is to strip any URLs containing index.php on the user side, but redirect internally to the appropriate URL.

I'm running a WAMP server (XP SP3, Apache 2.2.11, MySQL 5.1, PHP 5.2.9) off my media computer. CodeIgniter is installed in the /abc directory of my document root.

Thanks!
#2

[eluser]TheFuzzy0ne[/eluser]
Welcome to the CodeIgniter forums!

The first rewrite condition will always be true as the start of your request URI is always "index.php". Also, the path you're redirecting to is relative to index.php. What are you trying to achieve? I've read your post, but sorry, I just don't get it.
#3

[eluser]Scott Rabin[/eluser]
Sorry - the .htaccess I posted is a mess resulting from me messing around with it in frustration.

What I want to do is force user's browsers to not have index.php in them, by including the [R] flag in the first one (it redirects URLs like domain.com/path/index.php/controller to domain.com/path/controller in the user's address bar). This should stop the first rule from triggering after the redirect, since it no longer has index.php in the URI.

However, to make CodeIgniter work, it has to point domain.com/path/controller to domain.com/path/index.php/controller. I understand that this could cause an infinite loop, but I thought including the [L] flag in the RewriteRule would make it stop rewriting the URL after that triggers. Also, I figured that since this rule occurs after the index.php stripping rule, it wouldn't continue to rewrite.

I'm redirecting relative to index.php because CodeIgniter is not installed in the root directory of either my own development server or the web host. That way, it points to the root directory of wherever index.php is located (since this .htaccess file is in the same directory). The first rewrite rule works just fine for rewriting the index.php out of the URL; but an infinite loop is caused if I try to point the resulting address (domain.com/path/controller) to the index.php one (domain.com/path/index.php/controller).

The point of this is to actively strip index.php from the browser's address bars, and force clean urls like domain.com/path/controller. It's simply an aesthetic reason; domain.com/path/index.php/controller looks strange for a URL and it would be nice to be able to remove it.

I've only recently started using .htaccess with mod_rewrite. If there's something I'm not understanding about the way the [R] and [L] flags work, let me know.
#4

[eluser]TheFuzzy0ne[/eluser]
You've set the RewriteBase to /abc so the redirect will work relative to that directory anyway, so the './' is not necessary.

This should work a bit better:
Code:
RewriteRule ^index\.php/?(.*)$ $1 [R]

The top RewriteCond should not be necessary, as on the second pass, the RewriteRule no longer matches (as there's no index.php in the URL.
#5

[eluser]Scott Rabin[/eluser]
RewriteBase must've been a leftover from when I was playing with absolute pathing, and it didn't work (did a little research and figured that one out).

It strips index.php from the URL just fine, but if I uncomment the second RewriteRule to generate the proper CodeIgniter-safe URL, Firefox complains about an infinite loop. The question I have is how to make it stop doing that; I have a good grasp on Regex and was confused as to why the first RewriteRule kept triggering. After the redirect, it doesn't match, as you say, but after the second RewriteRule hits, it loops. Does it keep going through the .htaccess until no RewriteRules apply? I thought it ended if there was no explicit [R]edirect flag and it got to EOF.
#6

[eluser]TheFuzzy0ne[/eluser]
Hmmm. I guess my knowledge of htaccess wasn't as good as I'd thought. Let me run some tests and see if I can get it sorted for you.
#7

[eluser]TheFuzzy0ne[/eluser]
Poop... I admit defeat. It seems that I can't even perform the most trivial task with mod_rewrite...

htaccess: 2 - thefuzzy0ne: nil...
#8

[eluser]Unknown[/eluser]
Hi

Good day to all developers, I have trouble implementing a redirect on htaccess. I have setup a server (Ubuntu) with apache2. I have set the virtual host to read .htaccess file. I got trouble when someone visits my domain with out the main controller (domain.com/Home) they got a 404 page. Where as if they visit with the main controller, the page will properly display.

Here is my htaccess code:

<IfModule mod_rewrite.c>
Options +FollowSymlinks
RewriteEngine On
RewriteBase /

#Checks to see if the user is attempting to access a valid file,
#such as an image or css document, if this isn't true it sends the
#request to index.php
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php?/$1 [L]
</IfModule>

<IfModule !mod_rewrite.c>
# If we don't have mod_rewrite installed, all 404's
# can be sent to index.php, and everything works as normal.
# Submitted by: ElliotHaughin

ErrorDocument 404 /index.php
</IfModule>

Is there something wrong with my htaccess? Please advise




Theme © iAndrew 2016 - Forum software by © MyBB