homepage Welcome to WebmasterWorld Guest from 54.237.99.131
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Pubcon Platinum Sponsor 2014
Home / Forums Index / Code, Content, and Presentation / Apache Web Server
Forum Library, Charter, Moderators: Ocean10000 & incrediBILL & phranque

Apache Web Server Forum

    
trying to finalize my first mod rewrite, I need some help
blaketar

10+ Year Member



 
Msg#: 3554723 posted 4:48 pm on Jan 22, 2008 (gmt 0)

Practicing with my first Rewrite Rule and I have the basics but quite a few parameters elude me, hoping for some assistance.
Here is what I am trying to accomplish. I have a directory named "options" in this directory are three files, a .htaccess, index.html and 404.html

1st Rule Works: www.domain.com/options/AnyThing09AZ/
rewrite to: => /options/index.html?row=$1

2nd Rule DOESNíT WORK: www.domain.com/options/AnyThing09AZ
rewrite to: => redirect to include trailing slash, then 1st rule can apply

3rd Rule Works: www.domain.com/options/AnyThing09AZ/More09AZ/
rewrite to: => /options/index.html?row=$1&col=$2

4th Rule DOESNíT WORK: www.domain.com/options/AnyThing09AZ/More09AZ
rewrite to: => redirect to include the missing trailing slash, then 3rd rule can apply

5th Rule I DONíT UNDERSTAND: Anything deeper than these TWO would be garbage so it could either redirect back to 3 or ignore. Example: www.domain.com/options/AnyThing09AZ/More09AZ/more/garbage/

"more" and "garbage" would be ignored and it would still rewrite to: /options/index.html?row=$1&col=$2

Here is what I have thus far in the .htaccess which is located in the options directory.
RewriteEngine on
RewriteRule ^(.*)/(.*)/$ /options/index.html?row=$1&col=$2 [L]
RewriteRule ^(.*)/$ /options/index.html?row=$1 [L]
ErrorDocument 404 /options/404.html

 

jdMorgan

WebmasterWorld Senior Member jdmorgan us a WebmasterWorld Top Contributor of All Time 10+ Year Member



 
Msg#: 3554723 posted 5:06 pm on Jan 22, 2008 (gmt 0)

I see only three rules here. I see no rules to add a trailing slash.

Three hints to fix your problems:

1. Do not use "(.*)" unless absolutely necessary; The pattern ".*" is ambiguous and 'greedy', and will match as many slash sequences (directories) as possible into the first ".*" leaving only the final level to satisfy the second ".*". Therefore, the first rule shown in your post will match any number of directory levels, but will put the extra multiple-directory-level path information into the $1 variable instead of $2, which is not what you want.

To disambiguate, use a pattern of "([^/]+)" -- That is, "Match one or more characters except a slash" -- or equivalently, "Match characters until you find the next slash."

2. Put external redirects first, followed by internal rewrites; Your slash-appending rules should all go first.

3. Then within those sections, put your rules in order from most-specific (longest pattern) to least-specific (as you have done so far).

Jim

blaketar

10+ Year Member



 
Msg#: 3554723 posted 5:22 pm on Jan 22, 2008 (gmt 0)

Thanks so much for your response! I should have specified I DONT UNDERSTAND on the redirects rather than DOESNT WORK.

Rewrite rule 2 and 4 I do not understand.

jdMorgan

WebmasterWorld Senior Member jdmorgan us a WebmasterWorld Top Contributor of All Time 10+ Year Member



 
Msg#: 3554723 posted 10:08 pm on Jan 22, 2008 (gmt 0)

If you mean you don't know how to do it, then this code has been posted here many times:

# If requested URL does not contain a period and does not have a trailing slash
RewriteCond $1 !(\.¶/$)
# redirect to add a trailing slash
RewriteRule (.*) http://www.example.com/$1/ [R=301,L]

Replace the broken pipe "¶" character with a solid pipe character before use; Posting on this forum modifies the pipe character.

This rule must be placed before the other two rules above.

Jim

[edited by: jdMorgan at 12:23 am (utc) on Jan. 23, 2008]

blaketar

10+ Year Member



 
Msg#: 3554723 posted 11:20 pm on Jan 22, 2008 (gmt 0)

Thanks, that code works great with the exception of the actual directory the .htaccess is in.

Now for instance when I browse to: www.example.com/options OR www.example.com/options/ I get a 404 error. Appending index.html to the end of the URL brings me in quickly however.

It does the job for the other missing slashes however. I also modified the rule to include the real directory.

# redirect to add a trailing slash
RewriteRule (.*) http://www.example.com/options/$1/ [R=301,L]

[edited by: jdMorgan at 12:24 am (utc) on Jan. 23, 2008]
[edit reason] example.com please [/edit]

jdMorgan

WebmasterWorld Senior Member jdmorgan us a WebmasterWorld Top Contributor of All Time 10+ Year Member



 
Msg#: 3554723 posted 12:24 am on Jan 23, 2008 (gmt 0)

Sorry, you'll have to use REQUEST_URI then:

# If requested URL does not contain a period and does not have a trailing slash
RewriteCond %{REQUEST_URI} !(\.¶/$)
# redirect to add a trailing slash
RewriteRule (.*) http://www.example.com/options/$1/ [R=301,L]

Jim

Global Options:
 top home search open messages active posts  
 

Home / Forums Index / Code, Content, and Presentation / Apache Web Server
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Home ¦ Free Tools ¦ Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About ¦ Library ¦ Newsletter
WebmasterWorld is a Developer Shed Community owned by Jim Boykin.
© Webmaster World 1996-2014 all rights reserved