Htaccess not working on new hosting

New to mod_rewrite? This is a good place to start.

Htaccess not working on new hosting

Postby OMT » Mon Apr 20, 2009 12:59 pm

Hello,

probably someone else have posted similar thread here, but I was unable to find it, so if that is the case, just show it to me.

I've made a website using a framework called Zoop (some of you may already know it). It has the nasty habit of putting an "index.php" in the middle of all the URLs, so a classic link of it looks like that:

http://somesite.com/index.php/something/something2

Fortunately, the developers are aware of the total SEO-unfriendlyness of this style, and provided an htaccess file, so you can get rid of the index.php. The code of the htaccess is simple, here it is:
Code: Select all
  RewriteEngine on

  RewriteBase /

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


I have an Apache1.3 on my PC, which I use as a developing environment. All went well there, and links were made to work the way they were supposed to work. But when I uploaded the website to an Apache2.2.11 server with a shared hosting, the htaccess stopped working. Mod_rewrite was enabled, as the support told me, and it really did worked (returned an "Error 500" and also made some simple redirects, which I created to test whether it is working). But the rewrite rule was not. Since the file itself was affecting the way the server was working, it was readable and all. I uploaded the site to another shared hosting, also with Apache2.2.11. There it was in a separate directory, which made me use the RewriteBase. But the effect was the same - no pretty URLs.

So, what is wrong? Why it worked on an Apache1.3, but refuses to start on Apache2.2? Been some weeks since I'm trying to resolve that, an on several forums there was no response, so I'm getting kinda desperate.

Thank you all!
OMT
 
Posts: 8
Joined: Mon Apr 20, 2009 12:41 pm

Postby richardk » Mon Apr 20, 2009 2:27 pm

Does this mod_rewrite test work?

returned an "Error 500"

What does your error log say is causing the error?

Try
Code: Select all
Options +FollowSymLinks

RewriteEngine On

RewriteCond %{ENV:REDIRECT_STATUS} ^$
RewriteCond %{SCRIPT_FILENAME} !-f
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteRule ^(.+)$ /index.php/$1 [QSA,L]
richardk
 
Posts: 8800
Joined: Wed Dec 21, 2005 7:50 am

Postby OMT » Mon Apr 20, 2009 3:19 pm

Usually it says that it cannot find a directory (that was when I was experimenting with RewriteBase). Most of the time the log was saying nothing at all after such an error (RewriteLog is forbidden for some reason, and that makes it really hard to debug the problem).

Edit: the test you gave is working, I tried it first of all. Just forgot to mention it at my first post :)
OMT
 
Posts: 8
Joined: Mon Apr 20, 2009 12:41 pm

Postby OMT » Tue Apr 21, 2009 12:03 am

Sorry for the delay, I live in another time zone.

So, I tried the code you gave me:
Code: Select all
Options +FollowSymLinks

RewriteEngine On

RewriteCond %{ENV:REDIRECT_STATUS} ^$
RewriteCond %{SCRIPT_FILENAME} !-f
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteRule ^(.+)$ /index.php/$1 [QSA,L]


but it isn't working too. It does not return an error, it just makes the site to redirect to it's homepage. Same thing was happening with my htaccess. That is a normal behaviour of the framework - when it is passed an URL that it cannot link to something it recognises, it redirects the user to the homepage. When it was on the Apache1.3 it was also doing that before I made the correct htaccess. After that it was working OK. So it does 4 things at all - either Error 404 or 500, a redirect, or opening the correct page depending on what is into the htaccess.

Edit: when I added the full URL to the RewriteRule:

Code: Select all
RewriteRule ^(.+)$ http://site.com/index.php/$1 [QSA,L]


it started to add index.php even if it is omitted. What I need is pretty much the opposite of that. Maybe we're on to something here...
OMT
 
Posts: 8
Joined: Mon Apr 20, 2009 12:41 pm

Postby richardk » Tue Apr 21, 2009 8:27 am

It does not return an error, it just makes the site to redirect to it's homepage. Same thing was happening with my htaccess. That is a normal behaviour of the framework - when it is passed an URL that it cannot link to something it recognises, it redirects the user to the homepage.

That means that the variable the framework uses to get/check the URL is incorrectly set because of the mod_rewrite.

What variable does it use? Probably PATH_INFO or REQUEST_URI.
What value should the variable have?
What variable does the variable have?
richardk
 
Posts: 8800
Joined: Wed Dec 21, 2005 7:50 am

Postby OMT » Tue Apr 21, 2009 12:18 pm

