Forum Moderators: phranque

Message Too Old, No Replies

.htaccess

Cannot make rewrite rules work...

         

Stine

3:06 pm on Jan 14, 2005 (gmt 0)

10+ Year Member



Hi :)

I have added some content to my .htacces file that should help me in getting the URL

[mydomain.com...]

when I type either

[mydomain.com...]

or

[mydomain.com...]

In both cases I get the URL

[mydomain.com...]

though.

This is what is in my .htacces file:

RewriteEngine on
RewriteBase /

RewriteRule ^$ /benvenuti.html [R]

RewriteCond %{QUERY_STRING} ^l=italian$
RewriteRule ^welcome\.html$ /benvenuti.html [R]
RewriteCond %{QUERY_STRING} ^$
RewriteRule ^benvenuti\.html /welcome.html?l=italian [L]
RewriteCond %{QUERY_STRING} ^(.+)$
RewriteRule ^benvenuti\.html /welcome.html?l=italian&%1 [L]

RewriteCond %{QUERY_STRING} ^$
RewriteRule ^(welcome¦aboutMe¦cv¦tasks¦contact)\.html$ /index.php?m=$1 [L]

RewriteCond %{QUERY_STRING} ^(.+)$
RewriteRule ^(welcome¦aboutMe¦cv¦tasks¦contact)\.html$ /index.php?m=$1&%1 [L]

Can anyone tell me what is wrong? Any help is greatly appreciated <:}

Thanks,
Stine

[edited by: Stine at 3:29 pm (utc) on Jan. 14, 2005]

Stine

3:27 pm on Jan 14, 2005 (gmt 0)

10+ Year Member



Must be an easy one for you clever guys?:}

Stine

3:41 pm on Jan 14, 2005 (gmt 0)

10+ Year Member



Maybe it is my use of [L] and [R]?

jdMorgan

3:45 pm on Jan 14, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Stine,

Welcome to WebmasterWorld!

Two questions:

1) What is the result when you test these rules, a 500-Server Error, or "no rewrite happens"?

2) Do you have any other RewriteRules that are working on your server?

Jim

Stine

3:56 pm on Jan 14, 2005 (gmt 0)

10+ Year Member



Thank you Jim:}

Ad 1) It rewrites into [mydomain.com...]

Ad 2) No

Stine

jdMorgan

4:23 pm on Jan 14, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Ah, OK.

How about this simpler version?


# declare default page for "/"
DirectoryIndex benvenuti.html
#
# added "?" to clear the query string
RewriteCond %{QUERY_STRING} ^l=italian$
RewriteRule ^welcome\.html$ /benvenuti.htm[b]l?[/b] [R=301,L]
#
# this replaces two rules by detecting missing "l=italian" query and using [QSA]
RewriteCond %{QUERY_STRING} !l=italian
RewriteRule ^benvenuti\.html /welcome.html?l=italian [QSA,L]
#
# this replaces two rules by detecting missing "m=" query and using [QSA]
RewriteCond %{QUERY_STRING} !m=
RewriteRule ^(welcome¦aboutMe¦cv¦tasks¦contact)\.html$ /index.php?m=$1 [QSA,L]

See the Apache mod_rewrite documentation [httpd.apache.org] for the description of the [QSA] (query string append) flag, and the use of "?" at the end of the substitution URL to clear an existing query string.

Jim

Stine

4:56 pm on Jan 14, 2005 (gmt 0)

10+ Year Member



Thank you for your answer :}

I have put your rules into my .htaccess file... and now I get a "You are not authorized to view this page"...

Any idea? <:}

Stine

jdMorgan

5:52 pm on Jan 14, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



> I have put your rules into my .htaccess file... and now I get a "You are not authorized to view this page"...

Please provide more information in your posts. We cannot see your monitor. If you make us ask a whole bunch of questions to help fix *your* problems, then this will take a very long time.

What URL did you request with your browser?

What URL is in the browser address bar when you see this "You are not authorized"?

Are you seeing this your 401-Authentication Required page, or your 403-Forbidden page?

What is in your server error log file after this error happens?

What other code in httpd.conf, .htaccess files, or scripts, do you have that requires authorization or might invoke a 403-Forbidden response? (There is certainly no change that I proposed that would invoke authorization).

Jim

jdMorgan

7:03 pm on Jan 14, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Looking at this again, it's possible that the first two rules are creating a loop -- one URL is redirected to the other, then that new URL is rewritten back to the original, and so on until the browser or server hits its redirection limit. At that point you *should* get a 500-Server Error. However, some other configuration problem may cause the 500-Server Error page to be forbidden.

If this is the case, then the following code should prevent the looping:


# declare default page for "/"
DirectoryIndex benvenuti.html
#
# added "?" to clear the query string
[b]RewriteCond %{THE_REQUEST} /welcome\.html\?l=italian[/b]
RewriteRule ^welcome\.html$ http://www.yourdomain.com/benvenuti.html? [R=301,L]
#
# this replaces two rules by detecting missing "l=italian" query and using [QSA]
RewriteCond %{QUERY_STRING} !l=italian
RewriteRule ^benvenuti\.html /welcome.html?l=italian [QSA,L]
#
# this replaces two rules by detecting missing "m=" query and using [QSA]
RewriteCond %{QUERY_STRING} !m=
RewriteRule ^(welcome¦aboutMe¦cv¦tasks¦contact)\.html$ /index.php?m=$1 [QSA,L]

However, this does not fix the problem of the error page being forbidden. About all I can suggest for that is to explicitly declare your own custom error pages (you will also have to create each of these pages) by adding:

# Error page for "Authentication Required" (Incorrect username or password was entered)
ErrorDocument 401 /401err.html
#
# Error page for "Not Found" (Requested resource was not found - reason unknown)
ErrorDocument 404 /404err.html
#
# Error page for "Gone" (Requested resource was not found because it has been intentionally deleted)
ErrorDocument 410 /410err.html
#
# Error page for "Server Error" (Any internal server error)
ErrorDocument 500 /500err.html

By defining your own error pages, you gain more control, possibly avoiding a server configuration error that your host cannot or will not correct.

Jim

Stine

11:45 am on Jan 16, 2005 (gmt 0)

10+ Year Member



Hi Jim!

Thanks a lot for your thorough replies!

After replacing DirectoryIndex benvenuti.html by RewriteRule ^$ /benvenuti.html [R=301] and removing the first two L's it worked just fine! And now I know about QSA:} Thanks!

My only problem now is that my hidden values (<INPUT TYPE="HIDDEN" NAME=...) do not seem to survive the rewriting! I cannot fetch them from the $_POST (im doing php) at least... do you happen to be familiar with this problem?<:}

Thanks,
Stine

Stine

1:03 pm on Jan 17, 2005 (gmt 0)

10+ Year Member



... or if anyone else knows of course:) The problem is really driving me nuts>}

Thanks,
Stine

jdMorgan

4:39 pm on Jan 17, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



POSTs cannot be redirected, out of respect for the POSTer's security.

Either change your method to GET, or add an exclusion to the rules to avoid redirecting POSTs. For example,


RewriteCond %{HTTP_METHOD} !^POST$

Jim