Forum Moderators: phranque

Message Too Old, No Replies

.htaccess RewriteRule not working on Godaddy

Regular Expression error on Godaddy but not on other systems

         

pampalone

6:50 pm on Aug 16, 2010 (gmt 0)

10+ Year Member



I am having a problem with a regular expression for .htaccess on a Godaddy shared server. Here is the .htaccess file.

--------------
Options Includes FollowSymLinks Multiviews
RewriteEngine On
RewriteBase /

RewriteCond %{HTTP_HOST} !^www\.example\.co\.sa [NC]
RewriteCond %{HTTP_HOST} !^$
RewriteRule ^.*$ http://www.example.co.sa/$1 [R=301]

#RewriteRule ^([A-Za-z0-9-]+).html index.php?pg=$1 [L]
#RewriteRule ^(([A-Za-z0-9\-]+/)*[A-Za-z0-9\-\/]+)?$ $1.php [NC,C]
RewriteRule ^forum/topics/[A-Za-z0-9-\/]+/(\d+) view-forum-topics.php?intId=$1 [NC,L]
RewriteRule ^forum/posts/[A-Za-z0-9-\/]+/(\d+)/(\d+) view-forum-posts.php?intCatId=$1&intId=$2 [NC,L]

Allow from all
--------------

Here is my error message:
[Sun Aug 15 19:33:44 2010] [alert] [client #*$!.#*$!.#*$!.#*$!] /virtual/path/example/.htaccess: RewriteRule: cannot compile regular expression '^forum/topics/[A-Za-z0-9-\\/]+/(\\d+)'\n

It appears that when I escape the forward slash in [A-Za-z0-9-\/] it won't compile. The purpose of the expression is to rewrite any directory path to a php file (see RewriteRule above). It is supposed to match the digits at the end for use in the php file.

The site is currently being hosted on a different system and the administrator gave me what is working on his system albeit the Rewrite Rule is written for a virtual host file (under the <Directory> tag, not a .htaccess file.

Has someone seen this before and know how to make it work?

Thanks,
Erik

jdMorgan

8:24 pm on Aug 16, 2010 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



This is likely an older Apache 1.x server.

Apache 1.x supports only POSIX regular expressions, and does not support the PCRE as does Apache 2.x.

Suggested re-write, with several additional corrections, especially to character-escaping:

#RewriteRule ^([A-Za-z0-9\-]+)\.html$ index.php?pg=$1 [L]
#RewriteRule ^(([a-z0-9\-]+/)*[a-z0-9\-/]+)?$ /$1.php [NC,L]
RewriteRule ^forum/topics/[A-Za-z0-9\-/]+/([0-9]+)$ view-forum-topics.php?intId=$1 [L]
RewriteRule ^forum/posts/[A-Za-z0-9\-/]+/([0-9]+)/([0-9]+)$ view-forum-posts.php?intCatId=$1&intId=$2 [L]

Jim

g1smd

9:22 pm on Aug 16, 2010 (gmt 0)

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



As well as the corrected escaping do not overlook the required [L] flag needed on every rule.

pampalone

3:03 am on Aug 17, 2010 (gmt 0)

10+ Year Member



Thank you both for your comments.

This is great! I am making progress. Here is what I found based on your comments.
Godaddy is running Apache 1.3.33

The following Rewrite Rule appears to almost work but it is not replacing the $1 in the rule.

Here is the rule I have written, which appear to work in [regextester.com...] choosing Ereg:
RewriteRule ^forum/topics/[a-zA-Z0-9/-]+/([0-9]+)view-forum-topics.php?intId=$1 [NC,L]

Here is the text I want to write the rule on:
forum/topics/City-Of-Origin/health-insurance/3

The intId=$1 is not being replaced by the number 3.

The result is:
view-forum-topics.php?intId=$1 [NC,L]
Not
view-forum-topics.php?intId=3 [NC,L]

Is there a way to make the replacement happen that I am missing?

Erik

wildbest

6:57 am on Aug 17, 2010 (gmt 0)

10+ Year Member



I am having a problem with a regular expression for .htaccess on a Godaddy shared server.

I had a similar problem recently with one hosting account there. Install "Live HTTP Headers" add-on for FF and test to see if response headers are from Apache server? You may have a Godaddy shared Linux/Apache account, but response might be from MS IIS. In this case your htaccess file will not work as expected, if working at all.

wildbest

7:59 am on Aug 17, 2010 (gmt 0)

10+ Year Member



RewriteRule ^forum/topics/[a-zA-Z0-9/-]+/([0-9]+)view-forum-topics.php?intId=$1 [NC,L]

Here is the text I want to write the rule on:
forum/topics/City-Of-Origin/health-insurance/3


Try this first:

RewriteRule ^forum/topics/[^/]+/[^/]+/([0-9]+)$ view-forum-topics.php?intId=$1 [NC,L]

pampalone

3:57 pm on Aug 17, 2010 (gmt 0)

10+ Year Member



I got it working. It started working when I put a / before the view-forum-#*$!.php?

Here is what worked:

RewriteRule ^forum/topics/[a-zA-Z0-9/-]+/([0-9]+) /view-forum-topics.php?intId=$1 [NC,L]
RewriteRule ^forum/posts/[a-zA-Z0-9/-]+/([0-9]+)+/([0-9]+) /view-forum-posts.php?intCatId=$1&intId=$2 [NC,L]

Again, this was Apache 1.3.33 on Godaddy shared hosting.

Thank you for all your help!

Erik