PrettyUrls and modRewrite

Can someone explicitly explain in great detail why it is sometimes necessary to add the following question mark:

.htaccess

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d

# try ONLY ONE of the following RewriteRules:
RewriteRule ^(.*)$ index.php/$1 [L]
# or maybe this one
RewriteRule ^(.*)$ index.php?/$1 [L]

Edit:
@WebMachine recently raised the question in the following topic

That second rule is odd, because what you’d end up is this:

// original URL
www.example.com/contact-us

// rewritten URL
/index.php?contact-us

Which, in PHP, would give you:

var_dump($_GET);

// output
array (size=1)
  'contact-us' => string '' (length=0)

Perhaps that’s just the way CI handles things, but I’ve never come across it done quite like that before.

The reason for doing it, according to this SO thread, is because PHP is running under the CGI handler, but I’ve not been able to find a more detailed explanation.

2 Likes

Both,

Actually, BOTH RewirteRules are problematic albeit for different reasons.

The first is redirecting to $1 in the (hopefully non-existant) index.php subdirectory … unless you have the horrible (IMHO) Options +MultiVViews set. Just what are the chances that random input will hit a file there?

The second badly abuses the $_GET array by sending a / as the first character in the key variable. What is that supposed to do (orher than cause an error)?

Learn how to configure and use mod_rewrite (with sample code) at http://dk.co.nz/seo

Regards,

DK

1 Like

@dklynn

Many thanks for the link, I will certainly spend some time studying your web-page. I had a brief look and noticed the reference to httpd.conf, I believe that file is now redundant and apache2.conf is used instead.

I spent some more time researching the problem and from Ellislab’s - ExpressionEngine® User Guide

Some common problems include links returning 404s, a “No Input File Specified” error, or all links returning the same content. This can often be the case with hosts (such as GoDaddy) that require you to force query_strings. If this happens, follow the exception above for hosts that force query strings.

I used to use Godaddy and experienced the redirection problem. :frowning:

@fretburner

Many thanks for the link, etc.

CodeIgniter loads a default URI Class that is very useful.

The link: “www.example.com/contact-us/this-is-a/TEST/to-see-if-it-works

can be accessed in Controllers, Models and Views by:
echo $this->uri->segment(1, ‘DEFAULT’); // 'contact-us
echo $this->uri->segment(2, ‘DEFAULT’); // this-is-a
echo $this->uri->segment(3, ‘DEFAULT’); // 'TEST
echo $this->uri->segment(4, ‘DEFAULT’); // to-see-if-it-works
echo $this->uri->segment(5, ‘DEFAULT’); // 'DEFAULT

Also lots of other invaluable methods which I find invaluable.

Hi J_B!

[quote=“John_Betong, post:4, topic:207571, full:true”]
@dklynn

Many thanks for the link, I will certainly spend some time studying your web-page. I had a brief look and noticed the reference to httpd.conf, I believe that file is now redundant and apache2.conf is used instead.[/quote]

You are absolutely correct. I use Apache on my test server (WinDoze 7) as well as hosted (using a Unix variant) so, while I’m more familiar with setting up and running on WinDoze, anyone not knowing (or being able to find apache2.conf) probably shouldn’t be trying to change its contents as even a minor error in the configuration file can disable the web server.

My bad ( :smirk: ) for not stating that before, though.

Regards,

DK

1 Like

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.