I spent some time looking at the code, and there are check-ups on the variables indeed, mainly put for injection protection. But the same code which is making trouble on the new hosting is an exact copy of the one with a working mod_rewrite rules on the old one. So it shouldn't be the code by itself causing this problems. A friend of mine also spent some time on the code and found nothing to be interfering in that way with the URLs. My opinion is that it is server-related.

A little info on the values:
The first thing after the "index.php" is the name of the so called "zone" - that is a page in php, which is supposed to crunch and display the data.

The second, third and so far arguments are variables. They are used to pass values to variables in the code of the zone in use. For example I transmit in this way the page number or the search query for the search engine.

To summarise:

http://somesite.com/index.php/zone/vari ... riable_two

It appeared that when I made the redirect-like (although there was not a real redirect anywhere in the htaccess) thing from my previous post, only the affected pages were able to display. All the other ones went haywire. Yet again I would use this thing with a bunch of a conditional statements, 'cause it is useful effect. The only problem is that after submitting that query it has the old URL format in the URL field of the browser.

So, if there is a way to change what is shown in the URL field along with this "redirect" effect, I will consider my problem solved :)
OMT
 
Posts: 8
Joined: Mon Apr 20, 2009 12:41 pm

Postby richardk » Tue Apr 21, 2009 4:48 pm

With the following mod_rewrite
Code: Select all
Options +FollowSymLinks

RewriteEngine On

RewriteCond %{ENV:REDIRECT_STATUS} ^$
RewriteCond %{SCRIPT_FILENAME} !-f
RewriteCond %{SCRIPT_FILENAME} !-d
RewriteRule ^(.+)$ /index.php/$1 [QSA,L]

when you visit /zone/variable1/variable2/ you are redirected to you homepage (/) by your script. Correct? This is most likely because it is expecting /index.php to be at the beginning of the URL. The fact that /index.php is being processed shows the mod_rewrite is working.

My opinion is that it is server-related.

Yeah. The two different servers (, mod_rewrite versions and/or PHP versions) set the variable differently. Even if the fix is a server fix, you still need to know what variable is wrong.

(although there was not a real redirect anywhere in the htaccess)

Using http:// is a RewriteRule substitution usually causes a redircet.
richardk
 
Posts: 8800
Joined: Wed Dec 21, 2005 7:50 am

Postby OMT » Wed Apr 22, 2009 12:45 am

The variables themselves are correct, since they are generated by PHP scripts. Also I implemented some check-ups in case someone starts to shove random data, and if the variable is not like the expected (for example, a letter for page number), it is substituted with a default one as a fail-safe.

Since the new server is not set-up by me, I cannot know what crud is put into the httpd.conf. I wrote to the support for information on the subject of "What the heck is wrong with this thing?", and they told me that it is not their job to tell me such stuff, and that I must "play with the htaccess" to obtain the correct settings. Talking about some support, heh?

I cannot change the hosting, 'cause the guys I'm making the site for insist on using this one.

I can provide information with versions - the server is Apache2.2.11 on Unix, the PHP is 5.2.9 (and broke my admin panel of the site, so last week there was an emergency patch-up...).

The server, where everything was working OK, was Apache1.3.41 on Linux, the PHP is 5.2.6. This is actually my private server at home, where I test stuff before uploading to a shared hosting. It is not used as a permanent hosting and is not intended to be one.
OMT
 
Posts: 8
Joined: Mon Apr 20, 2009 12:41 pm

Postby richardk » Wed Apr 22, 2009 4:43 am

he variables themselves are correct, since they are generated by PHP scripts.

I'm not talking about your variables in the URL. I'm talking about REQUEST_URI and PATH_INFO in the $_SERVER array, etc.. They are not set by you, they can be affected by Apache and PHP.

Please print_r() the $GLOBALS array and look for differences.
richardk
 
Posts: 8800
Joined: Wed Dec 21, 2005 7:50 am

Postby OMT » Wed Apr 22, 2009 11:29 am

I'm not talking about your variables in the URL. I'm talking about REQUEST_URI and PATH_INFO in the $_SERVER array, etc.. They are not set by you, they can be affected by Apache and PHP.

Please print_r() the $GLOBALS array and look for differences.


Sorry for the misunderstanding. So, the difference between the ones on the current hosting and the ones on the old one (with the working mod_rewrite) are that on the new one the REQUEST_URI has "index.php" at it's beginning. Everything else after it is the same. The PATH_INFOs are identical - both are missing the "index.php" and start with the zone name.
OMT
 
Posts: 8
Joined: Mon Apr 20, 2009 12:41 pm

Next

Return to Beginner's Corner

Who is online

Users browsing this forum: No registered users and 16 guests

cron