Why Apache mod_rewrite doesn't behave as expected

I want to redirect urls from an old site that was using raw url requests to my new site that I have implemented in CodeIgniter. I just want to redirect them to my index page. I would also like to get rid of the "index.php" in my urls so that my urls are as simple as example.com/this/that. So this is the file .htaccess

I created:

RewriteEngine on
Options FollowSymLinks

RewriteBase /

RewriteCond $1 ^assets
RewriteRule ^(.*)$ example/production/$1

RewriteCond %{QUERY_STRING} .+
RewriteRule ^(.*)$ index.php? [R=301]

RewriteCond $1  !^(index\.php|example|robots\.txt)
RewriteRule ^(.*)$ index.php/$1 

      

It should also be noted that my index.php is actually a symbolic link to example/production/index.php

.

Now the first rule works as expected - all my styles and images render very well, these are the second two rules that I'm having trouble with. The second rule is to destroy the query string and redirect to my index page (from the outside). So, I found this in the Apache manual:

Note: Query string

The pattern will not match the query string. Instead, you should use RewriteCond with% {QUERY_STRING} variable. However, you can create URLs in the lookup string that contain part of the query string. Just use a question mark inside a wildcard string to indicate that the following text should be re-entered into the query string. If you want to delete an existing query string, end the substitution string with just a question mark. To merge the new query string with the old one, use the [QSA] flag.

However, when I try to access one of the old pages, instead of being redirected to my index page, I get a 404 error. I figured out a workaround by making it an internal redirect, but I'd really like it to be external.

The next problem, and the one that confused me the most, has to do with the third rule. I would expect this to do something like the following. If I enter:

http://example.com/this/thing

      

I would expect it to move to

http://example.com/index.php/this/thing

      

Unfortunately this doesn't work. Instead, no matter what I type, it always gets redirected to my index page as if there was nothing in the url (it just goes to http://example.com/

).

Also, and even more confusing for me if I replace this rule with the following:

RewriteCond $1 !^(index\.php|example|robots\.txt)
RewriteRule ^(.*)$ index.php/this/thing

      

If I type in the url, for example http://example.com/other/thing

, then it goes to http://example.com/index.php/this/thing

as expected, but if I type http://example.com/this/thing

it goes to http://example.com/

(my index page). I cannot make heads or tails out of it. Any help would be greatly appreciated.

0


source to share


2 answers


This should solve your index.php problem and it will simply detect if a robots.txt file is available:



RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule . /index.php [L]

      

+1


source


hmmm - that doesn't work either. The problem is that my urls don't actually ask for a file name or directory. For example: example.com/index.php/this/thing should call the "thing" method of the 'this' controller. - Stephen Oxley

Condition: If the request is NOT a file and NOT a directory, so that was correct, then what you should have done is concatenate the addition of the query string:



RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ index.php/$1 [L]

      

+1


source







All Articles