Forum Moderators: phranque

Message Too Old, No Replies

rewriterule not working for main directory

         

bicycling

4:46 am on Jan 21, 2009 (gmt 0)

10+ Year Member



Hi, i've got a htaccess file working for my domain. The redirection Works only when people type in www.example.com/index.html which will then redirect them to www.example.com/index.shtml.

While people surfing directly to www.example.com ...the DirectoryIndex set will automatically serve up index.html which is what i have intended.
I have a simple IP based htaccess script working which redirects the users....
My domain is on a Cpanel & Apache 2.0...any idea on how to correct it...
it's on the main directory..

RewriteRule ^index\.html$ index\.shtml [L]

jdMorgan

2:07 pm on Jan 21, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



That isn't a URL-to-URL external client redirect, it's a URL-to-filepath internal rewrite.

What, specifically, is the problem? You say the rule isn't working for your main directory, but then go on to say, "DirectoryIndex will automatically serve up index.html which is what i have intended." These statements appears to be contradictory.

If you wish to serve index.shtml in response to a request for "example.com/" then change the DirectoryIndex to specify index.shtml instead of (or prior to) index.html. However, the DirectoryIndex directive will be inherited by all subdirectories, so if you do this, then *all* subdirectories will use index.shtml as their index file (if it exists).

If you don't want that, then your rule can be changed to also apply to "example.com/" requests by making the URL-path optional in the pattern:


RewriteRule ^(index\.html)?$ index\.shtml [L]

Jim

g1smd

8:28 pm on Jan 21, 2009 (gmt 0)

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



I am also confused by the question.

The difference between rewrites and redirects is crucial here too.

bicycling

3:38 am on Jan 22, 2009 (gmt 0)

10+ Year Member



Thank You JD, sorry i'm lousy at explaining stuff. Yes your suggestion helped solved the problem.
RewriteRule ^(index\.html)?$ index\.shtml [L]

Didn't know the ()?$ solved the problem.
Yes JD you have put it correctly I want to "apply to "example.com/" requests by making the URL-path optional in the pattern"
The problem was to the main folder.

my script goes as:
RewriteCond %{REMOTE_ADDR} ^92\.
RewriteRule ^(.*)\.html$ /$1\.shtml [L]

For the above script how do i set it to apply to "example.com/" requests by making the URL-path optional

Don't seem to be able to set:
RewriteRule ^((.*)\.html)?$ /$1\.shtml [L]

The server returns a 404 page not found when infact it's already there.

thanks

jdMorgan

3:51 am on Jan 22, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Yes, since the $1 in the substitution path refers to the string matched by the first parenthesized sub-pattern, a request for example.com/ would result in a rewrite to example.com/.shtml - and that file does not exist, so you get a 404.

The easiest solution is a separate rule to handle that case, using "^$" as the pattern and /index.shtml as the substitution. Reproduce your REMOTE_ADDR RewriteCond if you also also want that condition on this new rule.

Jim

bicycling

4:41 am on Jan 22, 2009 (gmt 0)

10+ Year Member



Sorry Jim, can't work it out with my limited knowledge on this.
I tried adding a seperate rule ontop of that as suggested:
RewriteRule ^((.*)\.html)?$ /$1\.shtml [L]
RewriteRule ^$ index.shrml [L]

Any help here on this to now redirect multiple files from html to shtml ...as provided for index.html to index.shtml

thanks

jdMorgan

2:04 pm on Jan 22, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Well, you got it right, except for two things:

1) You cannot use the "RewriteRule ^((.*)\.html)?$ /$1\.shtml [L]" modification, because as you found out -- and as I explained, it will result in a rewrite to example.com/.html if example.com/ is requested. Put that back to what I posted above by removing the outer parentheses and the trailing question mark.

2) Your new rule is perfectly-correct, but as I mentioned, it appears that your existing rule requires an IP address range starting with "92." to invoke that rule. If you have the same requirement for the "/" page, then reproduce that RewriteCond for this new rule as well; RewriteConds only apply to the single rule that follows them, so if you need the condition to apply to more than one rule, then you need to reproduce the RewriteCond for both rules.

Jim

bicycling

3:00 am on Feb 9, 2009 (gmt 0)

10+ Year Member



Thanks Jim, i finally figured it out. Was on & off this project hence the delay in solving & thanking you.thanks

g1smd

8:50 pm on Feb 9, 2009 (gmt 0)

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



What was the solution? ... for the edification of future readers.

bicycling

10:08 am on Feb 13, 2009 (gmt 0)

10+ Year Member



Hi all thank you Jim for helping me out, so to contribute, although nothing much per say.

As advised by Jim, i created to rules as [er below.

RewriteCond %{REMOTE_ADDR} ^92\.
RewriteRule ^(.*)\.html$ /$1\.shtml [L]

RewriteCond %{REMOTE_ADDR} ^92\.
RewriteRule ^(index\.html)?$ index\.shtml [L]

No techie guy, but just needed some quick fix..thanks all.

jdMorgan

1:52 pm on Feb 13, 2009 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



There is no need to include "(index\.html)?" in the second rule's pattern, since that case will always be handled bu the first rule. And you should not escape periods in the substitution paths, because they are not regular-expressions patterns. I'd suggest:

RewriteCond %{REMOTE_ADDR} ^92\.
RewriteRule ^(.+)\.html$ /$1.shtml [L]
#
RewriteCond %{REMOTE_ADDR} ^92\.
RewriteRule ^$ /index.shtml [L]

There's no such thing as a "quick-fix." The code has to be close-to-perfect, or you can get unexplained problems later, and problems cost money.

Jim

[edited by: jdMorgan at 1:53 pm (utc) on Feb. 13, 2009]

bicycling

7:53 am on Feb 16, 2009 (gmt 0)

10+ Year Member



Thank you Jim for perfecting it, i totally agree with you. I am so far off from this modrewrite rules. Still need alot of time learning. Thank you very much all.