Forum Moderators: phranque

Message Too Old, No Replies

RewriteRule works on one Apache, doesn't on the other

rewriterules kills me

         

Herry

9:40 pm on Jun 2, 2010 (gmt 0)

10+ Year Member



Hi,

I have this weird problem. I developed a website on my local server, using so-called pretty urls, and a .htaccess file that has the following rewriterules.

Options +FollowSymLinks
RewriteEngine On

# Admin
RewriteRule ^admin/([a-zA-Z0-9_]+)$ /admin/index\.php?chapter=$1
RewriteRule ^admin/([0-9]+)/([a-zA-Z0-9_]+)/([a-zA-Z0-9_]+)$ /admin/index\.php?id=$1&chapter=$2
RewriteRule ^admin/([0-9]+)/([a-zA-Z0-9_]+)/([a-zA-Z0-9_]+)/([a-z]+)$ /admin/index\.php?id=$1&chapter=$2&page=$3&action=$4
RewriteRule ^admin/([0-9]+)/([a-zA-Z0-9_]+)/([a-zA-Z0-9_]+)/([0-9]+)/([a-z]+)$ /admin/index\.php?id=$1&chapter=$2&page=$3&subpage=$4&action=$5

# Site
RewriteRule ^home /index\.php?page=home
RewriteRule ^about /index\.php?page=about
RewriteRule ^contact /index\.php?page=contact
RewriteRule ^search /index\.php?page=search
RewriteRule ^names/([a-zA-Z0-9_]+) /index\.php?page=names&name=$1
RewriteRule ^objects/([a-zA-Z0-9_]+) /index\.php?page=objects&id=$1

It all worked flawless on my local server (Apache 2.2.15), but it fails on the server where I had to put the site live (Apache 2.0.63).
The weirdest is, it's not all wrong.
1) The Admin section works good on the live server. I didn't touch it.

2) I originally used and end-anchor in the rewriterules, like this.
RewriteRule ^home$ /index\.php?page=home
But it didn't work. Deleting the '$' made the single-pages work.

3) The pages using 2 variables (the last 2) never worked at all, no matter what I changed (note! They DO work in my local server).

Does anybody have a clue?
Major difference between Apache 2.0.63 and 2.2.15?
Do I miss out on something?

I spend 2 days Googling, but to no avail.

Please help, before I get a nervous breakdown.

Thanks
Herry

g1smd

9:51 pm on Jun 2, 2010 (gmt 0)

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



For starters, you need to add the [L] flag to every rule in that lot.

Escape the literal periods in the patterns "on the left", but do NOT add any escaping to the target filepath "on the right".

You also need to end anchor your patterns. At present a request for example.com/home and example.com/home987654321 will both display the same content.

The A-Za-z part of the pattern can be replaced with a-z and the [NC] flag to speed up the processing of each rule.

Herry

10:41 pm on Jun 2, 2010 (gmt 0)

10+ Year Member



Thanks for your quick reply. Unfortunately it didn't help.
The [L] flag didn't change the behaviour a bit.
The end anchor, I had before, and it only seemed to work in the admin section. When I removed it in the entries in the site section, they finally worked. But the last two lines -the ones with with the 2 variables, never worked.

The weird thing is that the original file worked flawless on my home server.

g1smd

10:52 pm on Jun 2, 2010 (gmt 0)

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



Whether it fixes "this" problem or not, you still do need all of the things I mentioned above, even the fixes to the escaping.

Herry

11:05 pm on Jun 2, 2010 (gmt 0)

10+ Year Member



I did, tho... But it only made things worse.
BTW what do you mean escaping? There's no "literal periods" on the left.
The only periods I see are the ones in "index.php" and they are escaped like this "index\.php"
Maybe you mean I should take away the escapes on the right?

Or do I misunderstand something?

Herry

11:08 pm on Jun 2, 2010 (gmt 0)

10+ Year Member



I removed the escaped in the target path, resulting in things like
RewriteRule ^contact$ /index.php?page=contact [L]

It only makes things worse

jdMorgan

1:10 am on Jun 3, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



More likely a difference in server configuration than any version-specific change.

Try modifying your "Options" directive as shown below.

If that doesn't cure the problem, try adding the second line.

Options +FollowSymLinks -MultiViews
AcceptPathInfo Off

Jim

jdMorgan

1:15 am on Jun 3, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Also, once you've made the fixes g1smd recommended above, further optimization is possible. For example:

RewriteRule ^(home|about|contact|search)$ /index.php?page=$1 [L]

replaces four of your current rules.

Jim

Herry

6:23 am on Jun 3, 2010 (gmt 0)

10+ Year Member



Thanks g1smd for giving me clues how to tidy up my .htaccess file.
And thanks jdMorgan for giving me the final kick in. -Multiviews did the trick!