Forum Moderators: phranque

Message Too Old, No Replies

Is this a Mod_rewrite problem or a scripting problem?

Not sure what is causing this problem when trying to use mod_rewrite

         

Trisha

10:48 pm on Jan 11, 2005 (gmt 0)

10+ Year Member



I'm in the process of moving a site to a new domain and host (WestHost). The mod_rewrite is working on the old site, but not on the new host/domain. There is no reason why anything on the .htaccess should need to be changed.

It is written like this (I realize the rewrite could probably be written better, but this should work.):

RewriteRule ^(.*)widgets/gadgets/(.*)$ $1widgets/article.php?gadget=$2

The rewritten url should look like:
h ttp://www.mydomain.com/widgets/gadgets/wodget

When I hold my cursor over the rewritten link, it looks like it should in the status bar. When I click on the link, I can tell the browser is trying to do something. The url in the address bar even changes to the correctly rewritten url. But, the page that loads is the same one that I was just on - the one with the rewritten link.

If however I type in:

h ttp://www.mydomain.com/widgets/article.php?gadget=wodget

the page comes up correctly, so everything is ok with the database and the scripting to pull out the right article.

Any idea what the problem might be? Is it the mod_rewrite or something else? Does it have anything to do with the way mod_rewrite is set up on WestHost that might be different from the old host?

jdMorgan

11:26 pm on Jan 11, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



The first question to answer in cases like this is, "Do *any* rewrites work?"

I like to start with something like:


RewriteRule ^silly\.html$ /index.html [L]

Where "silly.html" is a file that does not exist, and "index.html" is a file that does exist.

To test, you request silly.html with your browser, and you should get the contents of index.html.

If the answer is no, then you'll have to investigate differences in server configuration. In many cases, the answer is simply that one of the following two lines is missing from the beginning of the code:


Options +FollowSymLinks
RewriteEngine on

These may have been configured "globally" on your old server, but are not on your new, so you have to set them yourself.

Your error log should provide some information about what the problem is.

Jim

Trisha

11:41 pm on Jan 11, 2005 (gmt 0)

10+ Year Member



I should have added, I do start out the htaccess file with:

RewriteEngine on
Options +FollowSymlinks
RewriteBase /

jdMorgan

1:38 am on Jan 12, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Hmmm...

Take a look at your error log. Your statement, "When I click on the link, I can tell the browser is trying to do something. The url in the address bar even changes to the correctly rewritten url" leads me to believe that you've got a redirection loop involved here, but perhaps due to the examplified rewrite parameters you posted, I can't spot it.

You must arrange the rewritten directory and file names such that a rewritten URL will not match the pattern in the RewriteRule; Otherwise, the URL will be rewritten again, leading to a loop. There is also the possibility that another RewriteRule may be invoked after the first Rewrite, and that the two are "ping-ponging," rewriting the URL back and forth until your server redirection limit is reached. Remember that mod_rewrite in an .htaccess context is recursive, and that all RewriteRules should have a [L] flag unless you have a specific reason to omit it. In either case, the error log will help identify the specific error.

Jim

Trisha

2:15 am on Jan 12, 2005 (gmt 0)

10+ Year Member



Take a look at your error log.

I should probably do that, but I haven't even had a chance yet to see how/where to download the server logs.

Meanwhile - I was concerned about a possible looping thing too, but that would be so weird as that same mod_rewrite is still working just fine on the old site. I'm not using the [L] flag, I set this up a long time ago, I can add that though.

I think I may to just give up on this, at least temporarily, and set up the site all static, until I have more time to work on it.

I was really hoping it wouldn't be that hard to move the site, I guess I was wrong.

Trisha

2:37 am on Jan 12, 2005 (gmt 0)

10+ Year Member



The more I look at this, I just can't see why what I had before won't work, unless mod_rewrite is somehow set up differently on their servers. Not that I'm an expert at this and I know I can make mistakes, but this is the same sort of rewrite I've used on lots of other sites too, and it works.

Here is what I use without the widgets, I don't think I'm revealing anything that would break the tos:

RewriteRule ^(.*)m-biology/concepts/(.*)$ $1m-biology/article_concepts.php?concepts=$2

The only other thing I can think of is the following that I have on the article_concepts.php file. But it is not going to the site index anyway, so I don't think that is causing it.

if (!$concepts) {

header("location:http://www.mydomain.com");

exit;

Trisha

3:28 am on Jan 12, 2005 (gmt 0)

10+ Year Member



I've just heard that westhost uses Apache 1.3, I don't know what my current host uses, but maybe it is different and that is part of the problem.

jdMorgan

3:40 am on Jan 12, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Taken by itself, your mod_rewrite code is OK, even if not optimal. The problem is almost certainly a difference in the default server configuration between your old and new hosts.

The additional code you posted apparently does a 302 external redirect. That could be interfering, but I'm not sure what that code does in the scheme of your site.

We can guess and post all day, but it really will be much more efficient to look at the host's FAQ and find out where your error log is -- or just poke around the top level of your site using FTP and see if the log files are in a folder there. Trying to debug this without the log info will probably be difficult.

Jim

Trisha

3:54 am on Jan 12, 2005 (gmt 0)

10+ Year Member



even if not optimal

yeah, I've since learned how to do it better than when I first set this up. I thought it would be best to get it working how it was before, before I change anything.

The additional code

that is in case someone tried to go straight to the file article_concepts.php when a variable hasn't been passed to it.

I think the problem may be related to the host using Apache 1.3, and my other host likely used a newer version. I need to find out what the differences are between them.

Trisha

6:08 am on Jan 12, 2005 (gmt 0)

10+ Year Member



Turns out my old host runs 1.3.xx too. Tomorrow I will look up the log files.

Trisha

5:35 pm on Jan 12, 2005 (gmt 0)

10+ Year Member



Jim - I looked at the error log - nothing is in there related to this. It isn't creating an error! What does that mean? That it is a looping thing? Still, it's weird though, it worked before.

Trisha

9:43 pm on Jan 13, 2005 (gmt 0)

10+ Year Member



update:

jdMorgan figured out what the problem was! (Thanks again Jim!)

It turned out to be that westhost has MultiViews defaulted to On, while my old host had it defaulted to OFF.

I had to change the first part of the htaccess file to:

Options +FollowSymlinks -MultiViews
RewriteEngine on

and that took care of the problem!