Forum Moderators: phranque

Message Too Old, No Replies

Yamr

Yet another mod_rewrite question

         

gitactive

7:04 pm on Feb 21, 2009 (gmt 0)

10+ Year Member



Hello,

I've been trying to achieve the following:

- Rewrite non www requests to www prefixed ones
- Disable access to .html files and maybe redirect to an address that doesnt show the extension
- Internally rewrite requests without extension to .html requests so that the file is called properly

The code works for requests to http://example.org but not for www.example.org. Hope someone can help.

Thx.

----- START

RewriteCond %{HTTP_HOST} !^www\.example\.org$
RewriteRule ^ http://www.example.org%{REQUEST_URI} [L,R=301]

RewriteCond %{THE_REQUEST} ^[A-Z]+\ /[^?\ ]+\.html [NC]
RewriteRule \.html$ - [F,NC,L]

RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.html -f [NC]
RewriteRule .* $0.html [L]

----- END

jdMorgan

7:22 pm on Feb 21, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



The code works for requests to http://example.org but not for www.example.org.

"It doesn't work for www.example.org" is not a very informative statement...

Please describe how you tested, including test URLs, what the desired results were, what the actual results were, how those results differed from your expectations, and include any relevant entries from your server error log.

Also, be sure you completely flush your browser cache before testing any changes to your configuration.

Jim

gitactive

8:26 pm on Feb 21, 2009 (gmt 0)

10+ Year Member



Test String --> Result in address bar

example.org/test --> http://www.example.org/test.html
Acces denied. NOT OK.

example.org/test.html --> http://www.example.org/test.html
Access denied, OK.

http://www.example.org/test --> http://www.example.org/test
File "test.html" is displayed. OK.

http://www.example.org/test.html --> http://www.example.org/test.html
Access denied, OK.

Why access denied on the first one, should redirect to http://www.example.org/test and display the file.
And why ".html" ?

jdMorgan

8:54 pm on Feb 21, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Good question. It shouldn't happen. This implies some recursion, or perhaps the action of mod_dir or mod_negotiation interfering with your code. Try these minor tweaks, though:

RewriteCond %{HTTP_HOST} !^www\.example\.org$
RewriteRule ^ http://www.example.org%{REQUEST_URI} [L,R=301]
#
RewriteCond %{THE_REQUEST} ^[A-Z]+\ /[b][^.?\ ][/b]+\.html [NC]
RewriteRule \.html$ - [b][F,NC][/b]
#
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME}.html [b]-f[/b]
RewriteRule [b](.*) /$1[/b].html [L]

[NC] cannot work with "-f", and [L] is redundant when used with [F].

Be sure that your custom 403 error page (if you have one) does not end with ".html". If it does, you will have to explicitly exclude it from your second rule.

Jim

gitactive

9:28 pm on Feb 21, 2009 (gmt 0)

10+ Year Member



Thx for the quick replies Jim.

Unfortunately, same result.

The server must be applying the ".html" extension on the first run throught as it detects the actual file. My apache knowledge is too limited to answer that one.

Any clue?

jdMorgan

1:48 pm on Feb 22, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Disable MultiViews and AcceptPathInfo, and test again. (See Apache core "Options" and "AcceptPathInfo" directives.

Jim

gitactive

3:19 pm on Feb 22, 2009 (gmt 0)

10+ Year Member



Disabling MultiViews did the job. That would have saved me a lot of time. Outstanding advice from Jim, again.

I'll try and modify the code to redirect requests with extensions permanently. Be back with questions :)