1

I'm trying to catch every URL and rewrite it to index.php. I have the following code in my .htaccess right now:

RewriteEngine on
RewriteRule .* index.php

It does rewrite every URL, but I always get a 404 saying the index.php file cannot be found. The path it is looking for the file is correct though. What am I doing wrong here?


Update 1

When I directly browse to index.php, it does correctly display the file. Very strange.


Update 2

I turned on logging for mod_rewrite using this in httpd.conf:

RewriteLog /var/log/apache2/rewrite.log
RewriteLevel 3

This is what it logged:

strip per-dir prefix: /Users/rits/Sites/test/ -> 
applying pattern '.*' to uri ''
rewrite '' -> 'index.php'
add per-dir prefix: index.php -> /Users/rits/Sites/test/index.php
internal redirect with /Users/rits/Sites/test/index.php [INTERNAL REDIRECT]

4 Answers 4

1

I'm fairly sure this is because it is trying to rewrite index.php to index.php infinitely.

Try the following instead:

RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !index\.php
RewriteRule .* index.php

That should prevent rewriting to itself, and hopefully fix your problem.

Sign up to request clarification or add additional context in comments.

1 Comment

Hmm, okay. I'm not really sure what could be causing the problem, then. I'm used to redirect errors causing HTTP 500 errors, not 404. Erm... What happens if you try this: RewriteRule ^(.*)$ index.php?file=$1?
0

This is the .htaccess from one of my websites that I set up to redirect everything to the index page and it works for me, if you'd like to try it:

RewriteEngine on
RewriteOptions MaxRedirects=10
RewriteBase /
RewriteCond %{REQUEST_FILENAME} !index\.php
RewriteRule ^.*$ http://www.mywebpage.com/ [R=301,L]

Comments

0

Try this:

RewriteEngine on
RewriteRule ^(.*)$ http://www.yourwebpage.com/$1 [L,R=301]

Comments

0

This is the content of .htaccess file I have and works:

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.