Forum Moderators: phranque

Message Too Old, No Replies

Rewrite NOT working on new server

         

J0kerz

7:29 pm on Oct 22, 2010 (gmt 0)

10+ Year Member



Hi there,

I just moved from one server to another and the code in my .htaccess doesnt seem to work on the new server.

Here is the code that is used to redirect every url to the homepage.

RewriteEngine On
RewriteBase /
RewriteRule ^(.+)\.php$ / [R=302,L]


The page just dont load with such code in the .htacces file

Is there anything I need to setup to get this code to work on my new server?

Thanks guys!

g1smd

7:36 pm on Oct 22, 2010 (gmt 0)

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



You'll need a preceding negative-match RewriteCond that doesn't match index.php or whatever the PHP script is called, OR you'll need a RewriteCond that checks that the .php part was present in the incoming THE_REQUEST arriving from the browser.

It is likely that you currently have an infinite redirect loop. This happens when the "/" is resolved to "index.php" which then rematches the redirect pattern again. A quick look with "Live HTTP Headers" will confirm the infinite loop, or not.

That particular Firefox extension should be considered "basic webmaster kit" as it quickly shows what the browser asked for and what the server sent back in response.

J0kerz

7:48 pm on Oct 22, 2010 (gmt 0)

10+ Year Member



Thanks g1smd! It is infact an ifinitive loop.

What can I add to my code to correct this issue?

sublime1

7:51 pm on Oct 22, 2010 (gmt 0)

10+ Year Member



Your code redirects every url to the homepage ... even the home page.

Add a RewriteCond that tests %{REQUEST_URL} -- if it's already "/" then you don't need to process the request.

J0kerz

8:04 pm on Oct 22, 2010 (gmt 0)

10+ Year Member



Like that?

RewriteEngine On
RewriteBase /
RewriteCond %{REQUEST_URI} !^\$ [NC]
RewriteRule ^(.+)\.php$ / [R=302,L]

jdMorgan

8:10 pm on Oct 22, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



If the DirectoryIndex declaration in this server's configuration defines index.php as a possible directory index page, then using the method testing THE_REQUEST will be required, as g1smd notes above.

RewriteEngine On
RewriteBase /
#
RewriteCond %{THE_REQUEST} ^[A-Z]+\ /([^/]+/)*([^.]+\.)+php([?#][^\ ]*)?\ HTTP/
RewriteRule ^.+\.php$ http://www.example.com/ [R=301,L]

Jim

J0kerz

8:24 pm on Oct 22, 2010 (gmt 0)

10+ Year Member



Thanks alot guys! I really appreciate it!

g1smd

9:00 pm on Oct 22, 2010 (gmt 0)

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



Yes, I unfortunately missed a few words out of my reply...

This happens when the "/" is resolved to "index.php" by the DirectoryIndex directive which then rematches the redirect pattern again.

sublime1

10:21 pm on Oct 22, 2010 (gmt 0)

10+ Year Member



Hi @g1smd and @jdMorgan

So two things. I messed up on my first response (it was a typo) -- should have been REQUEST_URI (not URL). I am not worthy :-{

Second thing: what is it that THE_REQUEST has that REQUEST_URI doesn't -- I assume the URI includes the query string and other path elements. Do I assume wrong?

Tom

g1smd

10:37 pm on Oct 22, 2010 (gmt 0)

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



REQUEST_URI initially contains "/" but then resolving the "DirectoryIndex index.php" directive changes the value to "index.php" instead. The value is updated to show what it will resolve to.

THE_REQUEST contains the literal request arriving from the browser, viz. "GET / HTTP/1.1" or similar. Subsequent rules do not modify the information. You can test it to see exactly what was asked for.

sublime1

12:24 am on Oct 23, 2010 (gmt 0)

10+ Year Member



Wow.

That's utterly lame. Although I suppose that in least in the case of REQUEST_URI it's consistent with the target of RewriteRule, which I guess has the same value, and semantics.

I suppose it would have been too much to call the other variable THE_ORIGINAL_REQUEST, or perhaps the more obtuse and apache-like THE_IDEMPOTENT_REQUEST :-)

The more I learn about .htaccess variants of mod_rewrite, the more of a hack it looks -- I guess I have been living in the little paradise of server/vhost based rewrites, where things work more simply, as documented, and as expected. And the doc completely fails to mention this little, um, detail, as far as I can see.

The more I learn, the more I understand how little I know. And also, the more I can bill per hour for this crud :-)

Thanks -- I'll send you a monthly tithe for all your help :-)

Tom

g1smd

6:37 am on Oct 23, 2010 (gmt 0)

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



Mod_Rewrite is the swiss army knife of server configuration. It is fast, compact, extremely versatile, very powerful, but as you note the documentation is terse and not that easy to understand. Every little detail is important, and there are several places where it is not at all clear what is meant.