Forum Moderators: phranque

Message Too Old, No Replies

RewriteRule (redirecting) trouble

rewriterule,redirect

         

integrable

9:16 pm on Dec 13, 2009 (gmt 0)

10+ Year Member



hi,

i have next .htaccess file:

RewriteEngine On
RewriteRule ^.*$ index.php [NC,L]

and it works fine on localhost, server redirects all urls to index.php

BUT, it's does not work on another server, it gives 404 error
for all urls except this index.php, and does not make redirect to this adress from other urls.

What can be the reason for such a behaviour?

Thanks.

maximillianos

11:24 pm on Dec 13, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Does your webserver support redirects?

g1smd

12:45 am on Dec 14, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



That code is for a rewrite, not for a redirect. It shouldn't redirect the client. If it does, some other rule is at work.

However, what it does do is rewrite all URL requests, including those for images, CSS files, robots.txt, etc, and feed those requests to the script. That is very dangerous as it cannot process most of those requests. It also allows infinite duplicate content.

If you are wanting a redirect, then the target should be the URL "www.example.com/" and not a named index file. Such a redirect should include the domain name and the R=301 flag. That redirect should only happen for very specific URL requests (e.g. only for URL requests ending in .html for example) and should not happen for images, CSS files, robots.txt etc, and should only happen for direct client requests, not as the result of any internal rewriting.

integrable

1:31 am on Dec 14, 2009 (gmt 0)

10+ Year Member



Thanks, g1smd.
I changed this to:

RewriteCond %{REQUEST_FILENAME} !(^.*/index\.php$)
RewriteRule ^.*$ index.php [NC,L]

Now it's ok.

jdMorgan

2:01 am on Dec 14, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



It's likely that mod_rewrite isn't enabled with the required Options setting on the server. If it were, your rule would result in an 'infinite' rewriting loop (and you'd see 500-Server Errors in your server error log).

You likely need to set the FollowSymLinks or SymLinksIfOwnerMatch option on the server.

Also, you need to exclude index.php from being rewritten to itself, and you very likely should also exclude several 'standard' files and media filetypes from being rewritten to your script. The most efficient way is to specify them:


Options +FollowSymLinks -MultiViews
RewriteEngine on
#
RewriteCond $1 !^(index\.php¦robots\.txt¦sitemap\.xml)$
RewriteCond $1 !\.(gif¦jpe?g¦png¦ico¦css¦js)
RewriteRule ^(.*)$ index.php [NC,L]

This is just example code. You *must* include the RewriteCond to prevent index.php from rewriting to itself. The other exclusions are just examples, and the correct code depends on your actual needs.

If you use this concept, be sure to replace all broken pipe "¦" characters with solid pipe characters before use; Posting on this forum modifies the pipe characters.

[added] I started writing this reply before you posted above. While you've taken care of the "index.php" looping problem, do be sure to think about whether you really want your script to handle requests for images, css, JS, and for robots.txt and sitemap.xml, etc. Usually, these requests are not handled by scripts, because it's easier and more efficient to by-pass the script for 'real file' requests or -- thinking about it another way, for non-HTML-page requests. [/added]

Jim

integrable

5:12 pm on Dec 14, 2009 (gmt 0)

10+ Year Member



thanks jdMorgan

I know about rewritig rules for css, images etc. That's ok, my code was just an example.