Forum Moderators: phranque

Message Too Old, No Replies

RewriteRule works locally, not remotely?

         

magarlick

1:51 pm on Jun 15, 2012 (gmt 0)

10+ Year Member



Hi all

I am having problems getting a RewriteRule to work. Locally, on my Wamp server, the following two work fine:

#Convert URL from /faqs/ to faqs.php
RewriteRule faqs/?$ faqs.php [L]

#Convert URLS from, eg., /faqs/faq1.html to faqs.php?faq=1
RewriteRule faqs/faq([^/\.]+).html faqs.php?faq=$1 [L]

But when I upload them to the live site, the second one does not work. I get a 404. I have tried upmteen variations of it but no luck. Regular expressions are Chinese as far as I am concerned, despite having tried reading the rules many times.

Thanks.

g1smd

6:18 pm on Jun 15, 2012 (gmt 0)

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



RewriteRule faqs/?$ faqs.php [L]

The above rule promotes Duplicate Content, allowing URLs with or without a trailing slash to deliver identical content. Serve content only for the URL without slash:
RewriteRule ^faqs$ /faqs.php [L]



RewriteRule faqs/faq([^/\.]+).html faqs.php?faq=$1 [L]

There are several code errors in that rule. Try this:
RewriteRule ^faqs/faq([^.]+)\.html /faqs.php?faq=$1 [L]



Make sure that all of the rewrites are listed AFTER your non-www to www redirect.


Before your non-www to www redirect, add this extra redirect:
RewriteRule ^faqs/$ http://www.example.com/faqs [R=301,L]


This sorts out the Duplicate Content issue mentioned above.

magarlick

8:17 pm on Jun 15, 2012 (gmt 0)

10+ Year Member



Thanks for your reply - much appreciated.

Your suggested rewrite of my first rule above works on my remote server (which I think is Linux) but not on my local WAMP server.

Your suggested rewrite of my second rule above works locally but not remotely.

g1smd

8:24 pm on Jun 15, 2012 (gmt 0)

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



Have you got any rules that apply to "faqs.php" as part of a requested URL?

magarlick

8:31 pm on Jun 15, 2012 (gmt 0)

10+ Year Member



No. Here is the whole file (everything works everywhere except the faqs stuff):

RewriteEngine on
RewriteBase /

#Convert URLS from, eg, /artwork/misc/ to gallery.php?gallery=misc
RewriteRule artwork/([^/\.]+)/?$ gallery.php?gallery=$1 [L]

#Convert URLS from, eg, /artwork/misc/smeg.html to image.php?gallery=misc&image=smeg
RewriteRule artwork/([^.]+)/([^.]+).html image.php?gallery=$1&image=$2 [L]

#Convert URLS from /faqs/ to faqs.php
RewriteRule ^faqs$ /faqs.php [L]

#Convert URLS from /faqs/faq1.html to faqs.php?faq=1
RewriteRule ^faqs/faq([^.]+)\.html /faqs.php?faq=$1 [L]


#Convert URLS from, eg, /licensing/prices.html to prices.php
RewriteRule licensing/prices.html prices.php [L]
RewriteRule licensing/info.html info.php [L]
RewriteRule licensing/copyright.html copyright.php [L]

#Convert URLS from index.html to index.php?lang=en, or index_fr.html tp index.php?lang=fr
RewriteRule index.html index.php?lang=en [L]
RewriteRule index.html index.php?lang=fr [L]

#Convert URLS from *.html to *.php
RewriteRule ([^.]+).html $1.php [L]

ErrorDocument 404 /error404.php

<IfModule !mod_php5.c>
AddType x-mapp-php5 .php
</IfModule>

g1smd

8:50 pm on Jun 15, 2012 (gmt 0)

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



Add the proper ^start anchor to all RegEx patterns.

Escape literal periods in all RegEx patterns\.

Add the /leading slash to all rule targets. All rule targets.

Your "fr" index rule can never work.

Several other patterns are non-optimum, especially
artwork/([^/\.]+)/?$
(where the
/?
promotes duplicate content and the
.
inside
[^ ]
does not need to be escaped) and
artwork/([^.]+)/([^.]+).html
(the first
[^.]+
doesn't "stop" at the slash).

magarlick

9:12 pm on Jun 15, 2012 (gmt 0)

10+ Year Member



Hello again. Thanks for those tips, but it still does not solve my original problem. Despite the errors in the code it all works, locally and remotely, except the two lines dealing with faqs. As I said, your suggestions don't help, as one works locally only and the other remotely only. Can you offer alternative syntax for these two rules, or can you see anything else in the code that would prevent then from working? Thanks.

g1smd

9:43 pm on Jun 15, 2012 (gmt 0)

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



Sometimes an error elsewhere has a knock on effect. So it's a good idea to fix all errors even when they don't appear to be causing a problem. At least one of the code errors leaves your server wide open to malicious path injection techniques.

lucy24

9:49 pm on Jun 15, 2012 (gmt 0)

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



Your "fr" index rule can never work.

I think it's a typo and he meant to say

RewriteRule index_fr.html index.php?lang=fr [L]

or rather

RewriteRule index_fr.html /index.php?lang=fr [L]

Escape literal periods in all RegEx patterns\.


Except inside brackets, as in your first rule in the most recent post. Within grouping brackets, nothing needs to be escaped except spaces and parentheses (which might occur in a Cond but never in a Rule) and brackets (which should never occur anyway).

g1smd

10:02 pm on Jun 15, 2012 (gmt 0)

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



or rather

RewriteRule ^index_fr\.html$ /index.php?lang=fr [L]


(4 changes)

In particular omitting the start or end anchor allows infinite duplicate content.